If 'else' is about to happen anyway should

2020-08-11 04:07发布

Possible Duplicate:
Should ‘else’ be kept or dropped in cases where it’s not needed?

When

a = 0

This:

var foo = function() {
  if (a != 0) return true
  return false
}

Or this:

var bar = function() {
  if (a != 0) return true
  else return false
}

18条回答
Bombasti
2楼-- · 2020-08-11 04:27

I personally like the Syntax:

/*Function header comments*/
Function(...)
{ 
  /*English what the if is trying to achieve*/
  If(...)
  {
    ...
    Return True; /*What this tells me*/
  }
  Else
  {
    ...
    Return False: /*What this tells me*/
  }
}

Simply because I find myself in the situation if I simply do

If(...)
Return True;
Else
Return False;

or

If(...)
Return True;

Return False;

or

(...)?Return True:Return False;

That it's not as clear,even though with the right comment the one liner is cool, and if I'm doing a comparison anyways there is a good chance that down the road I may think of something that needs to happen in that area anyways, I mean I'm doing an if for more than grins. Also if I find a special case that needs an Else If its just a matter of adding in the Else If portions.

查看更多
劳资没心,怎么记你
3楼-- · 2020-08-11 04:29

If the language requires braces for if/else statements, I personally like to remove the else statement and the accompanying braces. The intention of the code will be just as clear, but the code will be less indented, which (usually) improves readability.

As others mentioned, the compiler will optimize the else statement. But if you're dealing with an interpreted language, the else statement has to be interpreted. In that case removing it will result in a minor (very minor) performance increase.

查看更多
对你真心纯属浪费
4楼-- · 2020-08-11 04:31

In this case just return the expression result directly.

return (a != 0)

But in general I try to avoid having returns from the middle of a function. Have one return per function.

查看更多
姐就是有狂的资本
5楼-- · 2020-08-11 04:32

It doesn't matter. The compiler will turn out the same code either way. Check to see what convention existing code follows, and then just do the same.

Personally I don't put the "else" because it's obvious. I find that the extra "else" looks cluttered.

查看更多
爱情/是我丢掉的垃圾
6楼-- · 2020-08-11 04:34
if(statement)
       result = true
else
       result = false

return result
查看更多
闹够了就滚
7楼-- · 2020-08-11 04:37

Of course "else" should be declared explicitly.

There are several reasons why 'else' should be declared explicitly :

  1. Readability
    I won't sacrifice readability for some "cool" programming style. But, i will understand if you're facing bandwith-oriented problem like minifying javascript.
  2. Conceptual. Your first code doesn't tell the human reader "what will you do if the 'if guard' returns false". Instead, your code tells the reader that "default value is false, true only happens when bla-bla-bla". Or in another word, your code tells the reader that "I assume the value is false, it's true only when bla-bla-bla".

On the conceptual reason above, it - of course - depends on your function specification. For some function it makes sense to use such code. For example "Function assumes the visitor is not registered, he is only registered only if bla-bla-bla".

But for another problem like "If a number is divisible by two then it is even otherwise odd", you should write it explicitly so that a reader (perhaps not a programmer) will not be confused when reading your code. Since the reader (maybe a mathematician) only knows such invariant and he/she only knows a little about programming language.

查看更多
登录 后发表回答