• Post Reply Bookmark Topic Watch Topic
  • New Topic

To return null or not to return null, that is the question  RSS feed

 
Ben Synes
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have seen in some cases, some methods where it may execute a for() and return the output from this loop, or from a if condition, but when you specify a return type in the signature, you obviously have to return something. I was at work and I noticed one guy, always adds a return null, presumably he never expects the return to ever be reached and hence correctly set from the contiional above, but my question is, is this good practice.

And if used, should it not be handled?

I can see some issue I would imagine with null pointer should the conditional/loop be prematurely exited.

 
Roger Sterling
Ranch Hand
Posts: 426
Eclipse IDE Fedora Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This probably is not good practice. you should study finally clause. Returning null from inside a for loop usually means the developer has not properly released his/her variables. This may have little impact in JRE 5, but in JREs after 5 (6, 7, 8) it usually means that memory will never get cleaned up, especially if the static key word is used anywhere in that class definition.


So the short answer is, break. http://docs.oracle.com/javase/tutorial/java/nutsandbolts/branch.html
 
Ben Synes
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe slightly misunderstood, let me demonstrate:


A simple example, of course you could return a String "not found", but Im thinking in terms of bespoke objects, and not necessarily a search for a match.
 
Roger Sterling
Ranch Hand
Posts: 426
Eclipse IDE Fedora Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Still same problem.

break; is the prescribed way to exit a for loop.

Programming practice means you do it the same way each time. In some instances, it makes little difference, but its the rigor that you want to enforce at that point.
 
Ben Synes
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you use break within an if? I thought not. If in the case of a conditional, then what to do?
 
Roger Sterling
Ranch Hand
Posts: 426
Eclipse IDE Fedora Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ben Synes wrote:Can you use break within an if? I thought not. If in the case of a conditional, then what to do?


If you will kindly read the article I referenced, you will answer your own question. ShowSomeEffort
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually returning null is fine if, and only if, it is a defined part of the contract for that method and callers expect that under certain circumstances that null could be returned.

But it almost sounds like your colleague is returning it by rote, which probably not a good design.

So. as with many other things, the answer is: it depends.
 
Ben Synes
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I appreciate your help, I will most certainly read the post. I made a stack of effort since last year, passing the SCJP and now studying for the WCD. Hope you won't be so hard on me next time.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An alternative to using null is the Optional class from the Google Guava library.
It doesn't force the caller to check for the empty case, but it gives them a stronger hint.
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And if you've already upgraded to Java 8, it's available as java.util.Optional.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Spoor wrote:And if you've already upgraded to Java 8, it's available as java.util.Optional.

Cool. I haven't had a chance to look at Java 8 in much detail yet.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!