Code Smell 156 - Implicit Else
We learn if/else on our first programming day. Then we forget the else

TL;DR: Be explicit. Even with Else.
Problems
Readability
Cognitive Load
Unforeseen conditions
Solutions
- Write the explicit else
Context
If we early return on an IF sentence we can omit the else part.
Afterward, we Remove the IF and use polymorphism.
That is when we miss the real cases.
Sample Code
Wrong
function carBrandImplicit(model) {
if (model === 'A4') {
return 'audi';
}
return 'Mercedes-Benz';
}
Right
function carBrandExplicit(model) {
if (model === 'A4') {
return 'audi';
}
if (model === 'AMG') {
return 'Mercedes-Benz';
}
// Fail Fast
throw new Exception('Model not found);
}
Detection
[X] Automatic
We can check syntax trees and parse them and warn for missing else.
We can also rewrite them and perform mutation testing.
Tags
- Conditionals
Conclusion
This kind of smell brings a lot of public debate, and hate.
We must exchange opinions and value each pros and cons.
Relations
More Info
Credits
Photo by Elena Mozhvilo on Unsplash
The biggest issue on software teams is making sure everyone understands what everyone else is doing.
Martin Fowler
This article is part of the CodeSmell Series.
About the author
I’m a senior software engineer loving clean code, and declarative designs. S.O.L.I.D. and agile methodologies fan.
Comments
Join the discussionHey 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.
Code Smells
In this series, we will see several symptoms and situations that make us doubt the quality of our developments. We will present possible solutions. Most are just clues. They are no hard rules.





