Write Code Documentation
Purpose
Make code safe to use and change by documenting what the code cannot say for itself: the contract a caller relies on, the constraints and invariants, and the reasons behind surprising decisions. Fewer, precise comments beat many that echo the code and rot.
When to use
- A public function, class, module or library API has no or weak doc comments.
- Existing comments are outdated, misleading or restate the code.
- Code with hidden business rules, workarounds or performance tricks is about to be handed over.
When not to use
- Project-level setup and usage. Use
readme-writing.
- Endpoint-level reference for HTTP or message APIs. Use
api-reference-docs.
- Understanding unfamiliar code before documenting it. Use
code-explanation or legacy-code-comprehension first.
Inputs
Required:
- The code to document, with language and enough context to understand callers.
Optional, improves quality:
- The team's doc comment convention (e.g., the language's standard docstring or doc-comment style) and whether docs are generated from comments.
- Background on business rules, tickets or incidents behind non-obvious code.
- Intended audience: internal maintainers or external library users.
If the reason for a non-obvious decision cannot be derived from the code, do not invent it: write a TODO(owner): explain why ... or list it as an open question.
Process
- Determine the visibility of each element (public API, package-internal, private). Public API gets full contract docs; private code gets comments only where the why is non-obvious.
- Before commenting, look for things that should be code instead: unclear names, magic numbers, long functions. Propose the rename or extraction and skip the comment it would replace.
- For each public element, write a one-line summary in the language's convention that says what it does for the caller, in domain terms.
- Document the contract: parameters (meaning, units, allowed ranges, nullability), return value (including empty and not-found cases), errors or exceptions thrown and when, side effects (I/O, state, events), thread-safety and idempotency where relevant.
- Add constraints and invariants that callers or maintainers must preserve (ordering, precision, time zone, rounding, performance limits).
- For non-obvious implementation choices, add a short why comment at the exact line: business rule, regulatory reason, bug workaround with its reference, or measured performance trade-off.
- Add a short usage example for public APIs whose correct use is not obvious; keep it compilable in principle.
- Remove or rewrite comments that restate the code, are wrong, or reference things that no longer exist; mark deprecated elements with the replacement and removal plan.
- Label every stated reason you inferred from the code as
[ASSUMPTION] in your response so the author can confirm it before committing.
- Present the result as the documented code (or a diff), followed by a short list of proposed code changes and open questions. If the user continues, suggest
api-reference-docs for external API docs or clean-code-review for the naming and structure findings.
Output format
## Documented Code
<code with doc comments and why-comments, in the language's convention>
## Proposed Code Changes Instead of Comments
- <rename / extract constant / extract function> — <reason>
## Removed or Corrected Comments
- <location>: <what was wrong or redundant>
## Open Questions and Assumptions
- [ASSUMPTION] <inferred reason> — confirm with <owner>
Quality checklist
Common pitfalls
- Documenting what the code does line by line. It doubles maintenance and becomes wrong at the next change; document intent and contract.
- Hiding a bad name behind a comment. Rename the thing instead.
- Writing "handles errors" without saying which errors, how they surface and whether the operation is retried.
Example
Input: def price(q, c): return round(q * c.base * (0.9 if q >= 100 else 1), 2)
Weak: # multiplies quantity by base price and applies discount
Strong:
BULK_THRESHOLD = 100 # proposed: replaces magic number
BULK_DISCOUNT = 0.10
def price(quantity: int, catalog_item: CatalogItem) -> Decimal:
"""Return the net line price in the item's currency, rounded to 2 decimals.
Orders of BULK_THRESHOLD units or more get BULK_DISCOUNT.
Quantity is expected to be >= 1; it is not validated here.
"""
- Open question: should
quantity < 1 raise an error? The current code silently returns 0 or a negative price.
- [ASSUMPTION] The 10% bulk rule comes from the commercial terms; confirm with the pricing owner.
- Proposed: use a decimal type and an explicit rounding mode instead of float
round.
1---2name: code-documentation3description: "Writes or improves docstrings, API comments and inline comments so they state contracts, intent, constraints and non-obvious reasons (the why), not a restatement of the code (the what), and flags comments that are wrong, stale or should become code. Use when a developer asks to document a function, class, module or public API, to review existing comments, or to prepare code for handover."4license: MIT5metadata:6 source: https://skillmd.com/skills/muratdincer/code-documentation7 version: "1.0.0"8 language: en9 category: 05-engineering10 role: developer11 area: docs12 title: "Write code documentation"13 related: "readme-writing, api-reference-docs, clean-code-review, code-explanation, legacy-code-comprehension"14 prompt: "Add proper documentation to this pricing module. Keep it useful; I do not want comments that just repeat the code."15---1617# Write Code Documentation1819## Purpose20Make code safe to use and change by documenting what the code cannot say for itself: the contract a caller relies on, the constraints and invariants, and the reasons behind surprising decisions. Fewer, precise comments beat many that echo the code and rot.2122## When to use23- A public function, class, module or library API has no or weak doc comments.24- Existing comments are outdated, misleading or restate the code.25- Code with hidden business rules, workarounds or performance tricks is about to be handed over.2627## When not to use28- Project-level setup and usage. Use `readme-writing`.29- Endpoint-level reference for HTTP or message APIs. Use `api-reference-docs`.30- Understanding unfamiliar code before documenting it. Use `code-explanation` or `legacy-code-comprehension` first.3132## Inputs33Required:34- The code to document, with language and enough context to understand callers.3536Optional, improves quality:37- The team's doc comment convention (e.g., the language's standard docstring or doc-comment style) and whether docs are generated from comments.38- Background on business rules, tickets or incidents behind non-obvious code.39- Intended audience: internal maintainers or external library users.4041If the reason for a non-obvious decision cannot be derived from the code, do not invent it: write a `TODO(owner): explain why ...` or list it as an open question.4243## Process441. Determine the visibility of each element (public API, package-internal, private). Public API gets full contract docs; private code gets comments only where the why is non-obvious.452. Before commenting, look for things that should be code instead: unclear names, magic numbers, long functions. Propose the rename or extraction and skip the comment it would replace.463. For each public element, write a one-line summary in the language's convention that says what it does for the caller, in domain terms.474. Document the contract: parameters (meaning, units, allowed ranges, nullability), return value (including empty and not-found cases), errors or exceptions thrown and when, side effects (I/O, state, events), thread-safety and idempotency where relevant.485. Add constraints and invariants that callers or maintainers must preserve (ordering, precision, time zone, rounding, performance limits).496. For non-obvious implementation choices, add a short why comment at the exact line: business rule, regulatory reason, bug workaround with its reference, or measured performance trade-off.507. Add a short usage example for public APIs whose correct use is not obvious; keep it compilable in principle.518. Remove or rewrite comments that restate the code, are wrong, or reference things that no longer exist; mark deprecated elements with the replacement and removal plan.529. Label every stated reason you inferred from the code as `[ASSUMPTION]` in your response so the author can confirm it before committing.5310. Present the result as the documented code (or a diff), followed by a short list of proposed code changes and open questions. If the user continues, suggest `api-reference-docs` for external API docs or `clean-code-review` for the naming and structure findings.5455## Output format56```markdown57## Documented Code58<code with doc comments and why-comments, in the language's convention>5960## Proposed Code Changes Instead of Comments61- <rename / extract constant / extract function> — <reason>6263## Removed or Corrected Comments64- <location>: <what was wrong or redundant>6566## Open Questions and Assumptions67- [ASSUMPTION] <inferred reason> — confirm with <owner>68```6970## Quality checklist71- [ ] Every public element states its contract: inputs, output, errors and side effects.72- [ ] No comment only restates what the next line of code says.73- [ ] Units, ranges, nullability and time zones are explicit wherever they matter.74- [ ] Each why-comment explains a reason a maintainer could not recover from the code, with a reference where one exists.75- [ ] Inferred reasons are flagged `[ASSUMPTION]`; nothing is presented as a fact without evidence.76- [ ] The comment style matches the language convention so documentation tools can parse it.77- [ ] All checks pass; if any fails, revise the output and re-run this checklist before answering.7879## Common pitfalls80- Documenting what the code does line by line. It doubles maintenance and becomes wrong at the next change; document intent and contract.81- Hiding a bad name behind a comment. Rename the thing instead.82- Writing "handles errors" without saying which errors, how they surface and whether the operation is retried.8384## Example85Input: `def price(q, c): return round(q * c.base * (0.9 if q >= 100 else 1), 2)`8687Weak: `# multiplies quantity by base price and applies discount`8889Strong:90```python91BULK_THRESHOLD = 100 # proposed: replaces magic number92BULK_DISCOUNT = 0.109394def price(quantity: int, catalog_item: CatalogItem) -> Decimal:95 """Return the net line price in the item's currency, rounded to 2 decimals.9697 Orders of BULK_THRESHOLD units or more get BULK_DISCOUNT.98 Quantity is expected to be >= 1; it is not validated here.99 """100```101- Open question: should `quantity < 1` raise an error? The current code silently returns 0 or a negative price.102- [ASSUMPTION] The 10% bulk rule comes from the commercial terms; confirm with the pricing owner.103- Proposed: use a decimal type and an explicit rounding mode instead of float `round`.