Collection of bad coding practices

Null returned from a method returning a collection or an array

with 6 comments

Null should never be returned from a method returning a collection or an array. Instead return a empty array (a static final empty array) or one of the empty collections (e.g. Collections.EMPTY_LIST assuming the client should not be modifying the collection).

(submitted by Rand McNeely)


Written by Alex

November 11, 2009 at 5:10 pm

Posted in Collections

Tagged with , ,

6 Responses

Subscribe to comments with RSS.

  1. I fully agree, but I would prefer Collections.emptyList() to Collections.EMPTY_LIST because the former one is typesafe.


    December 10, 2009 at 11:15 pm

  2. I also fully agree with this premiss and with the comment regarding Collections.emptyList(). Furthermore, if a possible return value is an unmodifiable Collections.emptyList(), please consider wrapping the actual list return type as an unmodifiable collection to maintain consistency between an empty return value and one with values – the unmodifiable nature should be maintained for both cases.

    Frank Hellwig

    April 11, 2010 at 11:43 pm

  3. Keep in mind that JAX-WS requires that returned collections be modifiable even when empty, so returning Collections.empty*() will cause errors on the client end. I’d prefer that JAX-WS handled these read-only collections properly, but we can’t have our all-powerful everything interface and eat it too =)

    Christopher Kovacs

    July 28, 2010 at 8:13 pm

  4. I Disagree strongly. Sometimes, there can be a lexical difference between passing back NULL and passing back an empty Array, and I don’t feel the two should be viewed as interchangeable.


    November 29, 2011 at 7:04 am

  5. @Francis

    I agree: semantics of NULL and of an empty collection is different and cannot be interchangeable.

    But do you have any idea how it could be used in practice? I see the only purpose of passing NULL instead of a collection: to notify the caller that something went wrong and the method failed to populate the collection. This is the case where exceptions work much better, IMO.


    November 29, 2011 at 7:44 am

    • Mega old thread, but I thought i’d add my bit…

      I think that when an empty list should never occur, then null should be returned. While an exception would usually be the appropriate course of action, when the lack of results indicated a bug, rather than a business exception, then a null pointer down the line should do just as well. By throwing a runtime exception you’re just littering your code with error handling code for things that will likely never happen, and unless you write a test for that particular case (which is a bug, not a business exception) then your code coverage will be diluted.


      March 16, 2012 at 3:30 pm

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 )

Google+ photo

You are commenting using your Google+ 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 )

Connecting to %s

%d bloggers like this: