Code Smell 156 - Implicit Else
TL;DR: Be explicit. Even with Else.
Problems
Readability
Cognitive Load
Unforeseen conditions
Fail Fast Principle violation
Solutions
Write the explicit else
Context
If we early return on an IF sentence we can omit the else part.
Afterward,...
maximilianocontieri.com2 min read
Markos Korvesis
Software Engineer
Hey Maxi Contieri, good article!
I feel, however, that the wrong vs. right cases you provided are not comparable with each other. The "wrong" example is an if/else and the "right" one is an if/else-if/else.
Implicit "else" seems fine to me when the "if" body is surrounded by curly braces, if not then I'd prefer a ternary return assuming there would only ever be two cases.
From my point of view, the most important takeaway is the suggestion to give up on the if/else* block altogether and replace it with polymorphism.