The first thing we read after the if the condition is the IF
TL;DR: You have the important else condition on the else.
Problems
- Readability
Solutions
- Swap the conditions.
Context
It is not as straightforward as it appears to write IF clauses in an elegant manner.
There are lots of variants and choices.
We need to pay special attention to readability.
Sample Code
Wrong
fun addToCart(item: Any) {
if (!cartExists()) {
// Condition is negated
this.createCart();
this.cart.addItem(Item);
// Repeated Code
}
else {
// Normal case is on the else clause
this.cart.addItem(Item);
}
}
Right
fun addToCart(item: Any) {
if (cartExists()) {
this.cart.addItem(Item);
}
else {
this.createCart();
this.cart.addItem(Item);
}
}
fun addToCartShorter(item: Any) {
if (!cartExists()) {
this.createCart();
}
this.cart.addItem(Item);
}
Detection
[X] Semi-Automatic
We can find negated expressions on IF conditions and check for this anti-pattern.
Tags
- IFs
Conclusion
We need to read code like prose.
Humans read the standard case first and the exceptional one after it.
Relations
Code Smell 156 - Implicit Else
Maxi Contieri ・ Aug 6 '22
More Info
Disclaimer
Code Smells are just my opinion.
Credits
Photo by Karol Kasanicky on Unsplash
Beauty is more important in computing than anywhere else in technology because software is so complicated. Beauty is the ultimate defense against complexity.
D. Gelernter
Software Engineering Great Quotes
Maxi Contieri ・ Dec 28 '20
This article is part of the CodeSmell Series.
Top comments (12)
Hi Maxi,
RANT WARNING
I see more of this type of code in reviews since the adoption of agile practices. I think this is down to a combination of factors:
Inexperienced developers changing pre-existing code and understandably fearful of introducing a bug by changing more than they need to in order to get the job done.
In short, the degradation of sound Software Engineering principles.
Rant accepted. we have more automated review tools than ever!
Whilst I agree linting tools and prettifies are very useful they cannot replace the human peer-review. Linters and prettifies cannot draw on personal experience (lessons learned) to advise and instruct junior team members.
One of the downsides of remote working, which I am finding is particularly prevalent in the software industry (in the UK), is the reduced team interaction that can result and the impact this has on bringing on junior team members. This is why I personally think peer-programming is more important than ever.
I like the post title for this one. Very creative.
Ok, I must ask: How were you able to invert the orientation of the title?
It is just "upside down text" :)
I wonder what do you say about languages that allow the condition to come after the statement?
and then what about
or
nice and intersting.
unless is pretty clear
the other sintax is new to me. which language is?
Both Perl and Raku
Python allows statements like this:
It is basically like the conditional ternary operator of Java, C, C++, but with condition in the middle.
nice and clear!
I've seen instances of this used with backwards if..else like:
So potential for same issues as normal if/else.