![cover-img](https://project-assets.showwcase.com/1420x/16381/1677037466573-daniel-herron-vBxbZokRL10-unsplash.jpg?type=webp)
Code Smell 51 - Double Negatives
22 February, 2023
8
8
0
Contributors
TL;DR: Avoid double negatives in boolean expressions
Problems
- Readability
Solutions
- Name your variables, methods and classes with positive names.
Sample Code
Wrong
if (!work.isNotFinished())
Right
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
Code Smell 24 - Boolean Coercions
Code Smell 07 - Boolean Variables
Code Smell 06 - Too Clever Programmer
More Info
What exactly is a name - Part II Rehab
https://refactoring.com/catalog/removeDoubleNegative.html
Credits
Photo by Daniel Herron on Unsplash
___
It’s harder to read code than to write it.
Joel Spolsky
Software Engineering Great Quotes
___
This article is part of the CodeSmell Series.