Writing effective project documentation — READMEs, API doc comments, and inline comments that explain why. Use when writing or improving documentation — READMEs, doc comments, inline comments, or when code lacks explanation for non-obvious behavior.
Good documentation answers the questions a reader actually has: what is this, how do I use it, why does it work this way. Identify the audience first, then write the smallest doc that serves them. Doc comments live with the code; READMEs live with the project.
Decide before writing whether the reader is a developer integrating the API, an end-user running the tool, or a future maintainer reading the source. Each gets a different doc.
For a project README:
Every public function gets a doc comment. Include:
Use inline comments sparingly:
Keep docs close to code. Doc comments beat wiki pages beat external sites. The further docs travel from the code, the faster they go stale.
Agent-specific failure modes — provider-neutral pause-and-self-check items:
# increments i next to i += 1 adds noise, not signal. Comments explain why — the business rule, the workaround, the non-obvious constraint — not what, which the code already shows./// Calculates the shipping cost based on weight and destination zone.
///
/// Returns `None` if the destination zone is not serviceable.
/// Weights over 30kg use freight pricing instead.
The example is short, names the inputs and outputs, and surfaces
the two non-obvious rules (unservicable zones return None,
heavy items switch to freight).
// increments i next to i += 1.A function with a self-evident name and a short body usually needs no doc comment. Save the words for the parts where the reader might be surprised.