Pattern: Negated condition in if statement or ternary
Issue: -
Using negated conditions with else clauses makes code harder to read. The code can be made more readable by inverting the condition and swapping the if/else blocks or ternary expressions.
Example of incorrect code:
if (!condition) {
doSomething();
} else {
doSomethingElse();
}
!condition ? doSomething() : doSomethingElse();
if (!a && !b) {
doSomething();
} else {
doOtherThing();
}
Example of correct code:
if (condition) {
doSomethingElse();
} else {
doSomething();
}
condition ? doSomethingElse() : doSomething();
// When there's no else clause, negation is fine
if (!condition) {
doSomething();
}