Make implicit state machines explicit
Find the state machines hiding in flags and scattered conditions, and turn each into one explicit, tested machine.
Prompt · works with Any coding agent · adapted from Kent C. Dodds
Find every implicit state machine in this app. There are probably a lot of them.
An implicit state machine is transition logic nobody wrote down as one: booleans that only make sense in some combinations (`isLoading && !error && data`), a status string compared in several files, handlers and effects that each nudge the same thing. An explicit one names every state, every event and every allowed transition in one place, so an impossible combination can't be represented.
1. **Inventory.** Search the whole app, including machines spread across several components or files. For each one, list where its pieces live (file paths), its states, its events, and every flag combination it can represent today that should be impossible. This step is done when every module is accounted for, including the ones with none.
2. **Choose.** For each machine, pick the change that improves all three at once:
- UX: a user can only ever see a real state, never a spinner and an error together.
- DX: a developer reads the states and transitions in one place.
- AX: an agent finds the machine, reads its transitions and changes it in one file.
If a machine is better left as it is, say why.
3. **Make it explicit.** First pin the machine's current behaviour with tests that walk its transitions, and run them green. Then rewrite it as one explicit machine with whatever this codebase already uses for state (a reducer, a discriminated union, a statechart library). Add a dependency only when the inventory shows the same need in several places, and name it in the report. Run the pinning tests again: they stay green, except where a test captured an impossible state, which you report.
4. **Report.** One line per machine: where it lived, what it is now, and what UX, DX and AX each gained. Then list every impossible state you removed.