• Post Reply Bookmark Topic Watch Topic
  • New Topic

should a method return null anytime?  RSS feed

 
Arun Prasath
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have been thinking that a method returning null is a wrong practice. After reading some of the other posts at javaranch, I would like to know can we return null from any method?
The best way rather is to throw exceptions right? than returning null.

If we could return null, then at what circumstances we can return null..
can someone explain please??
thanks
s.arun prasath
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can return null whenever you want. Just make sure you document your methods.
Throwing an exception instead of returning null as a means of indicating an error is indeed preferred, but there's certainly cases where returning null is appropriate.
 
Alexandru Popescu
Ranch Hand
Posts: 995
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Throwing an exception instead of a null means that the contract of the method is broken. Returning a null may mean something in your application.

--
:alex |.::the_mindstorm::.
 
Chengwei Lee
Ranch Hand
Posts: 884
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Consider the scenario whereby you wrote a method in your DAO to retrieve a collection of objects based on some criterias. And if your search returns no result, what are you going to do?

Are you going to throw a NoObjectMetSearchCriteriaException? Or, you could simply return null.

By throwing the exception, the caller of the method had to handle it. But think about, what else could the caller do since his criterias don't yield any results? Pretty much nothing in my opinion, unless he changes them.

By returning null, there's no need for the caller handle the exception. All he need to do is to test if the collection returned is null or otherwise & could proceed on.
 
Pho Tek
Ranch Hand
Posts: 782
Chrome Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Throwing an exception is actually an expensive operation. Read this
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> I have been thinking that a method returning null is a wrong practice.

Sun mustn't think so - consider the return value of showInputDialog() when
you select 'cancel', 'x' or 'escape'
 
Horatio Westock
Ranch Hand
Posts: 221
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Chengwei Lee:
Consider the scenario whereby you wrote a method in your DAO to retrieve a collection of objects based on some criterias. And if your search returns no result, what are you going to do?


In this situation I'd probably return an empty collection. If it was a query for a single item, I'd probably have two methods, one that threw an exception if it didn't exist, and one that returned null. These things are really specific to your application though.

I totally agree however, that it's a matter of what suits your application, and good documentation is the key.
 
James Carman
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Chengwei Lee:
Consider the scenario whereby you wrote a method in your DAO to retrieve a collection of objects based on some criterias. And if your search returns no result, what are you going to do?

Are you going to throw a NoObjectMetSearchCriteriaException? Or, you could simply return null.

By throwing the exception, the caller of the method had to handle it. But think about, what else could the caller do since his criterias don't yield any results? Pretty much nothing in my opinion, unless he changes them.

By returning null, there's no need for the caller handle the exception. All he need to do is to test if the collection returned is null or otherwise & could proceed on.



You used a bad example, but the idea is sound. For a method that returns a collection, you can simply return an empty collection rather than null. But, for a method which returns a single value (find a Person by social security number), null is okay as an indicator that "I didn't find it."
 
James Carman
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, didn't read Horatio's response first. But, man is he smart! He said the same thing I did (more or less)!
 
Chengwei Lee
Ranch Hand
Posts: 884
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by James Carman:
You used a bad example, but the idea is sound. For a method that returns a collection, you can simply return an empty collection rather than null. But, for a method which returns a single value (find a Person by social security number), null is okay as an indicator that "I didn't find it."


You're suggesting that an empty collection implies that there're no results instead of returning null?

Frankly speaking I don't see the significant difference in either. Or what makes returning null so wrong/bad. As long as the documentation/Javadoc is clear about the meaning of the return value, it should suffice.

Perhaps its a matter of choice or personal preferences.
 
Steven Bell
Ranch Hand
Posts: 1071
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For a method that returns an Array or Collection I would never return a null, but always return an empty Collection/Array. I don't think there is anything wrong about returning a null, but returning an empty Collection/Array makes for a much more elegant solution.

When you are getting an Array/Collection from some method you almost always go into some loop right after. If a null can be returned then you have to check for it, but if an empty Collection is returned is simply passes the loop.
 
James Carman
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Returning the empty collection avoids the potential for a NullPointerException and allows you to plug the return value into other methods which take a collection and may not have a null check in them (something from CollectionUtils for instance).
 
Lionel Badiou
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all,

To add a note, I would consider the problem here as follow : Does that make sense that a given method returns nothing sometimes ? (search methods that return a reference first come in mind).

If yes, null may be an acceptable value for 'nothing' (an alternative may be a class constant).

Hope that helps,
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Lionel Badiou:
To add a note, I would consider the problem here as follow : Does that make sense that a given method returns nothing sometimes ? (search methods that return a reference first come in mind).


As Steven and James already pointed out, the drawback of returning null is that you have to specifically deal with it. Returning a value that the client simply can use as any other value can make code much more simple, where possible.

This is the Null Object pattern, by the way: http://c2.com/cgi/wiki?NullObject
 
Chengwei Lee
Ranch Hand
Posts: 884
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Steven and James already pointed out, the drawback of returning null is that you have to specifically deal with it. Returning a value that the client simply can use as any other value can make code much more simple, where possible.


Ok, I see the point now. No checking overhead is incurred for an empty collection.
 
Arun Prasath
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot guys..
It depends when to return null and when we shouldnt.
I would avoid my best not to return null and only return it when required.
Thanks for your replies.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!