Semantics
Programming languages often have different constructs to give different semantic effects. You rarely need a different build if you do not want this result.
Simplicity
You should always try to use as simple as possible. I think the simplest is if
with simple block (just a command), without extra conditionals or against conditional.
How it works block if
But there are cases where this is not possible. When you have a situation where one block of action is mutually exclusive, you must use else
. Well, you do not really need to. You can put a second if
next with the inverted condition, but this does not make much sense, it makes reading and maintenance worse.
This also applies when you have a series of mutually exclusive options, that is, only one is valid. You can even make multiple% s of% s, but if the decision is interconnected, if it is more logical that it be done as a single operation it is better to choose it.
What's best
So yes, in your example you should make choosing if
primarily because of semantics. But this is not something silly that only serves a rule. It really makes it easy to read and tweak your code without causing problems in the future.
Codes should be written primarily for humans to read. So it is very helpful to give the correct understanding of what you are doing there. When all the choices lead to the same place choose the simpler . We can adapt this statement for the purpose of maintenance: if all forms solve the problem, choose the one that best expresses your intention.
Making% of separate% s that are logically connected passes the wrong idea. My understanding is that your code is making a single logical decision. Even though it is simpler to use else if
simple, it is not easier to read such code.
Beyond the question of if
But there is another problem. I would not say your two codes do the same thing. They are not equivalent. Okay, they're even the way it's written. But no one writes code like that except for example.
var bola = "redonda";
if (bola == "comestivel") return comer();
if (bola == "liquida") return beber();
if (bola == "redonda") chutar();
and
var bola = "redonda";
if (bola == "comestivel") return comer();
else if (bola == "liquida") return beber();
else if (bola == "redonda") chutar();
are equivalent.
I placed GitHub for future reference .
In a real code there could be a situation where the variable if
is not worth any of these texts. Your code will execute if
if the text is different from the two initial options in the second code, but will not execute in the first if the first situation is the same. In the above code, both will do the same thing.
Notice the response from bfavaretto that shows that there may be another problem when you choose the option of bola
s insulated. In your example it does not cause a problem, but when you can have two conditions being met and you want only one block to be executed (are mutually exclusive, or executes one or the other), separating chutar()
will give unexpected result, it can execute the two blocks and it's not what you want.
Conclusion
So finding the simplest is tricky. It is common for less experienced programmers to do extremely complicated things because they do not know how to do simple. It seems a contradiction but it is what happens the most. The simplest in these cases is the structure that seems more complicated (not that much).
I think there is still another problem in the code that does not seem so relevant to the question (but it may be) and I'm only guessing since this is not a real code. In the third case you are running the if
function but it is not returning the result of this function. In the previous conditions you are returning the result of the function. It may not have worked. That would make no difference.
Even though I would still put if
at the end unless you have a reason not to. It's a question of symmetry. If the intention is to terminate the function there, explicitly state that you will do this. If the intention clearly does not need to return a value in any case, you should then take out all chutar()
to make it clear that you are not returning a value. Or at least call the function and only then use return chutar()
.
Finally I'd rather give a space between return
and return
of the condition to not confuse with a function called if
.