DEV Community

Cover image for Code Smell 119 - Stairs Code
Maxi Contieri
Maxi Contieri

Posted on • Originally published at maximilianocontieri.com

Code Smell 119 - Stairs Code

Nested boolean conditions express a business rule. Not an IF

TL;DR: Avoid checking for boolean expressions and returning an explicit boolean.

Problems

  • Declarativeness

  • Ninja Code

  • Readability

  • Arrow Code

Solutions

  1. Return a boolean business formula value.

Context

When dealing with boolean formulas, it is more readable to show a business boolean formula than a stair of boolean checks followed by returning an explicit true/false;

Sample Code

Wrong

def is_platypus(self):
    if self.is_mammal():
        if self.has_fur():
            if self.has_beak():
                if self.has_tail():
                    if self.can_swim():
                        return True
    return False

# This is also wrong since it is polluted with IFs and not readable by a biologist
def is_platypus(self):
    if not self.is_mammal():
        return False
    if not self.has_fur():
        return False
    if not self.has_beak():
        return False
    if not self.has_tail():
        return False
    if not self.can_swim():
        return False 
    return True
Enter fullscreen mode Exit fullscreen mode

Right

def is_platypus(self):
    return self.is_mammal() &&  self.has_fur() && self.has_beak() && self.has_tail() && self.can_swim()

#We can even group conditions according to animal taxonomies
Enter fullscreen mode Exit fullscreen mode

Detection

[X] Automatic

Based on syntax trees, we can safely refactor the code removing the explicit boolean value.

Tags

  • Boolean

Conclusion

Beware of returning booleans.

After the return, you will need an If statement which is also a code smell.

Relations

More Info

Credits

Photo by Jukan Tateisi on Unsplash

Thanks again to Nico K. for this suggestion.


The real hero of programming is the one who writes negative code.

Douglas McIlroy


This article is part of the CodeSmell Series.

AWS Q Developer image

Build your favorite retro game with Amazon Q Developer CLI in the Challenge & win a T-shirt!

Feeling nostalgic? Build Games Challenge is your chance to recreate your favorite retro arcade style game using Amazon Q Developer’s agentic coding experience in the command line interface, Q Developer CLI.

Participate Now

Top comments (0)

Redis image

Short-term memory for faster
AI agents

AI agents struggle with latency and context switching. Redis fixes it with a fast, in-memory layer for short-term context—plus native support for vectors and semi-structured data to keep real-time workflows on track.

Start building

👋 Kindness is contagious

Explore this insightful write-up, celebrated by our thriving DEV Community. Developers everywhere are invited to contribute and elevate our shared expertise.

A simple "thank you" can brighten someone’s day—leave your appreciation in the comments!

On DEV, knowledge-sharing fuels our progress and strengthens our community ties. Found this useful? A quick thank you to the author makes all the difference.

Okay