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
Code Smell 102 - Arrow Code
Maxi Contieri ・ Nov 15 '21 ・ 2 min read
Code Smell 36 - Switch/case/elseif/else/if statements
Maxi Contieri ・ Nov 28 '20 ・ 1 min read
More Info
How to Get Rid of Annoying IFs Forever
Maxi Contieri ・ Nov 9 '20 ・ 5 min read
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
Software Engineering Great Quotes
Maxi Contieri ・ Dec 28 '20 ・ 13 min read
This article is part of the CodeSmell Series.
Top comments (4)
You really think throwing is preferable over an actual default case? From my point of view that second snippet is way more smellier than the first one. Having to wrap it on a
try/catchin order to be "safe" makes it usage way less convenient.My suggestion would be to just have something simple like this (added JSDocs to make DX even better):
And then the usage is as simple as:
As you can see, we have several options, none of which will make the app explode for not finding a result. Now with your approach we have less options and we need to use more boilerplate: