Covering Every Case in a Decision

A decision is exhaustive when each allowed input reaches a defined outcome.

Source
Anbloq
Language
English
Created
Updated
Chapters
4
Reading time
1 min
In this article4 chapters
  1. Enumerate a small input domain
  2. Try every allowed case
  3. Separate legal cases from unexpected input
  4. Cover each case promised by the type and caller

Enumerate a small input domain

A booking has the statuses pending, confirmed, and canceled. A label function can return a literal for each status. The list of possible inputs belongs in its function contract. An unexpected status deserves a deliberate error or fallback chosen at the boundary.

Try every allowed case

A logic chain handling confirmed and canceled may treat any other value as pending. That handles pending and hides an invalid status such as paused. Test those two cases separately. A domain validation step can prevent arbitrary text from reaching the decision.[1][2]

Separate legal cases from unexpected input

A reservation state can be pending, confirmed, or canceled. A display predicate that renders everything except confirmed and canceled as pending also paints a malformed state that way. If outside values can arise from older storage or a new client, give them their own contract. Test an actual pending value and an unknown spelling. Identical outcomes may be desired for a specific view, but that should be a deliberate choice. Domain validation describes where to reject inputs outside the allowed set.

Cover each case promised by the type and caller

A table can document one view for each legal state. When adding a status such as expired, check all consumers of the status and the transition rules. Some type systems help detect an unhandled variant. A catch-all branch can be appropriate for forward compatibility, provided its behavior gives useful information and does not perform an unrelated state change. See exhaustive matching after a new case.

Include every small legal variant in tests. Verify the case that returns a result and the case that merely displays a diagnostic when both are allowed by the interface.

References

  1. Python Software Foundation, Python Tutorial: Control Flow ↩
  2. Python Software Foundation, Python Language Reference: Expressions ↩