Not operator is our friend. Not not operator is not our friend.
TL;DR: Avoid double negatives in boolean expressions
- Name your variables, methods and classes with positive names.
if ( !work.isNotFinished() )
if ( work.isDone() )
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.
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.
It’s harder to read code than to write it.
This article is part of the CodeSmell Series.
Last update: 2021/06/11