Skip to content

Taxonomy

Wrong comparison operator

The boundary moved by one, and only the boundary can tell.

What it looks like

  • >= where > belongs, or the inequality pointing the wrong way.
  • == where === belongs, around a value that can be 0, '' or null.
  • A threshold check that is inclusive on one side of the codebase and exclusive on the other.

Why it survives review

Values comfortably inside or outside the range behave identically, and those are the values anybody writes a test with. Only a value sitting precisely on the boundary distinguishes the two versions, and boundaries are exactly what test authors round away from.

How to see it

  1. For every comparison, name the value that sits exactly on the boundary and decide which side it should fall.
  2. Check the same boundary in every place it appears. Half-open in one function and closed in another is a defect even when both are individually defensible.
  3. Watch for a rate limit, a quota or an expiry — these are where an inclusive-versus-exclusive mistake is worth money.

A minimal pair

Correct

return tokens >= 1;

Defective

return tokens > 1;

With exactly one token left, the correct build allows the request and the defective one refuses it.

Practise it

10 diffs in the corpus carry this class. They are not listed, because knowing which diff contains what would make finding it a comprehension question about this page.

Go to the exercises