DEV Community

Cover image for ㄥ8Ɩ llǝɯS ǝpoƆ - spɹɐʍʞɔɐq ǝslƎ/ℲI
Maxi Contieri
Maxi Contieri

Posted on • Edited on • Originally published at maximilianocontieri.com

ㄥ8Ɩ llǝɯS ǝpoƆ - spɹɐʍʞɔɐq ǝslƎ/ℲI

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

  1. 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);
    }
}

Enter fullscreen mode Exit fullscreen mode

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);    
}
Enter fullscreen mode Exit fullscreen mode

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

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


This article is part of the CodeSmell Series.

Top comments (12)

Collapse
 
tracygjg profile image
Tracy Gilmore

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.

  • IMO that is what unit tests are for, to help developers refactor code with confidence.
  • Another factor is the lack of or a poor (possibly toxic) peer review process and lack of/poor coding standards/conventions that lets poor code enter and persist in the code base.
  • The continual pressure to deliver features at the cost of addressing increasing technical debt (because is does not deliver value to the end-user).

In short, the degradation of sound Software Engineering principles.

Collapse
 
mcsee profile image
Maxi Contieri

Rant accepted. we have more automated review tools than ever!

Collapse
 
tracygjg profile image
Tracy Gilmore

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.

Collapse
 
cicirello profile image
Vincent A. Cicirello

I like the post title for this one. Very creative.

Collapse
 
webjose profile image
José Pablo Ramírez Vargas

Ok, I must ask: How were you able to invert the orientation of the title?

Collapse
 
mcsee profile image
Maxi Contieri

It is just "upside down text" :)

Collapse
 
szabgab profile image
Gabor Szabo • Edited

I wonder what do you say about languages that allow the condition to come after the statement?

doSomething() if cartExists();
Enter fullscreen mode Exit fullscreen mode

and then what about

createCart() if ! cartExists();
Enter fullscreen mode Exit fullscreen mode

or

createCart() unless cartExists();
Enter fullscreen mode Exit fullscreen mode
Collapse
 
mcsee profile image
Maxi Contieri

nice and intersting.
unless is pretty clear

the other sintax is new to me. which language is?

Collapse
 
szabgab profile image
Gabor Szabo

Both Perl and Raku

Collapse
 
cicirello profile image
Vincent A. Cicirello

Python allows statements like this:

x = 100 if condition() else 50
Enter fullscreen mode Exit fullscreen mode

It is basically like the conditional ternary operator of Java, C, C++, but with condition in the middle.

Thread Thread
 
mcsee profile image
Maxi Contieri

nice and clear!

Thread Thread
 
cicirello profile image
Vincent A. Cicirello

I've seen instances of this used with backwards if..else like:

x = 50 if not condition() else 100
Enter fullscreen mode Exit fullscreen mode

So potential for same issues as normal if/else.