# Code Smell 115 - Return True

> TL;DR: Don't return true or false. Be declarative.

# Problems

- Readability

- Primitive Obsession

- If/Else abuse

# Solutions

1. Return truth value in a declarative way

2. Replace *IF* With polymorphism.

# Context

Dealing with low-level abstractions, we usually return booleans. 

When we create complex and mature software, we start to forget about this primitive obsession and care about real-world rules and identities

# Sample Code

## Wrong

[Gist Url]: # (https://gist.github.com/mcsee/973a11295d0d93baa620763dd8eff801)
```java
boolean isEven(int num){
     if(num%2 == 0){
       return true;
    } else {
       return false;}        
}
```

## Right

[Gist Url]: # (https://gist.github.com/mcsee/ab38ef6fcc5dd6dea98f1edb452e75e9)
```java
boolean isEven(int numberToCheck){
  //We decouple the what (to check for even or odd)
  //With how (the algorithm)
  return (numberToCheck % 2 == 0);     
}
```

# Detection

[X] Automatic 

Many linters can check syntactic trees and look for explicit true/value returns

# Tags

- Primitive

# Conclusion

Search on code libraries for *return true* statements and try to replace them when possible.

# Relations

%[https://maximilianocontieri.com/code-smell-36-switchcaseelseifelseif-statements]

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

# More Info

- [How to get rid of IFs forever](https://maximilianocontieri.com/how-to-get-rid-of-annoying-ifs-forever)

# Credits

Photo by <a href="https://unsplash.com/@enginakyurt">engin akyurt</a> on <a href="https://unsplash.com/s/photos/flag">Unsplash</a>  

* * *

> The good news is: Anything is possible on your computer. The bad news is: Nothing is easy.

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