3 ms·
What about `else if`? Not a fan?
by theteapot 3y ago
What about `else if`? Not a fan?
- quickthrower2 3y agoOccasional vice for me.
- stouset 3y agoPersonally I only either use if statements that wrap a single line of logic, or a switch/case statement where each branch is a single line of logic. Most situations are handled by guard clauses, exhaustive type switching, or switching on logical cases. Very occasionally do I have to break this practice for a legitimate reason, but this keeps functions simple to read and to reason about. I occasionally am unable to avoid an else, but I will always avoid else if. It puts too much reliance on remembering how this condition interacts with the one on the if statement above it, which can often be many lines away. I’d infinitely rather use a case statement where all the alternatives are in an easily-understood table with every condition aligned on successive lines.