writing-code-comments
Skill này làm gì
Skill này hướng dẫn AI viết ít chú thích mã hơn nhưng tốt hơn. Nó dành cho lập trình viên muốn chú thích giải thích lý do mà người đọc không thể thấy trong chính mã.
Khi nào nên dùng
- ✓Khi bạn chuẩn bị thêm chú thích vào mã bằng bất kỳ ngôn ngữ lập trình nào.
- ✓Khi bạn đang xem xét một thay đổi có thêm hoặc sửa chú thích.
- ✓Khi một chú thích chỉ lặp lại mã hoặc mô tả các phiên bản cũ, và bạn muốn dọn dẹp nó.
Liên kết skill
https://github.com/PostHog/posthog/blob/master/.agents/skills/writing-code-comments/SKILL.mdSao chép rồi dán vào AI của bạn (Claude, ChatGPT hoặc Cursor) và nhờ nó làm theo hướng dẫn.
Cách dùng
Cách dễ nhất: sao chép tin nhắn và dán vào AI của bạn. AI sẽ đọc skill và làm theo hướng dẫn.
Tin nhắn có sẵn để dán
Dành cho Claude Code, Codex hoặc Cursor: chạy lệnh trong thư mục dự án của bạn và skill sẽ được cài đặt.
Tải tệp SKILL.md về và thả vào thư mục skill của agent.
Ví dụ cách nhờ AI
“Tôi có nên thêm chú thích ở đây không? Giúp tôi chỉ giữ những chú thích giải thích lý do.”
5 skill mới mỗi tuần. Đọc chỉ 1 phút.
Cách hoạt động
Trước khi viết chú thích, skill hỏi chú thích cho người đọc biết điều gì mà mã không cho biết. Nếu câu trả lời chỉ là lặp lại mã, skill xóa chú thích và đề xuất tên biến rõ ràng hơn.
Nội dung kỹ thuật (SKILL.md gốc)
Lấy nguyên văn từ GitHub, do tác giả viết bằng tiếng Anh.
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 counterabovecounter += 1 - ❌
// loop over usersabovefor user of users - ❌
# return the resultabovereturn 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 migrationleft 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 buildwhen the durable fact is that the build is expensive - ❌
# ci-backend runs ~28m, so 60m ≈ one red resultinstead of "sized past a full run of the slowest workflow" - ❌
# no story currently opts into webkit snapshotswhere dropping "currently" states the same fact - ❌
# ~20 minutes in June, past 25 by Julybecause 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 the300(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."""onget_user_by_id - ❌
# type: stringon 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
Câu hỏi thường gặp
Skill có nói rằng đừng bao giờ viết chú thích không?
Không. Skill giữ các chú thích giải thích lý do, chẳng hạn một ràng buộc ẩn hoặc một phép đổi đơn vị, và xóa phần còn lại.
Lịch sử thay đổi nên ghi ở đâu?
Trong thông điệp commit và mô tả pull request, nơi nó gắn liền với thay đổi. Chú thích về phiên bản cũ trong mã nguồn nhanh chóng trở nên lỗi thời.
Skill có hoạt động với mọi ngôn ngữ lập trình không?
Có. Cùng quy tắc áp dụng cho các ngôn ngữ như Python, TypeScript, Go, Rust và SQL.
Chi tiết
Mô tả gốc của tác giả
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
Độ phổ biến
- Sao GitHub
- 41k
- Vị trí xếp hạng
- #6819
- Giấy phép
- —
- Cập nhật
- 2/11/2026
Bình luận
- …
Thêm skill từ kho mã này
depot-ci
41kGiúp agent của bạn thiết lập và chạy quy trình Depot CI, một lựa chọn thay thế GitHub Actions, gồm chuyển các quy trình hiện có và đọc nhật ký chạy.
depot-container-builds
41kGiúp agent của bạn build image Docker trên các builder từ xa của Depot, gồm image đa nền tảng, đẩy lên registry và sửa lỗi build.
depot-github-runners
41kGiúp agent của bạn chuyển các job GitHub Actions sang runner do Depot quản lý, chọn kích thước và nhãn runner, và xử lý lỗi thiết lập runner.
editing-agents-md
41kGiúp agent của bạn quyết định một quy tắc nên nằm trong file AGENTS.md hay CLAUDE.md, và viết sao cho quy tắc được tuân thủ mà file không bị phình to.
Skill tương tự
diffs
394kGiúp agent của bạn tạo một diff có thể chia sẻ về các chỉnh sửa, dưới dạng liên kết xem hoặc file ảnh, thay vì một bản tóm tắt viết tay.
coding-agent
394kGiúp agent của bạn giao một việc lập trình lớn cho Codex, Claude Code hoặc OpenCode chạy nền, rồi theo dõi tiến độ của nó.
gh-issues
394kGiúp agent của bạn tìm issue trên GitHub, chọn những issue cần xử lý và mở pull request với bản sửa, kể cả theo dõi nhận xét đánh giá.