# Code Smell 118 - Return False

> TL;DR: Don't return explicit booleans. Most boolean usages are code smells.

# Problems

- Declarativeness

- Ninja Code

- Implementative solutions

# Solutions

1. Return a boolean proposition instead of checking a negation.

2. Answer must be a business logic formula, not an algorithm.

# Context

When dealing with boolean formulas, it is more readable to show a business boolean formula than introduce a negated [IF](https://maximilianocontieri.com/how-to-get-rid-of-annoying-ifs-forever) clause.

Programmers tend to return accidental implementative solutions instead of real business rules.

# Sample Code

## Wrong

[Gist Url]: # (https://gist.github.com/mcsee/57c079a22fe139667c8330a937d4dcca)
```javascript
function canWeMoveOn() {
  if (work.hasPendingTasks())
    return false;
  else
    return true;
}
```

## Right

[Gist Url]: # (https://gist.github.com/mcsee/0f0cfe7cd5133dc605555eeb20feaa95)
```javascript
function canWeMoveOn() {
  return !work.hasPendingTasks();
}
```

# Detection

[X] Automatic 

Based on syntax trees, we can safely refactor the code.

# 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-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-51-double-negatives]

# More Info

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

# Credits

Photo by [Morgan Housel](https://unsplash.com/@morganhousel) on [Unsplash](https://unsplash.com/s/photos/not)
  
Thanks to Nico K. for this suggestion.

* * *

> It's not at all important to get it right the first time. It's vitally important to get it right the last time.

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