Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unnecessary 'else' statement [duplicate]

Tags:

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 code with else statement is more preferable?

like image 337
Vitalii Fedorenko Avatar asked Apr 21 '10 10:04

Vitalii Fedorenko


People also ask

Is else unnecessary?

No, It's not required to write the else part for the if statement. In fact most of the developers prefer and recommend to avoid the else block.

Can you omit an else statement in Java?

Answer 526897a4abf821c5f4002967If you require code to run only when the statement returns true (and do nothing else if false) then an else statement is not needed. On the other hand if you need a code to execute “A” when true and “B” when false, then you can use the if / else statement.

What is Else statement used for?

In programming languages, an else statement is an alternative statement that is executed if the result of a previous test condition evaluates to false.


2 Answers

Generally I would prefer the structure of the code to follow the structure of the underlying "business" logic. In this case, my approach would depend what condition represents. If it is an error check, for example, which won't normally be hit but might occasionally be used, then the asymmetry of the second form matches the asymmetry of the logic.

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

But if either possibility is reasonable and it's simply a choice between them, I would allow the structure of the code to show that symmetry.

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

The code is there for the programmer to read, not the compiler.

like image 76
Andy Mortimer Avatar answered Sep 19 '22 08:09

Andy Mortimer


It was once considered (and probably still be by some) that functions should have one entry point (easy but was relevant when you consider assembly language) and one exit point.

One exit point is nice from a debugging standpoint (as in you can put one watch/break on one line and know you'll go through it) but can lead to some horrific nesting so more often than not readability tends to win out. Which produces the least nesting, the least lines of code and the most readable end result? Ultimately that tends to be far more important than anything else.

For what it's worth the last can be better expressed as:

return condition ? foo : bar;

assuming condition isn't terribly long.

Don't get overly concerned with supposed code "purity". It's an irrelevant distraction. Make things readable and generally be consistent.

like image 28
cletus Avatar answered Sep 22 '22 08:09

cletus