comment-code
FreeNo executable scriptsNot checkedWrite clear, useful in-code comments and documentation that capture what the code cannot say for itself. Use this skill whenever writing or revising comments…
by jd-solanki★ 162 GitHub starsAbout this skill
Writing Good Code Comments
Comments exist to capture information that was in the writer's mind but cannot be expressed in the code itself. Code states what happens mechanically; comments supply intent, contracts, rationale, units, invariants, and the higher-level picture. Good comments make a system understandable to someone reading it for the first time and let abstractions hide complexity. The cost is small — typically a fraction of development time — and it pays back every time someone (including the original author, weeks later) needs to read or change the code.
The one rule that governs everything
Comment things that aren't obvious from the code. Before keeping any comment, ask: Could someone who has never seen this code reconstruct this comment just by reading the code next to it? If yes, the comment adds nothing — delete it or replace it with something that does add information. "Obvious" means obvious to a first-time reader, not to you.
This single test eliminates most bad comments and motivates most good ones.
Don't repeat the code
The most common worthless comment simply restates the line below it, at the same level of detail.
i = i + 1 # increment i ← adds nothing
count = len(items) # get the number of items ← adds nothing
A close cousin is the comment that just recycles the words already in the name:
// Obtain a normalized resource name from the request.
String[] getNormalizedResourceName(Request req) { ... }
The only word here not already in the signature is "from." This tells the reader nothing they couldn't read off the declaration. The fix is to use different words than the name, words that explain the meaning the name only hints at: What is a normalized resource name? What are the elements of the returned array? When in doubt, the giveaway is one-comment-per-line at the same granularity as the code.
Comments work at a different level of detail than the code
A comment at the same level as the code repeats it. Useful comments go either lower (more precise) or higher (more abstract) than the code.
Lower-level comments add precision
Most valuable on declarations — fields, arguments, return values — where a name and type leave real questions open. Fill in what the type can't say:
- Units — pixels? milliseconds? bytes?
- Boundaries — is the range inclusive or exclusive of the endpoints?
- Nulls — is null allowed, and what does it mean?
- Ownership — for a resource that must be freed/closed, who is responsible?
- Invariants — properties always true, e.g. "always contains at least one entry."
// Vague:
uint32_t offset; // current offset in buffer
// Precise:
// Position in this buffer of the first byte that has not yet
// been returned to the client.
uint32_t offset;
When documenting a variable, think nouns, not verbs: describe what it represents, not how various pieces of code modify it. A comment that narrates "set to true here, reset to false there" mirrors the code structure and grows stale; a comment stating the meaning ("true means a heartbeat has arrived since the timer was last reset") is shorter, more stable, and lets the reader infer the manipulation.
Higher-level comments enhance intuition
These omit detail to convey overall intent — what the code is for and how it fits the surrounding method. Common for blocks inside methods, loops, and interface comments.
// Too low-level — partly repeats the conditions below:
// If there is a LOADING rpc with the same session whose last
// hash is smaller than the current one, use that rpc.
// Higher-level — states the purpose:
// Try to append this key hash to an existing, not-yet-sent RPC
// bound for the right server.
With the high-level version a reader can predict almost everything the loop does and can judge whether the code is correct — something a detail-echoing comment never enables. Higher-level comments are harder to write because they require stepping back from detail. Ask: What is this code trying to do? What is the simplest thing that explains all of it? What is the most important thing about it?
A specially useful higher-level form is "how we get here" — the conditions under which a block or method runs, especially for code that only executes in unusual situations.
Comment categories — and which matter most
| Category | Where | Priority |
|---|---|---|
| Interface | Block immediately before a class/method/function declaration; defines the abstraction | Highest |
| Data-structure member | Next to a field/variable declaration | Highest |
| Implementation | Inside a method body | Often unnecessary |
| Cross-module | Describes dependencies spanning module boundaries | Rare but critical |
Every class should have an interface comment, every class variable a comment, and every method an interface comment. The occasional declaration is so self-evident (a trivia
Install comment-code in Claude Code & Claude Desktop
Sign up to install this skill
Create a free account to reveal the install command and save the skill to your library.
- Reveal the one-line install command
- Save skills to your synced library
- Get notified when skills update
Allowed tools
Tools this skill is permitted to call.
No restriction — this skill can use any tool.
FAQ
What does the comment-code skill do?
Write clear, useful in-code comments and documentation that capture what the code cannot say for itself. Use this skill whenever writing or revising comments, docstrings, interface/API documentation, function or class headers, or inline explanations — and whenever reviewing code for comment quality or being asked to "document," "add comments," "explain in comments," or improve existing comments. Apply it even when the request only mentions writing the code, since fresh code is the moment to document it well.
How do I install the comment-code skill?
Copy the skill folder into ~/.claude/skills (the Claude Code tab above does this in one command), or install it as a plugin.
Does the comment-code skill run scripts?
No, this skill is instructions only (SKILL.md) with no executable scripts.
Related skills
docx
Use this skill whenever the user wants to create, read, edit, or manipulate Word documents (.docx files) or Word templates (.dotx files). Triggers include…
by AnthropicUse this skill whenever the user wants to do anything with PDF files. This includes reading or extracting text/tables from PDFs, combining or merging multiple…
by Anthropicxlsx
Use this skill any time a spreadsheet file is the primary input or output. This means any task where the user wants to: open, read, edit, or fix an existing…
by Anthropicpptx
Use this skill any time a .pptx or .potx file is involved in any way — as input, output, or both. This includes: creating slide decks, pitch decks, or…
by Anthropic