Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

End a method

 
Nicholas Carrier
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's the question: Is there a way to get to the end of a method and skip everything like break in a loop.

For example

public String getAnswer() {
String answer = null;

If (something == somethingElse) {
answer = "Yo' Momma";
// what would go here to go straight to the return
}

// other stuff which would lead to a different answer
answer = "Yo' Sister"

return answer;

}


Is there no other way than using else? Basically, how do I stop a method from finishing and go straight to a return?
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Nicholas Carrier:
... Basically, how do I stop a method from finishing and go straight to a return?

By inserting another return...
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[Nicholas]: Basically, how do I stop a method from finishing and go straight to a return?

In fact, there is a statement that does exactly this. It's called... wait for it... a return statement. You can actually invoke return from anywhere in a method - assuming you don't accidentally make it totally impossible for the subsequent code to ever be executed, in which case the compiler will complain that you've got some unreachable code. Typically though you can easily put a return in an if clause, in which case subsequent code is accessible from the "else".

Using your example code, you could write:

Though in this case you could also eliminate the else, and write (equivalently)

or better yet

which goes back to avoiding the original question entirely. But hopefully the earlier examples answered it well enough.

Note that you may well encounter some people who feel that a return anywhere other than the end of a method is wrong as it makes code "hard to read". These same people generally say the same thing about break (other than in a switch) and continue. I am tempted to say you should ignore these people. But instead, I recommend you react by making a point of inserting as many returns, breaks, and continues as possible in your code, just to annoy those people. That's what I do, anyway.

No, seriously, I think you should consider whether there's a way to avoid the multiple returns and produce something that is more readable. (Which I feel I achieved above with the last code example, but that's a matter of opinion.) I think multiple returns are something you should be careful not to overuse, particularly in long methods. But sometimes, multiple returns may really be the best solution. (Same with continue and break.) But be prepared to change it to something more convoluted if your boss is intolerant of such things. I would also note that long methods are inherently evil, and if code is unreadable because it's hard to spot the multiple returns in your really long method, the problem isn't the multiple returns, it's the really long method. Break it up into several shorter methods first, and that will generally fix any other readability problem.

The previous two paragraphs are my own opinions, which some other people will doubtless disagree with. Be aware that whichever side you choose here, there will be other people who disagree; oh well. But as a technical matter, it's entirely possible to call return from the middle of a method.
[ November 02, 2006: Message edited by: Jim Yingst ]
 
Nicholas Carrier
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was actually coming here to delete the question

As always, thanks for the help and I'm going to try use a way to make it more readable as suggested.

<----feels dumb
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The discussion about multiple returns was worth having, so I'm glad you didn't delete the question.

Opinion: A method small enough that you can totally grok it in a glance is ok with a couple returns. A method that requires you to move your eyes up and down to read it can certainly get into trouble with multiple returns.

About half related to this topic ... you could look for ways to eliminate the if-else chain. Maybe ...

or make "something" an object with polymorphic behavior instead of just a String ...

Note how you can add new relationships and smart answers without touching this code. Does that sound like fun?
 
Henry Wong
author
Marshal
Pie
Posts: 21385
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note that you may well encounter some people who feel that a return anywhere other than the end of a method is wrong as it makes code "hard to read". These same people generally say the same thing about break (other than in a switch) and continue. I am tempted to say you should ignore these people. But instead, I recommend you react by making a point of inserting as many returns, breaks, and continues as possible in your code, just to annoy those people. That's what I do, anyway.


I am also one of those who uses multiple returns. But then again, my methods are generally small and highly documented. In many cases, the comments are longer than the code... I don't insert more returns to annoy "those people", but I do it for the ternary operator.

How could anyone not like the ternary operator? I actually enjoy annoying "those people". And have gotten really good at using them. Heck, once I wrote a method that was about a page long, with only a single return expression. (one big wad of ternary operators with ternary operators)

Henry
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Stan James:
The discussion about multiple returns was worth having, so I'm glad you didn't delete the question...

I agree. I also wouldn't feel "dumb" about asking this, because I think it's a reasonable question -- especially if you were in the mindframe of looking for a "goto" type solution.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[Stan]: Opinion: A method small enough that you can totally grok it in a glance is ok with a couple returns. A method that requires you to move your eyes up and down to read it can certainly get into trouble with multiple returns.

Agreed. I'm also more tolerant of extra returns very near the beginning of a method, e.g.

That's assuming I wouldn't prefer to throw some sort of exception at the beginning, which is often what I'd do instead. But, you know, depending on the circumstances...

[Henry]: How could anyone not like the ternary operator?

Agreed. Though I'm much less happy with nested ternaries. Unless you've got some trick for formatting them that makes it really clear to everyone what's going on. I will sometimes put together a few consecutive ternaries, equivalent to a series of if / else if / else if / else statements. Most commonly in a Comparator:

[ November 03, 2006: Message edited by: Jim Yingst ]
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I never would have made that up, but I might steal it.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, no problem. Just please include the following copyright statement in the code:

[ November 03, 2006: Message edited by: Jim Yingst ]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic