This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Exit methods with return Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Exit methods with return" Watch "Exit methods with return" New topic
Author

Exit methods with return

Mihai Radulescu
Ranch Hand

Joined: Sep 18, 2003
Posts: 916

I use return statement more than once (when this is necessary) like in the code :

boolean method (String in) {
boolean ret = false;
if(in == null) {
return ret;///here 1
}

...
do stuff
...

ret = true;
return ret; //here
}

Is this a good ?


SCJP, SCJD, SCWCD, OCPJBCD
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

Well personally I think a method should hav oly one return. That is completely my opinion.

In your example you can change your code as such




Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Baps Vakkalagadda
Ranch Hand

Joined: May 25, 2004
Posts: 47
A method can have more than one return statement but no two statements must be in the same block.

The below snippet is incorrect because both the return statements come under the same block of code. In this case, JVM complains saying "unreachable ...". But the code that Mihai wrote is valid.

Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

"But the code that Mihai wrote is valid."

Actually, his question isn't about the validity of doing what he is doing. It is about what is the best way to handle it.

Mark
Jon Entwistle
Ranch Hand

Joined: Feb 20, 2003
Posts: 118

That is completely my opinion.


Any many others . It is generaly considered that have anything but a single return statement at the very end of the method is, while perfectly correct, bad style as it makes the code more difficult to read and easier to misinterpret.


SCJD, SCEA
Mihai Radulescu
Ranch Hand

Joined: Sep 18, 2003
Posts: 916

I agree with you Jon, if the code has more exit points is more difficut to read and interpret, BUT , this is not always.

Most of the time I try to avoid this kind of "multiple return" but when the solution seams to get to complex,I just make it easy with an extra retun.

My question is still : Is this a valid technique (for the developer exam and general) ?
Jon Entwistle
Ranch Hand

Joined: Feb 20, 2003
Posts: 118
I would say that you really shouldn't - if you feel the need, reconsider your algorithm. Remember the three golden guidelines from the cover of 'The practice of programming' by Kernigan and Pike - 'Simplicity, Clarity, Generality' (the first two apply here)

Cheers,

Jon
[ May 27, 2004: Message edited by: Jon Entwistle ]
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

"when the solution seams to get to complex"

This is when I relook at my code and see how I can further break it apart into more methods, if the method is complex then it is probably doing more than one thing. If you try to keep it simple and take the approach of "A method should do one thing and one thing only, then you will find that you will have very little if never chances of needing more than one return.

It is clues like these that help in seeing where to refactor. Clues like needing more than one return.

Good Luck.

Mark
Mihai Radulescu
Ranch Hand

Joined: Sep 18, 2003
Posts: 916

Yes Mark, you are right, the best way is to break the "mammoth" methods in to small "mammoth" methods with (clear)specific task. I know about, I read about(all the develop. exam book mention this) but ....
TahnX.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

"but ...."

There are no butts er buts.

Have only one return, 98% of the time. and you are guaranteed not to lose points. If you have more than one return, you also might not lose points (85% chance*), but you also might lose points (15% chance*).

Mark

* chance percentages are based on a poll by Associated Press and CNN, there is a 15-85% margin of error.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Exit methods with return
 
Similar Threads
Sorting an array?
Problem with Hibernate Cache
Is it the right way?
using graphics in servlets
Basic Questions...Please help