Code comments
The best comment is often a better name. Reach for a comment only when the code cannot carry the meaning on its own. A comment that restates the code adds maintenance cost and drifts out of date; a comment that explains why earns its place.
When to use this skill
- Writing a comment or a documentation block.
- Reviewing a diff for comment quality (too few, too many, or stale).
- Documenting a public API.
- Cleaning up noisy, redundant, or commented-out code.
When a comment earns its place
Try self-documenting code first: a precise name, a smaller function, a named constant, an early return. Add a comment when:
- The why is not obvious: a non-obvious rationale, a tradeoff, a workaround for an upstream bug, a performance or security reason.
- The behaviour is surprising: an ordering constraint, a side effect, a deliberate deviation from the obvious approach.
- It is a public API contract: callers need the parameters, return values, errors, and invariants without reading the body.
- It is legal or license boilerplate the project requires.
Do not comment the obvious. i += 1 # increment i is noise.
Explain why, not what
The code already says what it does. The comment says what the code cannot:
# Bad: restates the code
# loop over users and send email
for user in users:
send_email(user)
# Better: explains the non-obvious why
# Vendor API rate-limits at 10 req/s; batching avoids the 429 retry
# storm we hit in INC-204.
for batch in chunked(users, 10):
send_batch(batch)
Doc comments versus inline notes
- Public API: use the language's documentation format (docstring, JSDoc/TSDoc, godoc, rustdoc, Javadoc) so generated docs and editor tooltips work. See references/doc-formats.md.
- Implementation notes: a terse inline comment sits next to the tricky line, not in a header block far from the code it describes.
Keep comments close and current
A wrong comment is worse than no comment, because the reader trusts it. When you change code, update or delete the comment in the same edit. Delete commented-out code: version control already remembers it, and dead code in comments rots silently.
TODO and FIXME markers
Use a consistent marker (TODO, FIXME, HACK, XXX) and attach an owner or an issue link so it can be found and closed:
# TODO(JYL-512): replace with the streaming parser once it ships
A TODO with no link and no owner is a comment that will never be removed.
Anti-patterns
- Redundant comments that restate the code.
- Commented-out code left in the file.
- Stale comments that no longer match the code.
- Decorative banners and divider lines (rows of
#, *, or =) used as section separators; prefer a blank line or a short heading comment.
- Comments that apologise for a bad name: rename the symbol instead.
AI-specific pitfalls
Models tend to over-comment: a comment on every line, a docstring that repeats the signature, narration of the obvious. Rules when generating comments:
- Add a comment only when it tells the reader something the code cannot.
- Do not annotate self-evident lines.
- For a doc comment, describe the contract (params, returns, errors, invariants), not a paraphrase of the body.
- Match the file's existing comment density and style. A sparsely commented file should stay sparse.
Relationship to the language skills
This skill is the cross-cutting philosophy. For language-specific syntax and conventions (docstring style, JSDoc tags, godoc rules, rustdoc doctests), use the matching language skill and references/doc-formats.md.
1---2name: code-comments3description: Write and review code comments: when a comment earns its place versus self-documenting code, explaining why and intent rather than restating what, doc comments (docstrings, JSDoc/TSDoc, godoc, rustdoc, Javadoc) versus inline notes, TODO and FIXME conventions, avoiding comment rot and commented-out code, and the AI tendency to over-comment. Use when writing or reviewing comments, documenting an API, or cleaning up noisy or redundant comments.4---56# Code comments78The best comment is often a better name. Reach for a comment only when the code cannot carry the meaning on its own. A comment that restates the code adds maintenance cost and drifts out of date; a comment that explains why earns its place.910## When to use this skill1112- Writing a comment or a documentation block.13- Reviewing a diff for comment quality (too few, too many, or stale).14- Documenting a public API.15- Cleaning up noisy, redundant, or commented-out code.1617## When a comment earns its place1819Try self-documenting code first: a precise name, a smaller function, a named constant, an early return. Add a comment when:2021- The **why is not obvious**: a non-obvious rationale, a tradeoff, a workaround for an upstream bug, a performance or security reason.22- The behaviour is **surprising**: an ordering constraint, a side effect, a deliberate deviation from the obvious approach.23- It is a **public API contract**: callers need the parameters, return values, errors, and invariants without reading the body.24- It is **legal or license** boilerplate the project requires.2526Do not comment the obvious. `i += 1 # increment i` is noise.2728## Explain why, not what2930The code already says what it does. The comment says what the code cannot:3132```python33# Bad: restates the code34# loop over users and send email35for user in users:36 send_email(user)3738# Better: explains the non-obvious why39# Vendor API rate-limits at 10 req/s; batching avoids the 429 retry40# storm we hit in INC-204.41for batch in chunked(users, 10):42 send_batch(batch)43```4445## Doc comments versus inline notes4647- **Public API:** use the language's documentation format (docstring, JSDoc/TSDoc, godoc, rustdoc, Javadoc) so generated docs and editor tooltips work. See [references/doc-formats.md](references/doc-formats.md).48- **Implementation notes:** a terse inline comment sits next to the tricky line, not in a header block far from the code it describes.4950## Keep comments close and current5152A wrong comment is worse than no comment, because the reader trusts it. When you change code, update or delete the comment in the same edit. Delete commented-out code: version control already remembers it, and dead code in comments rots silently.5354## TODO and FIXME markers5556Use a consistent marker (`TODO`, `FIXME`, `HACK`, `XXX`) and attach an owner or an issue link so it can be found and closed:5758```59# TODO(JYL-512): replace with the streaming parser once it ships60```6162A TODO with no link and no owner is a comment that will never be removed.6364## Anti-patterns6566- **Redundant comments** that restate the code.67- **Commented-out code** left in the file.68- **Stale comments** that no longer match the code.69- **Decorative banners and divider lines** (rows of `#`, `*`, or `=`) used as section separators; prefer a blank line or a short heading comment.70- **Comments that apologise for a bad name:** rename the symbol instead.7172## AI-specific pitfalls7374Models tend to over-comment: a comment on every line, a docstring that repeats the signature, narration of the obvious. Rules when generating comments:7576- Add a comment only when it tells the reader something the code cannot.77- Do not annotate self-evident lines.78- For a doc comment, describe the contract (params, returns, errors, invariants), not a paraphrase of the body.79- **Match the file's existing comment density and style.** A sparsely commented file should stay sparse.8081## Relationship to the language skills8283This skill is the cross-cutting philosophy. For language-specific syntax and conventions (docstring style, JSDoc tags, godoc rules, rustdoc doctests), use the matching language skill and [references/doc-formats.md](references/doc-formats.md).