Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I am using es-lint to clean up the errors in my code. I have come across this error:

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

} else { 

I have always used else statements after a return. Is there something I may be overlooking?

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 } else {     cctot *= -1; } return precise(cctot); 
like image 330
David Brierton Avatar asked Oct 22 '17 14:10

David Brierton


People also ask

Do not use else after return else if?

At the time of writing this, the Mozilla Coding Style guidelines have this recommendation under “General C/C++ Practices”: Don't put an else right after a return. Delete the else, it's unnecessary and increases indentation level.

What happen when we omit the else block in Python?

It's just saying that there's no need for 'else' since the execution of the function already stopped and if the 'if' condition doesn't succeed, it will still run any code underneath it.

What can I use instead of return in Javascript?

To put it simply a continuation is a function which is used in place of a return statement. More formally: A continuation is a function which is called by another function. The last thing the other function does is call the continuation.

Can I use if without else Javascript?

You don't need an else statement, you can use ng-if . The ng-if statement is also available without an else condition.


2 Answers

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.

like image 85
codejockie Avatar answered Oct 13 '22 11:10

codejockie


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.

like image 29
llama Avatar answered Oct 13 '22 10:10

llama