Skip to content

Taxonomy

Incorrect early return

The function gives up before it has finished.

What it looks like

  • A guard clause whose condition is broader than the case it was written for.
  • A return inside a loop where a continue was meant.
  • An early return added during a refactor that skips a side effect further down.

Why it survives review

An early return reads as defensive programming, which reviewers are trained to approve of. The version that returns too eagerly still produces a well-formed value — just the wrong one — and it produces it faster, which if anything looks like an improvement.

How to see it

  1. For each early return, ask what work sits below it and whether every path that skips that work should.
  2. Inside a loop, check whether return was meant to leave the loop or the function.
  3. A guard that lost a conjunct in a diff has become a broader guard. Widening a guard is a behaviour change.

A minimal pair

Correct

if (cached !== undefined && !isStale(cached)) return cached;

Defective

if (cached !== undefined) return cached;

A stale entry is served from the cache instead of being refetched.

Practise it

No exercise in the corpus sets this class yet. The lesson stands on its own — the corpus grows by adding subjects, and pretending otherwise would hide the gap.