Keep agent-written code comments few, true and about why
Position: few comments, every one true, each saying why or what must not change. Not zero.
pstack’s no-comments skill takes the strict end: a reviewer agent deletes comments by default and keeps only a short list of exceptions (licence headers, behaviour forced by something outside your control, public API doc comments, justified lint suppressions, links to an issue or RFC). A comment explaining a surprise in your own code is treated as a sign the code should be renamed, typed or restructured instead.
The research is mixed but points the same way on the part that matters:
- Wrong comments hurt. Incorrect documentation greatly reduced LLM code understanding, while missing or incomplete documentation had little effect (Macke & Doyle, 2024). Comments generated from buggy code did not help bug-fixing models and could mislead them.
- Accurate, informative comments can help. In one bug-fixing study, comments raised fix accuracy up to about threefold; comments explaining how a method is implemented helped most, comments restating what it does helped least.
- Effect depends on task and model. Probing work found models treat comments as distinct concepts, with effects that helped some tasks and hurt others.
So the risk is not comments; it is comments that are stale, redundant or wrong. Agents produce those at volume.
-
MUST make the code carry its own meaning first: names, types, small functions, tests. If a comment is needed to explain your own code, first try to rename, extract, type or restructure so it is not.
-
MUST NOT write narration comments that restate the code (
// increment counter,// loop over users), section banners, or commented-out code. -
MUST NOT put change history in comments (
// updated to fix bug,// added by agent,// previously used X). That belongs in the commit message and PR (BP-0034). -
MAY keep comments in these cases only:
Keep Example Licence and provenance headers your project requires // SPDX-License-Identifier: MIT,// author: …Behaviour forced by something you cannot change Vendor API quirk, protocol rule, platform bug, with a link Public API contracts Docstrings/JSDoc for exported functions: inputs, outputs, errors, invariants The reason for a non-obvious decision Why this algorithm, why this limit, with a link to the issue, ADR or RFC Justified lint or type suppressions With the rule name and why it does not apply here -
SHOULD encode a “do not change” constraint as a type, a runtime check, a test or a lint rule where that is cheap; a test fails when the constraint is broken, a comment does not (BP-0037).
-
MUST keep comments true: when an agent changes code, it updates or deletes every comment and docstring that describes it, in the same change. A stale comment is a defect.
-
MUST NOT delete a comment you did not write without finding out why it is there: read the code around it, trace the named symbol, check history and linked issues. Delete only once the reason is gone or is now expressed in code.
-
SHOULD have the reviewer, not the author, judge comments: authors defend their own comments. A fresh-context reviewer checks each comment against the keep list and against the code it describes (BP-0036).
-
SHOULD state the comment policy in the repository’s agent instructions (AGENTS.md / CLAUDE.md) so every agent follows the same rule, and enforce what can be enforced with lint (for example, ban commented-out code or
@ts-ignorewithout a reason).
When to apply
Section titled “When to apply”Every change an agent writes or reviews in source code.
Examples
Section titled “Examples”Before (typical agent output):
// Function to get the user// Updated: now handles nullasync function getUser(id: string) { // Check if id is empty if (!id) return null; // Retry 3 times because the API is flaky return retry(() => api.users.get(id), 3);}After:
// author: Stephen Eatonconst USERS_API_MAX_ATTEMPTS = 3; // Vendor returns transient 502s under load: https://status.example.com/incidents/1234
async function findUser(id: string): Promise<User | null> { if (!id) return null; return retry(() => api.users.get(id), USERS_API_MAX_ATTEMPTS);}The narration and change note are gone; the name and return type say what the function does; the one comment left records a reason the code cannot express, with a link.
Agent-instruction snippet:
## Comments- Prefer names, types and tests to comments.- Keep only: licence/author headers, external constraints (with link), public API docs, reasons for non-obvious decisions, justified suppressions.- No narration, change notes, banners or commented-out code.- Update or delete comments in the same change as the code they describe.Anti-patterns
Section titled “Anti-patterns”- A comment on every line, restating the code.
// Changed by Claude to fix the null bugin source.- Deleting every comment in a legacy file, including the one explaining a vendor bug.
- A comment saying “do not change this” with no reason and no test.
- Comments that describe what the code did two versions ago.
Checklist
Section titled “Checklist”- Code made self-explanatory before any comment added
- No narration, banners, change notes or commented-out code
- Remaining comments fit the keep list and are true
- Constraints encoded in types, tests or lint where cheap
- Comments of unknown purpose investigated before deletion
- Policy in AGENTS.md / CLAUDE.md; reviewer checks comments