You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Josh Foure <us...@yahoo.com> on 2006/01/11 15:43:28 UTC

[all] Null vs. Empty Lists

Hi all,

I have noticed that a lot of the commons libraries
return empty Lists instead of null when a method is
declared to return a List and  there are no results. 
I have also noticed this in other libraries such as
Xerces and Xalan.  Is there a best practice concerning
when to return null and when to return an empty List? 
I see advantages to returning the empty List in that
you can can always get the iterator without checking
for null (and iterate through 0 elements when the List
is empty) or use addAll() on another List without
first checking for null.  By removing all the null
checks it seems to make the code cleaner.  However,
returning null may be a little more consistent in that
most methods that return a single result object tend
to return null if the object was not retrieved so why
would a collection be any different?  

I would love to hear your thoughts on this.
Thanks,
Josh



__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


Re: [all] Null vs. Empty Lists

Posted by Valerio Schiavoni <va...@gmail.com>.
i also suggest to take a look at the Null Object design patter, as it
addresses exactly this issue.
you can google for it, or just follow this link:

http://www.cs.oberlin.edu/~jwalker/nullObjPattern/

2006/1/11, Josh Foure <us...@yahoo.com>:
>
> Hi all,
>
> I have noticed that a lot of the commons libraries
> return empty Lists instead of null when a method is
> declared to return a List and  there are no results.
> I have also noticed this in other libraries such as
> Xerces and Xalan.  Is there a best practice concerning
> when to return null and when to return an empty List?
> I see advantages to returning the empty List in that
> you can can always get the iterator without checking
> for null (and iterate through 0 elements when the List
> is empty) or use addAll() on another List without
> first checking for null.  By removing all the null
> checks it seems to make the code cleaner.  However,
> returning null may be a little more consistent in that
> most methods that return a single result object tend
> to return null if the object was not retrieved so why
> would a collection be any different?
>
> I would love to hear your thoughts on this.
> Thanks,
> Josh
>
>
>
> __________________________________________________
> Do You Yahoo!?
> Tired of spam?  Yahoo! Mail has the best spam protection around
> http://mail.yahoo.com
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-user-help@jakarta.apache.org
>
>


--
To Iterate is Human, to Recurse, Divine
James O. Coplien, Bell Labs

Re: [all] Null vs. Empty Lists

Posted by Alessio Pace <al...@gmail.com>.
There is a little about this on the nice book "Effective Java
Programming", as far as I remember.

And yes, if you have a method which declares to return a collection of
something, is good to return an empty collection instead of null, for
the reasons you have said in your post :)

On 1/11/06, Josh Foure <us...@yahoo.com> wrote:
> Hi all,
>
> I have noticed that a lot of the commons libraries
> return empty Lists instead of null when a method is
> declared to return a List and  there are no results.
> I have also noticed this in other libraries such as
> Xerces and Xalan.  Is there a best practice concerning
> when to return null and when to return an empty List?
> I see advantages to returning the empty List in that
> you can can always get the iterator without checking
> for null (and iterate through 0 elements when the List
> is empty) or use addAll() on another List without
> first checking for null.  By removing all the null
> checks it seems to make the code cleaner.  However,
> returning null may be a little more consistent in that
> most methods that return a single result object tend
> to return null if the object was not retrieved so why
> would a collection be any different?
>
> I would love to hear your thoughts on this.
> Thanks,
> Josh
>
>
>
> __________________________________________________
> Do You Yahoo!?
> Tired of spam?  Yahoo! Mail has the best spam protection around
> http://mail.yahoo.com
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-user-help@jakarta.apache.org
>
>


--
Alessio Pace.

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org