Empty IEnumerable or null
-
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 fromNextVersions
(as I am doing now) or an IEnumerable with no items? -
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 fromNextVersions
(as I am doing now) or an IEnumerable with no items?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
-
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 fromNextVersions
(as I am doing now) or an IEnumerable with no items?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 -
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
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.
-
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
NextI think I will go with this approach. Like you said, it gives less room for error ("pit of success" and all that).