Awesome Reviewers

Reviewer · Algorithms

Edge-case algorithm correctness

When an algorithm tracks or transforms state over a non-trivial structure (a graph/tree, a time series, a set of mutually-exclusive categories), explicitly enumerate every kind of transformation or edge case the state can go through, and prove each one with a dedicated test — don't rely on the "normal" case to cover them implicitly.

Instruction

When an algorithm tracks or transforms state over a non-trivial structure (a graph/tree, a time series, a set of mutually-exclusive categories), explicitly enumerate every kind of transformation or edge case the state can go through, and prove each one with a dedicated test — don’t rely on the “normal” case to cover them implicitly.

Concretely:

  • Identify every structural edge case up front: parallel/duplicate edges, node merges, relabeling, re-ordering, or out-of-order/late updates, and decide precisely how each affects the tracked state (e.g. a “visited” mark on an edge must be remapped through a merge and must not leak onto a parallel edge with a different condition; a late-arriving signal should only ever raise a running peak, never move other fields backward).
  • When comparing or aggregating values that can arrive in different units or representations, normalize them to one canonical unit/representation before applying any threshold or comparison logic, so the result doesn’t depend on which representation happened to be used.
  • When classifying items into buckets for a summary/report, make the buckets a true partition: define one deterministic rule per item so every item lands in exactly one bucket, and add a test asserting the bucket counts sum to the total.
  • Validate preconditions (e.g. that a path resolves to real edges from a valid start to a valid end) before any mutation is applied, and re-validate against freshly-read state if a dry run and the real write happen against different reads.
  • Prefer algorithms with known, bounded complexity: cap iterative/recursive processes with an explicit, justified bound (derived from a real limit, e.g. max items × max pages) rather than for(;;)/while(true), and prefer a linear-time primitive (e.g. RE2-backed matching) over a heuristic guard against worst-case blowup (e.g. hand-rolled catastrophic-backtracking detection) when one is available.

For example, a check that marks only the newly-added edges on a decision tree must follow prior “taken” marks through declared merges and must only treat a mark as legacy when it maps to the exact same edge (or an unambiguous relabel when there’s a single edge between those nodes) — otherwise a merge or relabel can either hide a real new mark or wrongly flag the same walk twice. Add one test per transformation (merge, relabel, parallel edge) rather than assuming the happy-path test covers them.