Question

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

Was it helpful?

Solution

Perhaps you're paid by line of code?

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

OTHER TIPS

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.

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

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.

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.

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.

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.

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.

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.

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

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top