posthog/writing-code-comments
> Gates whether a code comment should exist and forces the ones that stay to explain why, not what. Use ALWAYS before writing or editing a comment in any language (Python, TypeScript, Go, Rust, SQL), and when reviewing a diff that adds comments. Not for user-facing copy (see `/writing-user-facing-copy`) or commit messages.
npx skills add https://github.com/PostHog/posthog --skill writing-code-comments
Run this before adding or editing any comment. The default is no comment. Good code with clear names carries most of its meaning on its own; a comment earns its place only when it tells a reader something the code cannot.
Before writing a comment, answer:
> What does this tell a future reader that the code itself doesn't?
If the answer is "it restates what the code does", delete it. Rename the variable or extract a function instead.
A comment worth keeping answers a _why_ the code can't:
# ATOMIC_REQUESTS is off, so wrap the two writes that must commit together// Stripe sends the amount in cents; the rest of our system uses dollars# Kept in sync with the enum in migrations/0042; update both# increment the counter above counter += 1// loop over users above for user of users# return the result above return resultIf a block needs narration to be followed, the fix is smaller functions and better names, not a comment.
Never record how the code got here. That belongs in the commit message and PR description, where it's attached to the diff and searchable. In the source it's noise that goes stale immediately.
# previously used a set here, switched to a list for ordering// per PR #1234 / # as discussed / # changed because the old way broke# AI: generated this helper / // agent: refactored# TODO(2024-01): remove after migration left in long after the migrationDelete it; the version history has it if it's needed again. Commented-out code is ambiguous to the next reader, who can't tell whether it's a note, a rollback plan, or an accident.
"""Gets the user by id.""" on get_user_by_id# type: string on an already-typed fieldWrite comments the way you'd write technical documentation: explicit and precise. State the reasoning so the reader does not have to infer it. Length is not a target in either direction: don't clip a comment to look terse, and don't pad it to look thorough. Say what needs saying and stop.
# do the thing — it's faster. Use a real connective instead ("because", "so that", "which means", "to avoid").The fix for the em-dash is the connective, not more words. A short comment is fine once the dash is gone:
# batch here — avoids N+1# batch here to avoid an N+1 against posthog_organizationmembershipTry, in order: (1) a better name, (2) a smaller function, (3) a type. Reach for a comment only when none of those can carry the meaning.
Take posthog/writing-code-comments from the repository into ~/.claude/skills for personal
use, or into .claude/skills inside a project.
The agent identifies a skill by the name field in its header. Two skills with the
same name cannot sit side by side — one of them will be ignored.