|
| 1 | +--- |
| 2 | +name: comment-style |
| 3 | +description: > |
| 4 | + Guidance for writing precise, useful code comments in any language — inline comments, |
| 5 | + docstrings, and block comments. Decides when a comment earns its place and what it |
| 6 | + should say: explain the non-obvious why or what, never restate the code, narrate |
| 7 | + history, or point at sibling code. Use when writing or editing comments, adding a |
| 8 | + docstring, deciding whether a comment is needed, reviewing comments in a diff, or |
| 9 | + cleaning up redundant, stale, or over-long comments. |
| 10 | +--- |
| 11 | + |
| 12 | +# Comment Style |
| 13 | + |
| 14 | +A comment must add information that is not already in the code. If deleting the comment |
| 15 | +loses no information, delete it. Default to no comment. |
| 16 | + |
| 17 | +Exception: doc comments a language's conventions require — Go godoc on exported |
| 18 | +identifiers, for example — stay even when they restate the signature. |
| 19 | + |
| 20 | +## When to Use This |
| 21 | +- Writing or editing inline comments, docstrings, or block comments |
| 22 | +- Deciding whether a line or block needs a comment at all |
| 23 | +- Reviewing a diff and judging whether its comments earn their place |
| 24 | +- Cleaning up comments that restate the code, narrate history, or sprawl too long |
| 25 | + |
| 26 | +## The test (in order) |
| 27 | +1. **Information, not narration.** Does the comment state a fact the code can't show on its |
| 28 | + own — an invariant, constraint, non-obvious consequence, unit, or reason? If not, cut it. |
| 29 | +2. **The fact, not the story.** State what is true *now*, in as few words as it takes. |
| 30 | + No "previously…", "used to…", "now we…" — history lives in git. A counterfactual is |
| 31 | + fine when it *is* the reason ("without this, retries share one deadline"); not when |
| 32 | + it recounts the edit that introduced the line. |
| 33 | +3. **This code, not other code.** Don't describe sibling code, UI, or behavior enforced |
| 34 | + elsewhere ("Mirrors the …") — that goes stale and says nothing about this line. |
| 35 | +4. **Why over what.** Prefer explaining *why*; a genuinely non-obvious *what* (surprising |
| 36 | + return value, silent edge case) also qualifies. |
| 37 | + |
| 38 | +## Precise means |
| 39 | +One sentence carrying the load-bearing fact. When a comment sprawls, find the single thing |
| 40 | +a future reader actually needs and keep only that. |
| 41 | + |
| 42 | +A PR or issue ref as a pointer is fine (`# … (#5765)`) — but it supplements the fact, it |
| 43 | +does not replace stating it. |
| 44 | + |
| 45 | +## Examples |
| 46 | +Bad — resemblance, no information about this code: |
| 47 | +`# Mirrors the deletability re-check pending box` |
| 48 | + |
| 49 | +Bad — history and justification burying one fact: |
| 50 | +`# Without this the analytics preflight only ran from the client-triggered background` |
| 51 | +`# check, so an operator who navigated away before the initiate response landed got a` |
| 52 | +`# plan with no analytics check recorded...` |
| 53 | + |
| 54 | +Good — same fact, stated precisely (ref kept as a pointer): |
| 55 | +`# Record the analytics preflight here too — the post-initiate auto-run misses it` |
| 56 | +`# when the client navigates away before the initiate response lands (#5765).` |
| 57 | + |
| 58 | +Good — non-obvious what: |
| 59 | +`// CommitsCount returns -1 when the trail has no commits yet.` |
0 commit comments