mcpbeat Sign in

Writing Code Comments Agent Skill

> 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.

1k tokens
context cost
the whole folder, loaded on every use
1
files
instructions only
0
copies elsewhere
how many repositories repackaged it
690
stars on the repo
on the repository, not the skill itself

Install

one command, takes just this skill from the repository
npx skills add https://github.com/PostHog/posthog --skill writing-code-comments

The instruction itself

10 sections, as written by the author

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.

The gate: one question

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

Delete these

Narration that restates the code

  • # increment the counter above counter += 1
  • // loop over users above for user of users
  • # return the result above return result

If a block needs narration to be followed, the fix is smaller functions and better names, not a comment.

Change history and chat context

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 migration

Commented-out code

Delete 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.

Redundant docstrings and type restatements

  • ❌ A docstring that repeats the function name in prose: """Gets the user by id.""" on get_user_by_id
  • # type: string on an already-typed field
  • ❌ Python test doc comments (the repo convention is none; the test name says it)

Keep these

  • A why that isn't obvious from the code: a workaround, a performance trade-off, a spec quirk, an ordering constraint.
  • A warning about a consequence that lives elsewhere: "changing this breaks the cache key", "callers rely on this being sorted".
  • A pointer to context a reader can't reconstruct from the repo: a link to the spec, ticket, or the reason a surprising value was chosen.

Style

Write 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.

  • Be explicit and technical. State the cause and effect. Name the actual conditions, values, and consequences. A reader should not have to reconstruct your reasoning from a hint.
  • Let length follow the content. One line is fine when one line covers it; use more when the reasoning needs more. Neither brevity nor length is the goal.
  • No em-dash. The tell to avoid is the clipped two-part phrase joined by a dash, like # do the thing — it's faster. Use a real connective instead ("because", "so that", "which means", "to avoid").
  • Explain why, not what. The what is in the code; the why usually is not.
  • Preserve existing comments when moving or refactoring code, unless the change makes them wrong. Don't drop an existing why just because you're relocating the function.
  • Match the surrounding density. Don't add a comment to every line of a file that had none; don't strip a well-commented module bare.

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_organizationmembership

When you're tempted to comment

Try, 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.

Other skills for the same job

different authors, same section of the catalogue
Changelog Generator
by frostant
×9

Automatically creates user-facing changelogs from git commits by analyzing commit history, categorizing changes, and transforming technical commits into clear, customer-friendly release notes. Turns hours of manual changelog writing into minutes of automated generation.

774 tokens
Codex
by softaworks
×2

Use when the user asks to run Codex CLI (codex exec, codex resume) or references OpenAI Codex for code analysis, refactoring, or automated editing. Uses GPT-5.2 by default for state-of-the-art software engineering.

2k tokens
Memory Safety Patterns
by ComeOnOliver
×2

Implement memory-safe programming with RAII, ownership, smart pointers, and resource management across Rust, C++, and C. Use when writing safe systems code, managing resources, or preventing memory bugs.

6k tokens
Pysam
by K-Dense-AI
×1

Python/HTSlib workflows for genomic files. Use when reading, querying, filtering, or writing SAM/BAM/CRAM, VCF/BCF, FASTA/FASTQ, or tabix data with pysam, including pileup, coverage, indexing, and CRAM references.

34k tokens scripts
Scientific Critical Thinking
by K-Dense-AI
×1

Evaluate scientific claims and evidence quality. Use for assessing experimental design validity, identifying biases and confounders, applying evidence grading frameworks (GRADE, Cochrane Risk of Bias), or teaching critical analysis. Best for understanding evidence quality, identifying flaws. For formal peer review writing use peer-review.

26k tokens
Gh Fix CI
by openai
vendor ×1

Use when a user asks to debug or fix failing GitHub PR checks that run in GitHub Actions; use `gh` to inspect checks and logs, summarize failure context, draft a fix plan, and implement only after explicit approval. Treat external providers (for example Buildkite) as out of scope and report only the details URL.

8k tokens scripts
Declarative Agent Developer
by microsoft
vendor ×1

> Create, build, deploy, and localize declarative agents for M365 Copilot and Teams. USE THIS SKILL for ANY task involving a declarative agent — including localization, scaffolding, editing manifests, adding capabilities, and deploying. Localization requires tokenized manifests and language files that only this skill knows how to produce. "scaffold an agent", "new agent project", "add a capability", "add a plugin", "configure my agent", "deploy my agent", "fix my agent manifest", "edit my agent", "localize my agent", "add localization", "translate my agent", "multi-language agent", "add an API plugin", "add an MCP plugin", "add OAuth to my plugin", "review instructions", "improve instructions", "fix my instructions"

66k tokens
Documentation
by lingxling
×1

Documentation generation workflow covering API docs, architecture docs, README files, code comments, and technical writing.

1k tokens

How to use it

Copy the folder

Take posthog/writing-code-comments from the repository into ~/.claude/skills for personal use, or into .claude/skills inside a project.

Check the name does not clash

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.