Reputation: 30868
I know that there are times when using return;
can serve a useful purpose in Java, such as in guarding:
public void foo(Bar bar) {
if(bar == null)
return;
// bar is not null, go ahead and do stuff with it
}
But what about just reaching the end of a method with return type void
? For example,
public void printMenu() {
System.out.println("Print out some boilerplate info here, line 1.");
System.out.println("Print out some boilerplate info here, line 2.");
System.out.println("Print out some boilerplate info here, line 3.");
return;
}
Other than pure style preferences, are there any reasons for or against including that return;
? If so, what are they?
EDIT: Well, that got answered quickly. To summarize the 15 answers posted below: "No."
Upvotes: 19
Views: 12864
Reputation: 10493
I think that unnecessary statements are just noise, so I wouldn't add that return in the end. That being said, I would add returns to the begin of the method if something doesn't satisfy my requirements or, even better, I would throw IllegalArgumentException exceptions.
Upvotes: 1
Reputation: 7504
One idea of structured programming was:
Every routine should have exactly one entry point and exactly one exit point.
If you subscribe to that policy, then the return
statement indicates the only way to exit the routine.
In practice, that policy does not make code clearer, and it has been mostly ignored since the 1970s. If you allow multiple return statements in other routines, then you should allow zero return statements where it makes most sense.
Upvotes: 1
Reputation: 41127
It doesn't serve any purpose but if you want to do it (because everyone in your team does it or for whatever reason), you can do it.
Upvotes: 0
Reputation: 5959
I would avoid it just from a consistency point. Its easier to never put it, than remembering to always add it.
Upvotes: 0
Reputation: 20895
I say don't ever do this. Return statements in void functions are only for the purpose of breaking out of the statement's logic. If you start doing this then you send a confusing statement to the readers of your code, one will be tempted to think that perhaps you planed having some if statement that you forgot. Always go for readability.
Upvotes: 2
Reputation: 101261
I think other than personal preference, there is no difference. The first case I think it's legitemate. Otherwise you should pack the statements in a big if-statement.
The last example the return is superfluous.
Upvotes: 0
Reputation: 11289
I see no reason, even from a style perspective, to have a dangling return at the end. After all, you know it's going to return because there's an end brace there...
Upvotes: 6
Reputation: 116197
I avoid them, myself. It's just a useless line of code. In fact, PMD has a rule that checks for such useless return
statements.
Upvotes: 12
Reputation: 2392
Purely style-based question, makes absolutely no difference (maybe an extra asm instruction, but who cares?). Do whichever you feel more comfortable with or follow the convention previously established in the code.
Upvotes: 1
Reputation: 772
In your example the 'return' at the end is a matter of personal, team or organization style. I personally prefer to do an explicit return.
Upvotes: 0
Reputation: 100821
Perhaps you're paid by line of code?
Other then that there's really no reason to put an empty return in the end.
Upvotes: 53