A style booklet for ML systems code · 15 rules · 11 chapters
Readable Python.
Code that reads like the best serving engines: open the main loop, and understand the system from one screen.
This is not a formatting guide. Let ruff format and a lint config settle spacing, quotes and import order, and never discuss them in review. Everything here is about structure: where state lives, how a loop is shaped, what a comment is for. Those are the things that make code easy or hard to follow, and no tool can fix them.
Each chapter states its rules, then shows a draft struck through in red and the revision beside it. The examples are small on purpose; the patterns scale.
The fifteen rules
Optimize for the reader. Code is read far more often than it is written. When writing it is easy and reading it is hard, rewrite.
Chapter 1The top-level loop is a list of named steps. A reader should see the whole system in one screen.
Chapter 2One class, one job. Data crosses boundaries as dataclasses.
Chapter 2Every piece of state has exactly one owner. No parallel dicts kept in sync by hand.
Chapter 3Use an enum for status, not a pile of booleans.
Chapter 3Functions return results. They don't mutate their arguments.
Chapter 4One level of abstraction per function, and at most four parameters.
Chapter 4Name things with domain words, and use the same word for the same thing everywhere.
Chapter 5Comments say why. The code says what. Git says when.
Chapter 6Fail loudly at boundaries. Decide what's recoverable in one place. No blanket
Chapter 7except Exception.Each piece of mutable state belongs to one thread. Hand off with queues.
Chapter 8Only overlap work that uses different hardware, and prove it with a profile.
Chapter 8Add an abstraction at the second implementation, not the first.
Chapter 9Tests describe behaviour, one idea per test.
Chapter 10Review AI-written code for what to delete.
Chapter 11
Contents
- PrinciplesRP01
- The shape of a systemRP02–03
- StateRP04–05
- FunctionsRP06–07
- NamingRP08
- Comments and docstringsRP09
- ErrorsRP10
- Concurrency and the GPURP11–12
- AbstractionsRP13
- TestsRP14
- Reviewing AI-written codeRP15
How to use it
- In review, cite rules by code, like a linter: "RP04: who owns this dict?" It keeps reviews short and impersonal.
- With coding agents, link the rules from
AGENTS.mdorCLAUDE.md. Agents follow concrete rules well, and chapter 11 lists what they tend to get wrong. - Breaking a rule is fine when you can say why in one sentence. Put that sentence in a comment (RP09).
Set in Newsreader, Instrument Sans and IBM Plex Mono. The red is the editor's pencil: it marks rules, chapter numbers and struck drafts, and nothing else.