iTranslated by AI
On the Use of `if`-`else` vs. `if`-`if` Structures
Introduction
A while ago, the following post on X became a hot topic.
The content of the post itself is nothing particularly remarkable (the fact that someone who isn't a professional programmer finds the interpretation of 'else' troublesome is understandable), but the fact that many of the reactions were along the lines of "the former is definitely better" felt interesting to me. I thought it was an intriguing topic, so I decided to write down my own thoughts.
Premises
First, when comparing the former and the latter, I will not consider speed differences. Given modern compiler optimizations and the ability of processors to extract parallelism, there is almost no point in trying to conclude which is faster by looking at this level of code; I believe it is an issue that should be discussed by looking at the actual generated binary or profiler results.
Similarly, I will not consider side effects associated with interrupts or conditional evaluation. Whether these elements should be considered depends on the language, and if it is a language where they must be considered, I believe it would be possible to write code that accounts for them in either case.
I will also exclude discussions about variable naming or rules like "you should wrap even a single statement in {}." This is purely a discussion about if else versus if if as a control structure.
if else vs if if
When I think about the cases where I would want to write this code with if else, it is when count++ and life-- are fundamentally mutually exclusive. From this snippet, it is unclear what count or life represent, but if you can state definitively that "if count increases, life must decrease," then I think if else is fine.
However, if there is a relationship that must always be satisfied between count and life like that, perhaps it should instead be written like this:
if (isSuccess) {
count++;
}
life = max_life - count;
On the other hand, if you cannot make such a definitive statement and the conditions being exclusive is just a coincidence of the current situation, then if if might be appropriate. For example, consider a case where a special state arises in the future where neither statement is executed, as shown below:
if (isSuccess && !isHidden) count++;
if (!isSuccess && !isHidden) life--;
Looking at it this way, it seems better to give each condition an appropriate name and use if if:
countUp = isSuccess && !isHidden;
lifeDown = !isSuccess && !isHidden;
if (countUp) count++;
if (lifeDown) life--;
Conclusion
As such, I might write either, and if anything, I am more likely to write the latter. When considering the structure of code, I often ask myself, "Is this fundamentally the case?" For example, if a code snippet appears repeatedly in several places, grouping them unconditionally into the same function might cause trouble if you want to make them separate processes in the future. In such cases, I find that thinking about "Are these fundamentally the same process? Or is it just a coincidence that they behave the same way under the current specification?" can make future extensions a little easier.
Discussion