views:

366

answers:

11

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."

+25  A: 

Perhaps you're paid by line of code?

Other then that there's really no reason to put an empty return in the end.

ChssPly76
What about inside an if block? Perhaps I have a recursive function and I want my exit conditions at the top of my function?
Zoidberg
That would not be "in the end" now, would it? :-) Using `return;` for `void` function is perfectly acceptable as a way to avoid 1000 nested ifs - OP has demonstrated that in his first example. The question was about using `return` just prior to closing brace which is utterly pointless.
ChssPly76
Accepted for being funnier than the other equally correct answers.
Lord Torgamus
A: 

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.

+1  A: 

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.

John Scipione
If I recall correctly there will be an implicit return instruction there whether it is typed or not.
Jim Ford
+7  A: 

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.

Thomas Owens
+3  A: 

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...

ZoogieZork
A: 

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.

Ikke
+1  A: 

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.

Lars Tackmann
A: 

I would avoid it just from a consistency point. Its easier to never put it, than remembering to always add it.

ccook
A: 

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.

fastcodejava
A: 

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.

Chip Uni
A: 

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.

Ravi Wallau