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

⭐ Football Update: mcsee/engi llws poo sprawkoaq sli ei - Jadwal Siaran Langsung ⭐ Football Update: mcsee/engi llws poo sprawkoaq sli ei - Jadwal Siaran Langsung

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);    
}
⭐ Football Update: mcsee/engi llws poo sprawkoaq sli ei - Jadwal Siaran Langsung ⭐ Football Update: mcsee/engi llws poo sprawkoaq sli ei - Jadwal Siaran Langsung

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)

⭐ Football Update: mcsee/engi llws poo sprawkoaq sli ei - Jadwal Siaran Langsung
 
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.

⭐ Football Update: mcsee/engi llws poo sprawkoaq sli ei - Jadwal Siaran Langsung
 
mcsee profile image
Maxi Contieri •

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

⭐ Football Update: mcsee/engi llws poo sprawkoaq sli ei - Jadwal Siaran Langsung
 
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.

⭐ Football Update: mcsee/engi llws poo sprawkoaq sli ei - Jadwal Siaran Langsung
 
cicirello profile image
Vincent A. Cicirello •

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

⭐ Football Update: mcsee/engi llws poo sprawkoaq sli ei - Jadwal Siaran Langsung
 
webjose profile image
José Pablo Ramírez Vargas •

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

⭐ Football Update: mcsee/engi llws poo sprawkoaq sli ei - Jadwal Siaran Langsung
 
mcsee profile image
Maxi Contieri •

It is just "upside down text" :)

⭐ Football Update: mcsee/engi llws poo sprawkoaq sli ei - Jadwal Siaran Langsung
 
szabgab profile image
Gábor Szabó • • Edited

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

doSomething() if cartExists();
⭐ Football Update: mcsee/engi llws poo sprawkoaq sli ei - Jadwal Siaran Langsung ⭐ Football Update: mcsee/engi llws poo sprawkoaq sli ei - Jadwal Siaran Langsung

and then what about

createCart() if ! cartExists();
⭐ Football Update: mcsee/engi llws poo sprawkoaq sli ei - Jadwal Siaran Langsung ⭐ Football Update: mcsee/engi llws poo sprawkoaq sli ei - Jadwal Siaran Langsung

or

createCart() unless cartExists();
⭐ Football Update: mcsee/engi llws poo sprawkoaq sli ei - Jadwal Siaran Langsung ⭐ Football Update: mcsee/engi llws poo sprawkoaq sli ei - Jadwal Siaran Langsung
⭐ Football Update: mcsee/engi llws poo sprawkoaq sli ei - Jadwal Siaran Langsung
 
mcsee profile image
Maxi Contieri •

nice and intersting.
unless is pretty clear

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

⭐ Football Update: mcsee/engi llws poo sprawkoaq sli ei - Jadwal Siaran Langsung
 
szabgab profile image
Gábor Szabó •

Both Perl and Raku

⭐ Football Update: mcsee/engi llws poo sprawkoaq sli ei - Jadwal Siaran Langsung
 
cicirello profile image
Vincent A. Cicirello •

Python allows statements like this:

x = 100 if condition() else 50
⭐ Football Update: mcsee/engi llws poo sprawkoaq sli ei - Jadwal Siaran Langsung ⭐ Football Update: mcsee/engi llws poo sprawkoaq sli ei - Jadwal Siaran Langsung

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

⭐ Football Update: mcsee/engi llws poo sprawkoaq sli ei - Jadwal Siaran Langsung Thread
 
mcsee profile image
Maxi Contieri •

nice and clear!

⭐ Football Update: mcsee/engi llws poo sprawkoaq sli ei - Jadwal Siaran Langsung 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
⭐ Football Update: mcsee/engi llws poo sprawkoaq sli ei - Jadwal Siaran Langsung ⭐ Football Update: mcsee/engi llws poo sprawkoaq sli ei - Jadwal Siaran Langsung

So potential for same issues as normal if/else.