Unnecessary 'else' after 'return'. (No-else-return)

While the rule correctly points out that the else block is unnecessary, and it is a style preference, I would add additional considerations for readability and most importantly scanability.

For the developer writing this code, and to the machine interpreting it, it may be a style point and that's it. But for the developer who needs to fix a bug, enhance a feature, do a code review, etc. the ability to quickly scan through the code and see the else blocks helps to identify branches of logic.

In a few lines of isolated code it is easy to see the intent, but among hundreds of lines of code having if else blocks can serve as useful identifiers, much like other common visual practices like indentation, line breaks, and naming conventions.


What that is basically saying is that the else part of the if statement is unnecessary if there is a return in the if part. Something like this is what it expects:

if (cctot <= 3 && cctot > 0) {
      alert('Credit under $3.00 not allowed');
      return Number.MIN_SAFE_INTEGER; // important to return 0 so we can check for these conditions for validation
}
cctot *= -1;

In general, this:

if (condition) {
  return something;
} else {
  // do another thing
}

return anotherThing;

is similar to:

if (condition) {
  return something;
}

// do another thing
return anotherThing;

After the if with a return statement, there is no need for the else part as the code below the if will only run when the condition stated is not fulfilled.


It's a code style preference. You don't need the else and instead can put the else code directly below the if. This is because if the if succeeds, that's the end of the function, so the else code will never be reached anyway.

So this:

if (condition) {
  return foo;
} else {
  // do bar
}

return baz

is equivalent to this:

if (condition) {
  return foo;
}

// do bar

return baz

This style seems to vary in different programming communities. Go developers will nearly always omit the else, while I've seen more JS devs include it.

While I prefer to leave off the else, it is again purely a matter of preference. Don't let it worry you too much. People may get dogmatic about this kind of thing, but it's really not that important.