Code comments
How to write a comment in Go, TypeScript, shell scripts, Makefiles, Dockerfiles and CI YAML.
A comment has two readers. A developer new to the code understands it on first read, without opening another file. A maintainer finds the rules and traps a change could break.
The writing rules apply too, above all: no history, no capitals for emphasis. Only review checks this page, with the check below. Older comments do not all follow it.
The rules
Section titled “The rules”- Open with what the code does, then say why. The first words say what the lines below do or decide, in plain words: “Refuse a key Postgres can’t store”. Then the reason: the rule the code keeps or the trap it avoids. Leave out what the line already says (
i++ // add one). - Write it to be read once. Short sentences, one idea each. Several cases are a list, one line per case, in the order the code handles them. Name the thing itself, such as the function, the number or the type, not a phrase the reader has to decode (“the reserve”, “counted the same way”).
- A long comment means the code needs work. If a guard, fallback or workaround takes many lines to explain, remove the second path or fix the cause, and most of the comment goes too.
- Keep it short, as a senior engineer would write it. Say what the code does and why in the fewest lines that still say it, and do not narrate how the code runs. Most comments are one to three lines, wrapped at about 80 columns. A doc comment over about 15 lines, or a comment inside a function over about 8, needs a reason. Design discussion, rejected options and plans go in a decision record (an ADR,
docs/). Short never drops a rule or a trap the maintainer needs. When a comment cannot be both short and complete, keep the rule and say it in fewer words.adr/ - A doc comment tells the caller what they must know: what the function returns or does, its preconditions, its errors, whether it uses the caller’s transaction and whether concurrent calls are safe. Every exported name and package has one, in Go Doc Comments form.
- In TypeScript, an exported function, hook or component gets a
/** */comment (TSDoc) when its name and types do not say enough. - State rules, not counts or call sites. “Every service guard calls this” stays true. “The seven call sites” is wrong once an eighth is added.
- A wrong comment is a bug. Change the comment with the code: update or delete it in the same commit.
- Leave tool markers as they are:
//go:directives,eslint-disable,@ts-expect-error,# shellcheckand//.nolint
To reword or fix existing comments, see Changing existing text.
The check
Section titled “The check”Before a comment merges, its author reads it once as a Go developer who has never opened the file, with only the comment and the code below it in view. The reviewer does the same. Someone who has read the rest of the file already knows its words, so hide the rest of the file while you read. Rewrite the comment if either of them:
- read a sentence twice;
- had to open another file, or scroll, to know what a word refers to;
- met a word the file made up (“the owner”, “the reservation”) where the thing itself could be named (“the request holding the key”);
- could not say, after the first line, what the code below does;
- found a line that could go without losing a rule or a trap.
A comment that is correct and still fails this check is not done.
Examples
Section titled “Examples”History and capitals where the reason belongs (adapted from cmd/):
// Before// Every service below is constructed unconditionally by api.NewServer —// a nil can only mean a broken refactor. Wire unconditionally: the// scheduler's own tick-time nil-guards are a seam for TEST// configurations, and hiding a wiring bug behind them would skip a// money-path worker silently, forever. (Seven dead nil-guards here did// exactly that in theory until the 2026-08-11 module audit.)
// After// api.NewServer always builds these services, so wire them without nil// checks. A nil check here would hide a wiring bug by turning off a// money-path worker without a word.Correct, but too dense to read once (from internal/):
// Before// A key the reserve cannot store would get the 503 on every retry: one// longer than an event's idempotency_key may be, counted the same way, or// one that isn't UTF-8 (net/http accepts any byte from 0x80 up in a// header), which Postgres text refuses.
// After// Refuse a key Postgres can't store; otherwise reserveKey fails and the// client gets a 503 "retry" that fails the same way every time:// - over 255 characters (the cap on a usage event's idempotency_key);// - not valid UTF-8 (Go accepts raw bytes in a header; Postgres text doesn't).The second version opens with what the code does, gives each case its own line, and names the number and the function.
As short as the rule allows (same file):
// Before// An orphaned reservation never gets an answer, so a retry with the key// would never learn what its owner did.
// After// Tell the client the outcome is unknown when the row has been pending// past idempotencyOrphanAfter: the first request, which holds the key,// died or could not store its answer. The client checks whether the// change happened, and retries with a new key only if it did not.The second version opens with what the code does, names the constant, and says “the first request, which holds the key” where the first said “owner”. It is two lines longer because it keeps the rule a maintainer needs: the client retries only if the change did not happen. A two-line draft of this comment said the request “died and will never answer” and “retry with a new key”. It was shorter, and it was not true of the code.
Stuck, or found a bug? Ask in Discussions · Open an issue