Unnecessary 'else' statement

Posted by Vitalii Fedorenko on Stack Overflow See other posts from Stack Overflow or by Vitalii Fedorenko
Published on 2010-04-21T10:00:56Z Indexed on 2010/04/21 10:03 UTC
Read the original article Hit count: 248

Filed under:
|
|

As you know, in Eclipse you can turn on "Unnecessary 'else' statement" check that will trigger on if-then-else with premature return. And, from my experience, there are two most possible situations when use such statement:

1) Pre-check:

if (validate(arg1)) {
    return false;
}
doLotOfStuff();

2) Post-check:

doLotOfStuff();
if (condition) { 
    return foo; 
} else {
    return bar; 
}

In the second case, if the trigger is on, Eclipse will suggest you to change the code to:

doLotOfStuff();
if (condition) { 
    return foo; 
} 
return bar; 

However, I think that the return with else statement is more readable as it is like direct mapping of business logic. So I am curios if this "Unnecessary 'else' statement" code convention is widespread or else statement is more preferable?

© Stack Overflow or respective owner

Related posts about code-formatting

Related posts about eclipse