跳到正文
Skillpedia
通用编程41k · #6819

writing-code-comments

它能做什么

本技能指导 AI 编写更少但更好的代码注释。适合希望注释能解释读者无法从代码本身看出的原因的开发者。

适用场景

  • ✓当你准备在任何编程语言的代码中添加注释时。
  • ✓当你审查新增或修改了注释的改动时。
  • ✓当某条注释只是重复代码或描述旧版本,你想把它清理掉的时候。

技能链接

https://github.com/PostHog/posthog/blob/master/.agents/skills/writing-code-comments/SKILL.md

复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。

使用方法

最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。

可直接粘贴的消息

请用这个技能帮我:https://github.com/PostHog/posthog/blob/master/.agents/skills/writing-code-comments/SKILL.md 阅读该链接中的 SKILL.md 文件,并在我的任务中按照它的说明来做。
想让你的 AI 自己找到这样的技能吗?安装免费的 Skillpedia MCP:只需一行,每次任务前它都会在 15,000+ 个技能中搜索。1 分钟完成安装 →

可以这样问 AI

“这里需要加注释吗?请帮我只保留解释原因的注释。”

每周 5 个新技能,1 分钟读完。

工作原理

在写注释之前,它会先问:这条注释能告诉读者哪些代码本身无法说明的内容。如果答案只是重复代码,它就会删除该注释,并建议使用更清晰的命名。

技术内容(原始 SKILL.md)

作者在 GitHub 上的英文原文。

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

Perishable measurements and current-state stamps

Measured timings, counts, and rates rot silently: nothing forces them to update, and a rotted number misleads the next person sizing a timeout or shard count. The same goes for "currently" / "today" hedges, because the sentence states the same fact without them. State the durable relationship the number stood for.

  • ❌ # skip the ~20 min build when the durable fact is that the build is expensive
  • ❌ # ci-backend runs ~28m, so 60m ≈ one red result instead of "sized past a full run of the slowest workflow"
  • ❌ # no story currently opts into webkit snapshots where dropping "currently" states the same fact
  • ❌ # ~20 minutes in June, past 25 by July because trend narration is change history

Numbers that stay:

  • A dated snapshot: # as of August 2024, Homebrew ships 4.13.2 (the date makes staleness visible)
  • A restated adjacent code literal: # runs that took >5 min (300 seconds) beside the 300 (it updates with the code)
  • A platform constant: # GitHub's comment size limit (~64KB)
  • A target or budget: # Target: ~15 min per shard (policy, not measurement)
  • Cited evidence: # 30% peak memory observed on 16-core runs (#46853) (the link dates it)

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.
  • Use mostly ASD-STE100 Simplified Technical English. Use active voice, simple tenses, one idea per sentence, and consistent terms.
  • Let length follow the content. One line is fine when one line covers it; use more when the reasoning needs more. Neither brev
常见问题

这项技能是否规定永远不写注释?

不是。它会保留解释原因的注释,例如隐藏的约束或单位换算,并删除其余注释。

改动的历史应该写在哪里?

写在提交信息和拉取请求描述中,这样它就与改动保持关联。源代码中关于旧版本的注释很快就会过时。

它适用于所有编程语言吗?

适用。同样的规则适用于 Python、TypeScript、Go、Rust 和 SQL 等语言。

详情

作者的原始描述

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. Removes the comment types that clutter the codebase: narration that restates the code, change-history and chat-context notes ("previously did X", "per PR #123", "AI:"), perishable measurements and current-state stamps ("~20 min build", "currently", "today"), commented-out code, and redundant docstrings. Keeps the ones that earn their place: a non-obvious

repo: PostHog/posthog

热度

GitHub 星标
41k
排名
#6819
许可证
—
更新时间
2026/11/2

这个技能有用吗?

0

登录 后即可投票。

评论

  • …

同一仓库的更多技能

相似技能