The topic of returning nulls, empty objects and empty collections came up today and we'd like to get the opinion of others. We have read the discussion on returning null for object and separately the discussion on returning empty collections and agree with the marked answers for both topics. However, in our case, we have a class that contains a Foo.Item class and a Foo.Items class, which is a collection of Foo.Item objects. If Foo.Item returns null, does Foo.Items collection return null as well or should it return an empty collection?
-
2After reading the links, no answer is actually needed. Its all provided in your already provided links. – Kyle Rosendo Oct 06 '10 at 18:32
3 Answers
A collection is an object. There are times when an empty object is better than null, and this comes up particularly often with collections, hence the inclination for people to argue in favour of empty collections. There are problems that empty objects can cause, and these come up more often with other types (particularly value-semantic types), hence the inclination for people to argue against empty objects when you don't particularly mention empty collections.
However, at the end of the day, and empty collection is still and empty object.
It's worth considering that the main reason for using empty collections is also exactly the same reason why empty collections should sometimes not be used, namely that we don't want to have to test for null before doing a for-each.
Okay, so far so good, we return an empty collections all the time and foreach code calling them becomes easier to write.
But wait a minute, there's a flaw here. It could be useful for governments; foreach through the new claims for unemployment assistance made in November, and add up how much its going to cost the exchequer that month. Answer: zero! The reason being that since it's currently October, we don't have any new claims from November yet. The correct answer here is not an empty collection, it's null.
And of course, it's precisely the same sort of example people will use with non-collection empty objects.
So, they each have their place. So also does the middle ground; return a null object, but in some cases coalesce it with an empty object on receiving it.

- 110,372
- 10
- 146
- 251
Your abstract description is not enough. This is (mostly) not a technical issue with a single answer but a design matter.
If your class also has Foo.Widget and Foo.Widgets, the answers for Item and Widget will not necessarily be the same.
But in general, the collection properties should return empty collections, unless there is a good reason not too. And any code consuming those collection should still check for null. The official (library) recommendation is that of a double-safety.

- 263,252
- 30
- 330
- 514
In case of object you should return it as null because that will allow a quick check for nullability. In case of collection you should return empty list. Empty list is very important because consumer of your code would not need to check for nullability. Considering that they will be using foreach
whatever processing they might be doing will be skipped. If on UI they are trying to draw up a table, they will show empty table. Empty collections are the way to go because of all these benefits.

- 3,258
- 1
- 23
- 36
-
How does the calling code tell the difference between a collection that doesn't exist and a collection that exists and is empty then? – Jon Hanna Oct 08 '10 at 10:52
-
From my experience collection does not exist and collection being empty are more or less same. Consider this; I have collection of RSVPs which is null. In most scenarios this also will be considered as nobody has confirmed to attend and that is similar to having collection of RSVPs as empty collection. You can say I can have null collection even if I intended to make it empty due to some technical error. Well in that case any way there is an error that I can not handle. I still favour empty collection instead of null collection. – Pradeep Oct 08 '10 at 11:11