Clanker Discipline
Apply these rules when writing or reviewing state types, data models, and functions that manage application state. Agents tend to add flags, optional fields, and special cases that compound into state nobody intended — catch that before it lands.
When you find violations, refactor fully. The goal is clean, maintainable code, not minimal diffs. Rip out the flags, reshape the types, restructure the functions. A bigger diff now is better than layering workarounds that compound later.
1. Derive, don't store
Every boolean you add doubles the theoretical state space. When a value can be derived from data you already have, do not store it. The best source to derive from is an event stream: a log of what happened.
Before: cached flags
An agent was asked to show a footer only when the assistant finishes naturally. It invented four flags:
Four fields to answer one question, with four mutation sites elsewhere keeping them in sync.
After: derive from evidence
The answer is now computed from events that already exist.
When NOT to derive
- The domain genuinely has a state machine with ordered transitions. A checkout step is not a cached conclusion; it IS the state.
- A field contains temporal or external data that cannot be rederived (timestamps from async processes, API responses needed downstream).
- The derivation would be more complex than the stored value.
If you cannot derive, encapsulate
If mutable state must exist, trap it in the smallest possible scope. A closure is better than a class field:
Nothing outside the closure can touch the timer.
The debugging payoff
When state is derived from evidence, debugging becomes data-in, answer-out:
No mocking or timing reproduction. The bug is in the events or in the pure function.
2. Make wrong states impossible
Every optional field is a question the rest of the codebase must answer every time it touches that data.
Discriminated unions over optional bags
Null over sentinels
Phased composition over grab-bags
Brand identical primitives
Delete dead variants
If a type has a variant that is never constructed, delete it. A status: 'open' | 'completed' where 'completed' is never set suggests a lifecycle that does not exist.
3. Enforce function contracts
Never add side effects to a pure function
When a pure function quietly gains a side effect, every callsite inherits behavior it did not ask for. If a function needs side effects, extract them into a separate orchestrator.
- Semantic functions are small, pure, and self-describing. All inputs in, all outputs out, no hidden effects.
- Pragmatic functions are orchestrators. They compose semantic functions and contain messy domain glue.
Before: semantic function that grew into a pragmatic one
After: composed from semantic functions
Pick a mutation contract
If a function mutates its input, return void. If it returns a value, clone first. Never mutate the input and return the same reference — callers cannot tell whether to use the return value or the original.
4. Data over procedure
When a long if-chain returns a similar shape from every branch, the logic is a lookup table encoded as code. Convert it to data.
Before: if-chain
After: declarative table
Easier to scan, extend, and test. An agent adding a new step adds a data entry, not a branch in a control flow.
When NOT to convert
If branches have different control flow — not just different return values — keep them as code. A table maps inputs to outputs; it cannot express "call X then conditionally call Y."
Checklist
When reviewing code (yours or an agent's):
- Can any new field be derived from existing state? Derive it.
- Is mutable state visible beyond its minimal scope? Trap it in a closure.
- Do any models allow field combinations that should be impossible? Discriminated union.
- Are there sentinel values (
'none','unknown',-1) wherenullwould work? Use null. - Are there identical type aliases for different domain concepts? Brand or eliminate.
- Does any function both mutate its input and return it? Pick one contract.
- Has a semantic function grown side effects? Extract them.
- Is there an if-chain where every branch returns a similar shape? Make it a table.
- Are there dead type variants never constructed? Delete them.


