This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Can we have two return statement in a method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Can we have two return statement in a method" Watch "Can we have two return statement in a method" New topic
Author

Can we have two return statement in a method

Mintoo kumar
Ranch Hand

Joined: Aug 21, 2007
Posts: 61
Hi,

I have a method which have return boolean based on condition ,so i have two retrun statement in a method.I just wanted to know , is it wrong to have two return statment in a method. method is as follow as ..
___________________________________________________________

private boolean isInvalid(String value) {
String sVal[] = { "000000000", "111111111", "222222222", "333333333" };
for (int index = 0; index < sVal.length; index++) {
if (sVal== null || sVal[index].equals(value) ){
return true;
}
}
return false;
}// end of method
______________________________________________________________
Is the above method wrong ,because of it is having two return statemtnts.

Thanks in advance.
rakesh sugirtharaj
Ranch Hand

Joined: Dec 16, 2007
Posts: 151
I dont think it is wrong. I do this all the time. But i am not sure if there is any best practice regarding this.


Cheers!
RSR
Satya Maheshwari
Ranch Hand

Joined: Jan 01, 2007
Posts: 368
I think it is always better to have a single exit from a method, though it may involve a bit of extra coding.


Thanks and Regards
Ove Lindström
Ranch Hand

Joined: Mar 10, 2008
Posts: 326

Originally posted by Satya Maheshwari:
I think it is always better to have a single exit from a method, though it may involve a bit of extra coding.


I disagree. Effective coding is to do what you want to do and nothing else. If I ask you to go fetch a mug from the kitchen, I want you to get back to me as soon as you have found it. I don't care that you can also search for plates and other kitchen stuff.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Satya Maheshwari:
I think it is always better to have a single exit from a method, though it may involve a bit of extra coding.


Why?


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Satya Maheshwari
Ranch Hand

Joined: Jan 01, 2007
Posts: 368
I disagree. Effective coding is to do what you want to do and nothing else. If I ask you to go fetch a mug from the kitchen, I want you to get back to me as soon as you have found it. I don't care that you can also search for plates and other kitchen stuff.


This is an old debate and better not to have it again . Its is more of a matter of one's coding style and preference.
Below are some links which have both pros and cons for it.

http://whatimean.wordpress.com/2007/02/08/multiple-return-points-are-bad/
http://leepoint.net/JavaBasics/methods/method-commentary/methcom-30-multiple-return.html
http://onthethought.blogspot.com/2004/12/multiple-return-statements.html
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Satya Maheshwari:

This is an old debate and better not to have it again . Its is more of a matter of one's coding style and preference.


I agree. It's just that the coding style allowing multiple returns is the only right one...
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11153
    
  16

The right way is whatever your company's coding style says. period.

if you are writing code for yourself, the best way is whatever is clearest. remember, you're going to come back to this code in a month or a year, and you want to be able to understand what you did.

Ultimately, this is as much a religious war as the vi vs. emacs debate, or the "where should i put my curly braces?" debate. ask 3 people, and you'll get 5 answers.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Bob Ruth
Ranch Hand

Joined: Jun 04, 2007
Posts: 320
I think a lot depends on the code you are looking at too.
In OO land, methods may not be gigantic in nature, but sometimes they might. When you go to debug a method that has one entry..... and 12 exits spread over 9 pages of code, sometimes you wish that you had one exit point that you had to monitor for a particular value rather than each and every one individually. a) it is a lot of repetetive coding to add, and b) you might miss one or discount the possibility of that path and be mistaken.


------------------------
Bob
SCJP - 86% - June 11, 2009
Jimmy Clark
Ranch Hand

Joined: Apr 16, 2008
Posts: 2187
The following is neater and the "best"


[ October 29, 2008: Message edited by: James Clark ]
Taariq San
Ranch Hand

Joined: Nov 20, 2007
Posts: 192
Originally posted by James Clark:
The following is neater and the "best"






Slight change and its now the best.

They're much the same, and not worth a debate. Do what the company style guide says.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Bob Ruth:
I think a lot depends on the code you are looking at too.
In OO land, methods may not be gigantic in nature, but sometimes they might. When you go to debug a method that has one entry..... and 12 exits spread over 9 pages of code, sometimes you wish that you had one exit point that you had to monitor for a particular value rather than each and every one individually. a) it is a lot of repetetive coding to add, and b) you might miss one or discount the possibility of that path and be mistaken.


Some observations:

1) A gigantic method, wether in OO code or not, should be broken down into smaller methods. There really is no good reason for gigantic (or even barely big) methods at all.

2) Methods with multiple exit points can be harder to break down into smaller methods.

3) Modern IDEs, like Eclipse, can highlight all exit points of a method for you. There is no reason to miss one.
Matteo Di Furia
Ranch Hand

Joined: Jun 20, 2008
Posts: 102
Originally posted by Taariq San:



Slight change and its now the best.

They're much the same, and not worth a debate. Do what the company style guide says.


Ehy, isn't this exactly the same code posted by Mintoo kumar ?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37943
    
  22
Originally posted by Matteo Di Furia:


Ehy, isn't this exactly the same code posted by Mintoo kumar ?
Not quite. It is testing "00" rather than "0000000000".

But whether that is the "best?" There are a lot of people who say that any boolean method with a "negative" return type is "less-than-best." They would say it ought to read "public boolean isValid(Foo foo)" and return true for a valid input.

I am not absolutely sure, but I think they are correct.
If you are iterating through an array like that, you can add the boolean value to the test. Then you only get one return statement If you find one valid input, there is no point in checking the remainder of the array, so the !valid bit is false and the for-loop terminates.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by James Clark:
The following is neater and the "best"



Well, the null check is absolutely unnecessary...

And I seriously don't see why a break is better than a return. As far as I can tell, it has exactly the same drawbacks.
Jimmy Clark
Ranch Hand

Joined: Apr 16, 2008
Posts: 2187

And I seriously don't see why a break is better than a return. As far as I can tell, it has exactly the same drawbacks.


It's mostly a style aspect. What is "best" for some may not be "best" for others. If the example method was bigger with many more statements and complexity, then having a single statement that returns control is much better, in my opinion. Especially, if I am not the author of the code.

Five to ten page methods with multiple return statements can be difficult to read/debug.
[ October 29, 2008: Message edited by: James Clark ]
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 2986
    
    9
[Matteo]: Ehy, isn't this exactly the same code posted by Mintoo kumar ?

Aside from what Campbell noted, it's also posted with code tags this time, which makes it much, much better.

[James]: Five to ten page methods with multiple return statements can be difficult to read/debug.

Sure. Which is why programmers who write five to ten page methods need to be shot.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by James Clark:

It's mostly a style aspect. What is "best" for some may not be "best" for others. If the example method was bigger with many more statements and complexity, then having a single statement that returns control is much better, in my opinion. Especially, if I am not the author of the code.

Five to ten page methods with multiple return statements can be difficult to read/debug.


Methods above a dozen of lines are difficult to read. Don't write them. If you encounter them, break them down. Then use multiple return statements where they make sense. Nothing more to say.
Mintoo kumar
Ranch Hand

Joined: Aug 21, 2007
Posts: 61
Thanks to All.
Jimmy Clark
Ranch Hand

Joined: Apr 16, 2008
Posts: 2187
Sure. Which is why programmers who write five to ten page methods need to be shot.


Hah, hah. Very true.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can we have two return statement in a method
 
Similar Threads
stuck on null pointer
Count Words
which has more priority: || and &&
Simple precedence question.
Getting values back from an array