Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

bool-comparison

Flags a comparison with true or false, which can be written as the value itself or its negation.

Why

if (ready == true) says the same thing as if (ready), and if (ready == false) the same as if (!ready). The comparison only adds something to read past.

What it flags

An == or != where one side is true or false, on either side:

ComparisonWritten as
x == truex
x != falsex
x == false!x
x != true!x

When the fix negates a comparison or a ternary, it adds parentheses so the ! applies to all of it: a < b == false becomes !(a < b).

What it leaves alone

  • A comparison between two literals, like true == false.
  • Other operators, like count > 0.

Example

// Before
if (ready == true) {
    start();
}

return done != false;

// After
if (ready) {
    start();
}

return done;