Coding Tactic: Avoid Double Negatives
There’s something innately more difficult about thinking “not this” as opposed to “this”. You want to keep an eye out for doubling and even tripling up the negations.
Three ways people typically express negation:
- The
not
symbol, which is usually an exclamation!
- The
else
keyword or symbol (including in ternary operations) - The variable name: (e.g.
notStopped
)
Each on their own is fine. The real issue shows up when you have two or more combined. If you catch this in your code, you can clean it up with these two steps:
-
Rename the variables to their positive state
e.g.
notStopped
becomesisStopped
-
If you have an
else
combined with anot
, remove thenot
and swap the internal brackets:if (!gameOver) { A } else { B }
becomes
if (gameOver) { B } else { A }