# Code Smell 119 - Stairs Code

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

# Problems

- Declarativeness

- Ninja Code

- Readability

- [Arrow Code](https://maximilianocontieri.com/code-smell-102-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

[Gist Url]: # (https://gist.github.com/mcsee/b7125d33f30a8a37a40bc994fe7fcba6)
```python
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
```

## Right

[Gist Url]: # (https://gist.github.com/mcsee/b0afdb15577225b97f66381872f373f1)
```python
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
```

# 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](https://maximilianocontieri.com/how-to-get-rid-of-annoying-ifs-forever) which is also a [code smell](https://maximilianocontieri.com/code-smell-36-switchcaseelseifelseif-statements).

# Relations

%[https://maximilianocontieri.com/code-smell-118-return-false]

%[https://maximilianocontieri.com/code-smell-115-return-true]

%[https://maximilianocontieri.com/code-smell-101-comparison-against-booleans]

%[https://maximilianocontieri.com/code-smell-24-boolean-coercions]

%[https://maximilianocontieri.com/code-smell-62-flag-variables]

%[https://maximilianocontieri.com/code-smell-102-arrow-code]

%[https://maximilianocontieri.com/code-smell-80-nested-trycatch]

# More Info

- [How to Get Rid of Annoying Ifs Forever](https://maximilianocontieri.com/how-to-get-rid-of-annoying-ifs-forever)

# Credits

Photo by [Jukan Tateisi](https://unsplash.com/@tateisimikito) on [Unsplash](https://unsplash.com/s/photos/stairs)
    
Thanks again to Nico K. for this suggestion.

* * *

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

_Douglas McIlroy_
 
%[https://maximilianocontieri.com/software-engineering-great-quotes]

* * *

This article is part of the CodeSmell Series.

%[https://maximilianocontieri.com/how-to-find-the-stinky-parts-of-your-code]
