# Code Smell 51 - Double Negatives

*Not operator is our friend. Not not operator is not our friend.*

> TL;DR: Avoid double negatives in boolean expressions

# Problems

- Readability

# Solutions

1. Name your variables, methods and classes with positive names.

# Sample Code

## Wrong

[Gist Url]: # (https://gist.github.com/mcsee/f5c32dfa2aefa4f7fcb180ea28995508)
```javascript
if ( !work.isNotFinished() )
```

## Right

[Gist Url]: # (https://gist.github.com/mcsee/842684ebe8f5496d1d6374436a0c9473)
```javascript
if ( work.isDone() )
```

# Detection

This is a semantic smell. We need to detect it on code reviews.

We can tell linters to check for Regular Expressions like *!not* or *!isNot* etc as a warning.

# Tags

- Readability

# Conclusion

Double negation is a very basic rule we learn as junior developers.

There are lots of production systems filled with this smell. 

We need to trust our test coverage and make safe renames and other refactors.

# Relations

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

%[https://maximilianocontieri.com/code-smell-07-boolean-variables]

%[https://maximilianocontieri.com/code-smell-06-too-clever-programmer]

# More info

%[https://maximilianocontieri.com/what-exactly-is-a-name-part-ii-rehab]

%[https://refactoring.com/catalog/removeDoubleNegative.html]

%[https://levelup.gitconnected.com/knot-of-nots-avoiding-negative-names-for-boolean-methods-641896a94a42]

# Credits

<span>Photo by <a href="https://unsplash.com/@herrond">Daniel Herron</a> on <a href="https://unsplash.com/s/photos/no">Unsplash</a></span>

* * *

> It’s harder to read code than to write it.

_Joel Spolsky_
 
* * *
 
%[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]

*Last update: 2021/06/11*
