Write documentation first, code second, for any non-trivial change to a codebase. Use when starting a new feature, refactor, bug fix, or migration; when multi-file work needs a reviewable plan; or right after a discuss-before-begin session has reached consensus. Keeps decisions, plans and the "why"
---
name: document-ahead-coding
description: >-
Write documentation first, code second, for any non-trivial change to a
codebase. Use when starting a new feature, refactor, bug fix, or migration;
when multi-file work needs a reviewable plan; or right after a
discuss-before-begin session has reached consensus. Keeps decisions, plans
and the "why" in docs/ so the plan is reviewable, code stays traceable, and
nothing is silently lost.
---
# Document Ahead of Coding
## Why
Coding is cheap to write, expensive to redo. A short documentation phase forces
the plan to be explicit before it costs time to implement, lets the user review
and veto a bad direction when it costs minutes (not days), and leaves a record
of *why* — not just *what* — for the next reader.
- **Review before cost**: reject a bad plan before it becomes code.
- **Decisions separated from code**: choices live in docs/, not buried in
commit messages or lost in chat.
- **Traceability**: every change points back to the task doc that authorized it.
## When to use
More than a trivial one-file edit:
- a new feature, module, or subsystem
- a refactor or migration touching many files
- a bug fix whose root cause is not obvious
- any work needing more than one step or touching more than one subsystem
## Workflow (paired with discuss-before-begin)
This skill is the **downstream** of `discuss-before-begin`: discuss first until
consensus, then this skill turns the agreed plan into durable documents, then
code.
1. **Discuss** — until the user confirms consensus (see `discuss-before-begin`).
2. **Record** — write the discussion and the user's explicit choices into
`docs/discussion/`.
3. **Distill** — extract reusable rules from the discussion into
`docs/principle/`.
4. **Decompose** — break the work into tasks in `docs/task/` (one file per
task).
5. **Describe** — start `docs/manual/` as the shape of the feature becomes
clear.
6. **Code** — start only after the docs above exist and the user has seen them.
7. **Sync** — when reality diverges from the plan, update the docs *first*, and
log deferred/rejected ideas in `docs/temp/`.
8. **Close** — mark task docs done and make sure `docs/manual/` matches reality.
## Directory layout
```
docs/
discussion/ # conversations & the user's explicit choices
principle/ # reusable rules distilled from discussions
task/ # task + sub-task files
01-do-something.md
01-01-do-something.md
01-02-do-something.md
02-do-something.md
...
manual/
how-to-do-something.md # how to use the codebase
# if there are chapters, make a folder named
# after the chapter
temp/ # "decided not to do now" + why; may be
choose-not-do-something.md # revisited later
why-not-do-something.md
```
## Doc conventions
### discussion/
- One file per discussion topic. Record the user's actual decisions verbatim
where possible, plus the alternatives rejected and why.
### principle/
- Reusable rules that survive beyond one task (patterns, constraints, "always
X / never Y"). These are the *laws* future tasks must obey.
### task/
- One file per unit of work. Naming: `NN-description.md`; subtasks use
`NN-NN-description.md` (decompose when a task is too big to review as one
change).
- Each task doc **must** contain these fields:
| Field | Meaning |
|---|---|
| **Goal** | what "done" looks like |
| **Why** | link to the discussion/principle that motivated it |
| **Approach** | the concrete steps, in order |
| **Files touched** | expected files/areas, so scope is reviewable |
| **Acceptance criteria** | how to verify it works |
| **Status** | `todo` → `doing` → `done` |
### manual/
- Write as the feature takes shape; update it as the feature lands. Never let
it describe something the code no longer does.
### temp/
- Record things deliberately *not* done now, and the reason. This is a
reversible-decision graveyard: a future "why didn't we..." is answered here.
## Keeping docs in sync (iron rules)
1. **Docs lead, code follows** — before writing a line, the relevant task doc
must already describe what you are about to do.
2. **Reality surprises → doc first** — if an API doesn't exist, a design
doesn't fit, or a step turns out different, update the doc first, then code.
If it changes the agreed plan, tell the user and record it in `temp/`.
3. **No `done` with stale docs** — a task may not be marked done while its doc
is out of date.
4. **Docs vs. code conflict is a bug** — if they disagree, fix the doc, the
code, or both, and say which.
## Definition of done for the documentation phase
The docs are "ready to code from" when:
- every task has an approach and acceptance criteria,
- every discussion that led to a decision is recorded,
- the user has read and approved the plan.
## Anti-patterns
- **Docs as ritual** — writing docs nobody reads. Every doc must change a future
decision or save a future reader's time.
- **Over-documentation** — don't restate the code; record decisions,
constraints, and "why".
- **Drift** — "I'll update the docs at the end" is how docs become lies. Update
them together with the code.
- **Silent deviation** — changing the plan during coding without telling the
user or logging it in `temp/`.
## Scale
- **Large task** — full pipeline above.
- **Small task** — a single task doc may be enough, but still name the Goal and
Acceptance criteria, and still record any surprising decision in `temp/`.
don't have the plugin yet? install it then click "run inline in claude" again.
separated procedure into eight explicit numbered steps with inputs and outputs, added decision points for trivial changes and rejections and surprises, formalized output contract and outcome signals, clarified external prerequisites like docs/ directory structure and user availability, added edge cases like emoji missing manual and mid-coding pivots.
write documentation before touching code on any non-trivial change to a codebase. this forces the plan to be explicit, lets reviewers veto bad directions while they're still cheap to change, and leaves a permanent record of why decisions were made, not just what happened. use this after reaching consensus in discuss-before-begin, or when starting a new feature, refactor, bug fix, or migration that touches multiple files or subsystems. it separates decisions (living in docs/) from implementation (code), making both traceable and reviewable.
docs/ directory (or permission to create one). standard layout is docs/{discussion, principle, task, manual, temp}/.record the discussion (input: notes from discuss-before-begin or prior consensus; output: docs/discussion/*.md files)
docs/discussion/topic-slug.md (e.g., auth-strategy.md).distill reusable principles (input: discussion files; output: docs/principle/*.md files)
docs/principle/rule-slug.md (e.g., api-versioning-policy.md).decompose into tasks (input: discussion, principles, scope agreement; output: docs/task/*.md files)
docs/task/01-feature-x.md, docs/task/01-01-auth-layer.md, docs/task/01-02-data-model.md, etc.todo → doing → done. update as work progresses.draft the manual (input: task files, feature shape; output: docs/manual/*.md)
docs/manual/chapter-name/ folders.review with user (input: discussion, principle, task, manual files; output: user approval)
code (input: approved task docs; output: code changes in repo)
docs/temp/ if it deviates from the agreed plan.sync docs with reality (input: surprises during coding; output: updated docs + entries in docs/temp/)
docs/temp/ explaining what changed and why. notify the user.close the task (input: code merged, docs reviewed again; output: task status marked done, manual synced)
status: done only after the code is merged and the doc is up to date with what was actually implemented.docs/manual/ and update it to match the shipped feature. no stale docs.docs/temp/ for future reference.if the change is trivial (single-file edit, obvious fix, no design decisions): skip the full pipeline. write only a one-line task doc with goal and acceptance criteria, and skip discussion/principle layers.
if the user rejects the plan at review (step 5): do not code. return to the discussion or principle layer (step 1 or 2), update those docs, and re-decompose tasks. re-present to user.
if you hit a surprise during coding (step 6, 7): stop coding immediately. do not push forward with a different approach. update the task doc to reflect reality, create a docs/temp/ entry explaining the deviation, tell the user, and wait for approval to continue.
if the manual doesn't yet exist when coding begins: create a stub docs/manual/overview.md with the feature's high-level purpose and entry points. expand it as the feature lands. do not leave it blank.
if a task has sub-tasks and only one sub-task is approved by the user: code only that sub-task. do not assume the parent task is approved. each task must be explicitly approved.
if docs/ directory does not exist: create it with subdirectories discussion/, principle/, task/, manual/, temp/. this is a prerequisite; do not proceed without it.
documentation phase complete when all the following exist and the user has approved:
docs/discussion/*.md: one file per discussion topic, recording choices and rejected alternatives.docs/principle/*.md: reusable rules distilled from discussions (may be empty if no cross-task principles emerged).docs/task/*.md: one file per unit of work. each task has goal, why, approach, files touched, acceptance criteria, status.docs/manual/*.md: draft user-facing or maintainer-facing docs covering the feature. may be incomplete, but must not contradict the code.during coding, when reality diverges:
docs/temp/deviation-topic.md (if the change is major) documenting what changed and why.task closure:
done in the task file.docs/manual/ updated to match the shipped code (no stale descriptions).docs/temp/ for future reference.the user knows the skill worked when:
before coding: you have a written, reviewable, approved plan that is specific enough to code from. the user has read it and said "yes, build this." no ambiguity about approach or scope.
during coding: when something doesn't match the plan, you catch it, document the change, and ask before proceeding. the user sees the deviation in docs/temp/ or a direct message, not as a surprise in a pull request.
after coding: the code matches the approved plan (or the approved deviations). docs/manual/ is up to date. task docs are marked done. future readers (the user, a teammate, or you in six months) can open docs/task/ and understand what was built and why. they can open docs/temp/ and see what ideas were considered and rejected.
net effect: decisions are not buried in commit messages or chat. plans are not silent. nothing is lost.