Chapter 4: Reading and debugging

Some bugs are common enough to know on sight. This one is probably the best known.

Off-by-one

Also called Fencepost error

Being wrong by exactly one at an edge: stopping a step too early or too late, or miscounting because lists number from 0. A list of 5 has positions 0 to 4, so position 5 is off the end.

These get past casual checking, because the middle of the data is fine and only the edges break: the first item, the last item, the empty list. Agents make them too. So ask for tests at the edges, because reading the code carefully is not a reliable way of catching one.

Say it to your agent

“Add tests for getPage at the edges: page 1, the last page, one past the last page, an empty list, and a list with exactly one full page. Run them before you change the code.”

The workshop

Name a word, or describe what you mean in everyday language.