# Design the checks

A station that checks work does four things, in this order:

| Stage | Does | Touches the outside world |
|---|---|---|
| 1. Fetch | Reads everything the checks need, once | Reads only |
| 2. Check | Runs every rule over what was fetched | No |
| 3. Judge | Interprets what the checks found | Only to ask a model |
| 4. Act | Records the verdict and writes results to other systems | Writes |

The stages are functions called one after another inside the station's body. A stage becomes a station of its own only when it needs its own runs or retries: see [Write a station that processes workpieces](/docs/guides/process-workpieces#several-stages-in-one-body). Judging is covered in [Code first, the model for judgment](/docs/method/code-before-the-model), and the verdict in [Three verdicts](/docs/method/verdicts). Acting comes last, after every check and the judgment.

## Fetch everything first

The first stage reads every record the checks will need. After that, no check calls another system.

- **One snapshot.** Every check sees the same data, even if the source changes during the run.
- **Network failures happen in one place,** before any check has run.
- **Checks become pure functions,** so you can test them with saved data and no connection.
- **No repeated calls** for the same record.

The fetch stage:

- **fails at once** when something the checks can't do without is missing. "Invoice not found" is a failure, not a verdict.
- **records what it read**: the ids, the counts and the time, so a reader knows what the checks saw.
- **reads shared data once per run.** If every invoice is compared with the register of invoices already posted, read the register once, not once per invoice.

The snapshot is for judging. When the act stage changes an outside system, it reads the record it's about to change again, just before changing it.

## Check from broad to narrow

Check the big picture first, then the detail. acme's `IN1` checks each invoice in four tiers:

| Tier | Question | Checks |
|---|---|---|
| 1. What it must carry | Is everything there? | It has a vendor, a number, a date and a total |
| 2. Does it add up | Are its own numbers consistent? | The lines add up to the subtotal; subtotal plus tax equals the total |
| 3. Does it follow the rules | Is it acceptable? | The due date isn't before the invoice date; the tax rate is one acme accepts |
| 4. Is it new | Have we seen it before? | No posted invoice has the same vendor and number |

- **The broad finding frames the narrow ones.** A line that's off by 180 reads differently when you already know the total is off by 180.
- **A narrow check can point back.** "Line 3 accounts for the difference in the total."
- **A reader can stop early.** If the totals hold, the detail may not matter.
- **One cause shows up once.** A missing total is reported by tier 1. The tier 2 checks that need the total don't run on that invoice, so they don't report the same gap again.

## One question per check

Each check answers one question at one level of detail. If its question needs "and" or "and also", it's two checks.

| Two checks in one | Split |
|---|---|
| Do the lines add up, and is the total right? | Do the lines add up to the subtotal? Is subtotal plus tax equal to the total? |

A check is a rule a practitioner would name: "subtotal plus tax equals the total". It isn't a primitive ("parse the number") or a bundle ("validate the invoice"). Give each check:

- a stable id that findings and reports refer to;
- a title a practitioner reads as a rule;
- its weight: hold, warn or info (see [Three verdicts](/docs/method/verdicts));
- a version that goes up when its logic changes.

## Group checks by what they ask

Put a check with the checks that ask the same kind of question, not next to the code that happened to have its data. A total check added to the line-by-line comparison because that code already summed the lines is in the wrong place. It's a tier 2 question about the whole invoice.

**The test:** label each check by its kind (must carry, adds up, follows the rules, is new). Two checks with the same label in different groups, or two labels in one group, mean the grouping is wrong.

**Shared work points to the same group.** When two checks need the same computed value, compute it once and let both read it. If they ask the same kind of question, they belong together. If they ask at different levels, pass the value on; don't compute it twice.

## Follow the practitioner

Order the checks the way the person who does this work by hand would check it. Show them the list. If they wouldn't check it in that order, the order comes from how the code was built, not from the work.

## Write the report where you compute

The stage that works out the findings also writes the report about them. A separate stage that only formats data and attaches it decides nothing. Keep the report separate only when it does real work of its own, such as combining the check results with the judgment.

## Related

- [Code first, the model for judgment](/docs/method/code-before-the-model): what comes after the checks
- [Three verdicts](/docs/method/verdicts): how findings add up to a verdict
- [Classify before you present](/docs/method/classify-before-present): turning raw records into what the reader needs
- [One job per station](/docs/method/one-job-per-station): where acting and delivering happen
