Sub topic 5
Capturing design decisions before code
This chapter assumes you can read a requirements document with acceptance criteria, as covered in the previous chapter. It also assumes you have used a coding agent on a multi-file change.
What the design document is for
Requirements say what the system must do. Code says how it does it. The design document sits between them and records the choices that the requirements leave open. These include which components exist, how data is shaped, and which trade-offs you accepted.
An agent will make those choices whether or not you do. If the spec is silent, the agent picks a storage format, an error style, or a library on its own, and picks differently each session. A design document turns those hidden choices into text you can review before any code exists. Reviewing a paragraph is cheaper than reviewing a 600-line diff.
What belongs in a design
Keep the design to the decisions someone could reasonably make differently. A useful design usually has these sections:
- Overview: two or three sentences on the approach.
- Components and interfaces: the modules involved and the function signatures between them.
- Data model: the fields, types, and storage that matter.
- Decisions: each choice, the alternatives you rejected, and the reason.
- Error handling: what fails, and what the caller sees.
- Testing approach: which acceptance criteria are checked at which level.
- Requirement mapping: which part of the design satisfies which criterion.
The decisions section is the most valuable. A rejected alternative with a reason stops the agent from "improving" your design back into the option you ruled out.
A worked example
Here is a design for the password reset requirement from earlier chapters.
## Design: Password reset
### Overview
A `ResetTokens` module issues and redeems single-use tokens. The HTTP
layer calls it and never touches storage directly.
### Interfaces
- `issue(email: str, now: datetime) -> str | None`
- `redeem(token: str, now: datetime) -> str` (returns the user id)
### Data model
`reset_tokens(token_hash, user_id, issued_at, used)`
### Decisions
1. **Store a hash of the token, not the token.**
Rejected: storing the raw token. A database leak would expose
usable reset links.
2. **Pass `now` as a parameter.**
Rejected: calling `datetime.now()` inside the module. Tests for the
30-minute expiry would need to patch the clock.
3. **Return the same response for unknown emails.**
Rejected: a 404. It reveals which emails are registered.
Satisfies: Requirement 1.2.
### Error handling
`redeem` raises `TokenError` for unknown, expired, or used tokens.
The HTTP layer maps it to one generic message.Decision 2 shows how design supports verification. Passing now as an argument is what lets a test check the 30-minute boundary without waiting, which the testing chapter relies on.
Common errors
- Restating the requirements. If the design repeats the acceptance criteria, it adds nothing. Write only what the requirements do not say.
- Writing the implementation. Full function bodies belong in code. Signatures and data shapes are enough, and long code in a design goes stale quickly.
- Decisions without reasons. "Use SQLite" gives the agent no way to judge an edge case. "Use SQLite because the service runs on one node" does.
- Leaving open questions to the agent. Phrases like "choose a suitable hashing method" hand the decision back. Name the method or mark the question as open and resolve it before tasks begin.
- Contradicting the requirements. Check the mapping section. A criterion that no design element satisfies is a gap, and the agent will fill it with a guess.
Reviewing the design
Treat the design as the first review gate. Ask the agent to draft it from the approved requirements, then read it for the decisions you disagree with. Edit the text directly, because corrections here cost one sentence each. Once you approve it, the task list can reference specific design elements, such as "implement redeem as specified."
Exercise
A design says "Store reset tokens securely" and the agent stores them as plain text. What is wrong with the design, and how would you fix it?
Show answer
The statement is a goal, not a decision, so the agent chose its own interpretation. Replace it with a concrete decision that includes the rejected alternative and the reason, for example "Store a hash of the token. Rejected: raw tokens, because a database leak would expose usable reset links."
Exercise
Why does the example design pass now into issue and redeem instead of reading the clock inside them?
Show answer
The 30-minute expiry in the acceptance criteria can then be tested by supplying fixed times, with no waiting and no clock patching.