#7 Functions Returning Collections, should never return null

05 Oct

A function that returns a collection, such as a list of all files in a folder, all sections on a page, or all attachments on a web site, should never return null.  It is a simple rule, and if followed reduces the complexity of the calling code.

The type being returned could be an array or any kind of collection class, such as a Vector, a List, a Hashtable, whatever.  In these situations, the method should never return null. If there are no matches for the particular query, it should return an empty collection. If there is an error, it should throw an exception. There is no situation that justifies returning a null value.

Returning a null means that the code that calls it always has to check for null, to avoid null pointer exception.

Often a collection is retrieved in order to iterate through all the elements. An empty collection works nicely, causing the iteration to execute zero times, which is what you want to have happen when there are no instances of what you are looking for. The code can be written cleanly and handle all cases equally well.

If you wish to do something different on the zero case, you can test for an empty connection as easily as you can test for null. But that test need be done only when you really want to do something different on the zero case.


A method returning a collection should never return null.

Leave a comment

Posted by on October 5, 2011 in Coding


Tags: ,

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: