# Code Smell 199 - Gratuitous Booleans

> TL;DR: Check carefully your boolean expressions

# Problems

- Readability

- Possible Defects

# Solutions

1. Refactor and remove obsolete code

# Context

When a function is designed to return an invariant value, it may be poor design, but it shouldn’t adversely affect the outcome of your program. However, when it happens on all paths through the logic, it is likely a mistake.

This rule raises an issue when a function contains several return statements that all return the same value.

# Sample Code

## Wrong

[Gist Url]: # (https://gist.github.com/mcsee/dc73985cd7ff45aa7496f41f96e81fe3)
```python
# Gratuitous boolean expressions

if a > 0 and True:
    print("a is positive")
else:
    print("a is not positive")

```

## Right

[Gist Url]: # (https://gist.github.com/mcsee/c8d0be601736520faa86a8d7e781be2f)
```python
if a > 0:
    print("a is positive")
else:
    print("a is not positive")
```

# Detection

[X] Automatic 

Many [linters](https://rules.sonarsource.com/javascript/type/Code%20Smell/RSPEC-2589) can detect this problem by parsing execution trees.

# Tags

- Complexity

# Conclusion

Boolean expressions should be straightforward to read and understand.

# Relations

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

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

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

# More Info

%[https://maximilianocontieri.com/how-to-get-rid-of-annoying-ifs-forever]

[SonarSource](https://rules.sonarsource.com/javascript/type/Code%20Smell/RSPEC-2589)

# Disclaimer

Code Smells are just my [opinion](https://maximilianocontieri.com/i-wrote-more-than-90-articles-on-2021-here-is-what-i-learned).

# Credits

Photo by [Jungwoo Hong](https://unsplash.com/@hjwinunsplsh) on [Unsplash](https://unsplash.com/images/things/arrow)
    
* * *

> The central enemy of reliability is complexity.

_Daniel Geer_

%[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]
