The TiffinBox BuildOne system, every chapter
0 / 0

Workbench / The TiffinBox Build

S35 · V3·34

Add a Case, Count the Places

Volume 3, chapter 34. TiffinBox models orders the "other way" the chapter describes: data (sealed records for states and events) plus one pure function that pattern-matches on both. This stage adds a new kind of event, a kitchen asking for more time, and lets the compiler and the tests say what has to change: four switches that no longer cover every case, a specification table one column short, and an invariant that assumed every path was strictly forward. It also turns the server's 13-condition if-chain into a route table, with a golden master of 44 recorded HTTP exchanges proving the refactor changed nothing.

6 parts · ~4 min to read
Updated Suggest a correction

Stage 35: add a case, count the places

The brief

What you'll build.

  • web-tests/golden.py: a golden master: 44 scripted requests against a fresh server (pages, JSON, orders, events, errors, cross-origin refusals, static files), with status, the headers that matter and the body recorded, and what varies between runs (times, nonces, the stream's epoch, the order form's key) replaced by placeholders; record writes web-tests/golden/server.txt, no argument compares; release-check.sh runs it;
  • a route table: Server.Route(method, pattern, handler) and Handler, one list of 13 routes in place of the if-chain; orderJson and nextTicket extracted;
  • OrderEvent.KitchenDelayed(at, extraMinutes): Accepted stays Accepted with more prep time (total at most 120 minutes, acceptance time unchanged); every other state refuses it; POST /orders/{id}/events with delay <minutes>;
  • tests: the lifecycle table gains a column, a delay test, and the history invariant collapses repeated states; 45 core tests.

Why a real company needs this. Adding a case is the most common change in a business system: a new order state, a new payment method, a new event. Whether the code tells you everything that must change, or you find out in production, is decided by how it was modelled. And refactoring without a safety net is how behaviour changes nobody intended ship.

Why now. Chapter 34 is OOP and its alternatives: encapsulation, polymorphism, inheritance and composition, dispatch, the expression problem, Liskov substitution, values and identity. TiffinBox has both styles: sealed data with pattern matching in the core (Volume 1) and a large class with an if-chain in the server.

What you'll have at the end.

  • a new event, added where the compiler said;
  • a server whose routes are data you can read in one screen;
  • a golden master for every later refactor;
  • ADR-0037: data plus functions for the domain, objects for the boundaries.

Theory to own

  1. The expression problem. Which change is cheap in each design: adding a type, or adding an operation? (The chapter, "The expression problem".)
  2. Exhaustiveness. What does a sealed interface give a switch that an enum or a string doesn't? (Companion, Gaps 1.)
  3. Characterisation tests. How do you refactor code whose behaviour you don't fully know? (Companion, Explain it differently 6.)
  4. Values and entities. Is an Order a value or an entity? (The chapter, "Values and identity"; Companion, Code walkthrough.)

Build it

Step 1: the golden master

Write golden.py. Choose requests that exercise every route and its errors. Run it twice and normalise until two runs of unchanged code agree, then record.

Checkpoint. golden master: 41 exchanges, identical, run after run.

Step 2: the route table

Replace the if-chain in routeUnthemed with a list of Routes, keeping the order (first match wins), and extract the two inline handlers. Run the golden master.

Checkpoint. 40 server tests and golden master: 41 exchanges, identical.

Step 3: add an event and let the compiler count

Add record KitchenDelayed(Instant at, int extraMinutes) implements OrderEvent and compile core before changing anything else.

Checkpoint. A list of every place that must decide what a delay means.

Step 4: let the tests count too

With Transition updated, run the core tests.

Checkpoint. 45 core tests pass, with the table and the invariant updated for the right reasons.

Senior and architect lens

1. Model the domain so the compiler finds the places. Sealed types turned "where do I need to handle the new event?" into four compiler errors. Strings, enums with default, and instanceof chains turn it into a search.

2. ADR-0037: domain as data, boundaries as objects. Write it with these points:

  • Domain: states and events are sealed records; rules are pure functions with exhaustive switches; no default on a sealed switch.
  • Boundaries: HTTP routes, persistence and messaging are objects behind interfaces (a Handler per route), so transport can change without the domain.
  • Changes: a new case goes in the sealed type first; the compiler's errors are the checklist; the lifecycle table is updated in the same change.
  • Refactors: behind the golden master; a deliberate behaviour change re-records it in the same commit, with the diff reviewed.

3. Tests that encode an assumption break on legitimate change. The history invariant was right about the rule and wrong about its encoding; a test failing after a valid change is a prompt to restate the rule, not to delete the test.

4. The contract didn't move. A delay changes prepMinutes, which the order's JSON doesn't include, so clients can't see it yet; the kitchen queue's promised time is also unchanged. Stage 46 (API design) adds it to the contract.

What breaks first, and when it's addressed:

Pressure What breaks Where it's addressed
the delay is invisible prepMinutes isn't in the contract; the queue's promise doesn't move stage 46 (API design), stage 52 (jobs: promise reminders)
commands are strings the server's parser has a default; new events are missed silently stage 49 (Spring Web chapter: typed request bodies)
Server is still large 552 lines: transport, orders and kitchens in one class stage 47 (dependency injection: services)
the golden master's coverage edges not in the script aren't checked stage 55 (testing: property-based HTTP tests)

Evidence

  • golden.py reports 44 exchanges, identical on the final build; golden/server.txt's diff from stage 34 contains only the three delay exchanges.
  • 45 core and 40 server tests pass; exp/35/delay-compile.out holds the four errors and the two test failures.
  • ADR-0037 is committed.

Carry forward

  • The golden master runs in the release check and guards every server refactor from here on.
  • The route table is where stage 46 adds versioned routes and stage 47 injects handlers.
  • KitchenDelayed reaches the kitchen display when the contract carries prep times (stage 46).