Skip to content

Taxonomy

Silent truncation

The value was cut to fit rather than rejected for not fitting.

What it looks like

  • A slice to a maximum length with no check that anything was lost.
  • Rounding down where rounding up was meant, or the reverse.
  • A field written to a column narrower than the value.

Why it survives review

Every input short enough behaves identically, and every input anybody writes down is short enough. Truncation produces a well-formed value — a valid string, a whole number — so nothing downstream objects. The loss is only visible by comparing against what went in.

How to see it

  1. After any cut-to-fit, ask what happens to the part that was removed. If the answer is 'nothing', that is a decision somebody should have made explicitly.
  2. Check whether a too-long value should be truncated or refused. For a name, truncate; for an identifier or a signature, refuse.
  3. Math.floor and Math.ceil are not interchangeable, and swapping them is invisible on whole numbers.

A minimal pair

Correct

if (slug.length > MAX) throw new TooLong(slug);

Defective

slug = slug.slice(0, MAX);

Two different long titles now produce the same slug, and the second one overwrites the first.

Practise it

2 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