Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Design and Architecture
  4. Empty IEnumerable or null

Empty IEnumerable or null

Scheduled Pinned Locked Moved Design and Architecture
questionannouncement
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    Gideon Engelberth
    wrote on last edited by
    #1

    I have a data model where each record has a "next version pointer" to track updates to the items. As it stands now, the object looks something like this:

    Public MustInherit Class VersionedDataObject(Of T As VersionedDataObject(Of T))

    Private \_next As T
    Public Property NextVersion() As T
        Get
            Return \_next
        End Get
        Set(ByVal value As T)
            If value Is \_next Then Exit Property
            \_next = value
    
            If \_next Is Nothing Then
                \_nextVersions = Nothing
            Else
                \_nextVersions = New NextVersionIterator(Me)
            End If
        End Set
    End Property
    
    Private \_nextVersions As IEnumerable(Of T)
    Public ReadOnly Property NextVersions() As IEnumerable(Of T)
        Get
            Return \_nextVersions
        End Get
    End Property
    
    'other common stuff
    

    End Class

    The question is, when NextVersion is Nothing, would you return Nothing from NextVersions (as I am doing now) or an IEnumerable with no items?

    L D 2 Replies Last reply
    0
    • G Gideon Engelberth

      I have a data model where each record has a "next version pointer" to track updates to the items. As it stands now, the object looks something like this:

      Public MustInherit Class VersionedDataObject(Of T As VersionedDataObject(Of T))

      Private \_next As T
      Public Property NextVersion() As T
          Get
              Return \_next
          End Get
          Set(ByVal value As T)
              If value Is \_next Then Exit Property
              \_next = value
      
              If \_next Is Nothing Then
                  \_nextVersions = Nothing
              Else
                  \_nextVersions = New NextVersionIterator(Me)
              End If
          End Set
      End Property
      
      Private \_nextVersions As IEnumerable(Of T)
      Public ReadOnly Property NextVersions() As IEnumerable(Of T)
          Get
              Return \_nextVersions
          End Get
      End Property
      
      'other common stuff
      

      End Class

      The question is, when NextVersion is Nothing, would you return Nothing from NextVersions (as I am doing now) or an IEnumerable with no items?

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      Why don't you look at what .NET does itself, e.g. the methods operating on a Queue, such as Dequeue. :)

      Luc Pattyn


      I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


      G 1 Reply Last reply
      0
      • G Gideon Engelberth

        I have a data model where each record has a "next version pointer" to track updates to the items. As it stands now, the object looks something like this:

        Public MustInherit Class VersionedDataObject(Of T As VersionedDataObject(Of T))

        Private \_next As T
        Public Property NextVersion() As T
            Get
                Return \_next
            End Get
            Set(ByVal value As T)
                If value Is \_next Then Exit Property
                \_next = value
        
                If \_next Is Nothing Then
                    \_nextVersions = Nothing
                Else
                    \_nextVersions = New NextVersionIterator(Me)
                End If
            End Set
        End Property
        
        Private \_nextVersions As IEnumerable(Of T)
        Public ReadOnly Property NextVersions() As IEnumerable(Of T)
            Get
                Return \_nextVersions
            End Get
        End Property
        
        'other common stuff
        

        End Class

        The question is, when NextVersion is Nothing, would you return Nothing from NextVersions (as I am doing now) or an IEnumerable with no items?

        D Offline
        D Offline
        David Skelly
        wrote on last edited by
        #3

        I would return an empty enumerable from NextVersions. That way, the consumer of this property does not have to include a test for whether the IEnumerable is Nothing, but can just go ahead use it. If it's empty, then the iteration over the enumerator will simply do nothing. That makes life simpler for the person using your VersionedDataObject and takes away the risk of getting a NullReferenceException when someone forgets to check for Nothing. So, you can just do something like:

        For Each version In myVersionedObject.NextVersions
        ' do something with version
        Next

        G 1 Reply Last reply
        0
        • L Luc Pattyn

          Why don't you look at what .NET does itself, e.g. the methods operating on a Queue, such as Dequeue. :)

          Luc Pattyn


          I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


          G Offline
          G Offline
          Gideon Engelberth
          wrote on last edited by
          #4

          Not a bad idea. My object does not directly implement IEnumerable like Queue or List (and isn't a 'collection' type of object), so I'll have to look for other objects to find some examples. For now I think I will go with suggestion from the other response.

          1 Reply Last reply
          0
          • D David Skelly

            I would return an empty enumerable from NextVersions. That way, the consumer of this property does not have to include a test for whether the IEnumerable is Nothing, but can just go ahead use it. If it's empty, then the iteration over the enumerator will simply do nothing. That makes life simpler for the person using your VersionedDataObject and takes away the risk of getting a NullReferenceException when someone forgets to check for Nothing. So, you can just do something like:

            For Each version In myVersionedObject.NextVersions
            ' do something with version
            Next

            G Offline
            G Offline
            Gideon Engelberth
            wrote on last edited by
            #5

            I think I will go with this approach. Like you said, it gives less room for error ("pit of success" and all that).

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups