Book Deep-Read · A Philosophy of Software Design · Chapter 13
A Philosophy of Software Design · Ch 13 · Comments Should Describe Things that Aren't Obvious from the Code · Ousterhout · 2nd ed 2021
The previous chapter said comments must be written. This one answers the next question: what should they say? The author's answer is a single rule: write only what you can't see by reading the code. Think of the note you leave a neighbor with your spare key. You don't write "this is a key." You write "the door sticks — lift it a little before you turn."
Lots of people write comments like "add one to the counter on the next line." Anyone reading the code can see that, so the sentence says nothing. Meanwhile the things that matter — does the count start at zero or one, where does it stop, why are we counting at all — are missing. The most common bad comment isn't wrong; it just repeats what the code already said.
The person who wrote the code knows it too well to tell "obvious to anyone" apart from "known only to me." So they restate the obvious and leave out the part only they know. Whoever comes next has to dig through other files, ask around, or guess.
Written this way, readers can use and change the code with confidence without turning it inside out. One honest cost: judging what isn't obvious means putting yourself in a stranger's shoes, and the author is usually the worst judge — often it takes a colleague reading it to find out.
A comment is worth exactly what it says that the code doesn't — go finer or go higher, but never sit at the same level as the code.
Want to see what "precision" and "intuition" comments each add, the five things an interface comment should cover, how a buffer-copy method should be documented, and how the Google, Linux and Python style guides back this chapter up? → Switch to Deep
You'd think a good comment explains the code; Ousterhout says a comment at the same level as the code is nearly worthless. A comment earns its keep by being lower-level than the code (adding precision: units, boundaries, what null means) or higher-level (adding intuition: what this block is for, and why) — and by keeping interface comments (for users) apart from implementation comments (for maintainers).
Chapter 13 is the second of the book's comment chapters (12–16): Chapter 12 argues why to write comments, this one says what to write, Chapter 14 covers naming, Chapter 15 says to write comments first, and Chapter 16 covers keeping them current as code changes. It is where Chapter 4's "deep modules" meets prose: a module is only truly deep if its interface comment is good. In practice it maps to API docs, docstring conventions, and the comment checks in code review.
We comment because a programming language is less expressive than the ideas in the programmer's head. Plenty of important information — why something is done this way, a variable's unit, a method's side effects — has no place to live in the code. Comments exist to carry it.
Yet a large share of real comments do the opposite: they restate the code. Such a comment costs the writer time, costs the reader time, and has to be kept in sync, all without adding information. Worse, it makes the spot look "already commented," so the information that's actually missing never gets written. This chapter gives a set of tests so every line of comment carries what code can't.
The author recommends agreeing on conventions up front: interface comments for every class and method, comments for every class member variable, implementation comments inside methods, and notes on cross-module design — where each goes and in what format (Javadoc, Doxygen, Go's godoc style and so on). Conventions tell readers where to look, and tell writers which slots they still owe.
The chapter's first red flag: Comment Repeats Code. The test is concrete: could someone who has never seen this code write the comment just from the code next to it? If so, it has no value. A common variant uses exactly the same words as the variable or method name, just strung into a sentence.
The fix: use different words. Don't repeat what the name already says; write what the name can't — the value's meaning, its limits, why it's needed.
These matter most on variable declarations: class member variables, method arguments and return values. Names and types are usually too vague. The typical gaps the author lists:
For variable comments the author's advice is to think nouns, not verbs: describe what the variable represents, not how the code manipulates it. Illustration: // current position in the response buffer is weaker than // offset of the next unread byte in the response buffer; equals length when fully read — the second says which byte and when we're done.
These go before a block of code and tell the reader what the block does as a whole, and why, so they can grasp the structure without reading every line. The book shows a block comment from real systems code: the original spelled out every condition (if some request is in a certain state and its last key is smaller than the current key, put the current key into it) — practically the code rewritten in English. The revision is one sentence, roughly: "try to append the current key onto an existing request to the same server that hasn't been sent yet." The reader instantly knows the purpose; the details are in the code.
The author's self-check questions: What is this code trying to do? What is the simplest thing I can say that explains everything in it? What is the most important thing about it? Engineers tend to be good at details; higher-level comments are harder because they require stepping outside the code.
The chapter's longest section. Its core principle: separate interface comments from implementation comments. An interface comment gives what you need to use a class or method and should not describe how it works inside. The second red flag targets exactly this: Implementation Documentation Contaminates Interface.
A class's interface comment gives a high-level description of the abstraction it provides. A method's interface comment usually covers:
The book's example is a copy method on a buffer class. In substance its comment says it copies a range of bytes from the buffer to an external location; offset is the index of the first byte to copy, length the number of bytes, and dest must have room for at least length bytes; the return value is the number of bytes actually copied — fewer than length if the range runs past the end of the buffer, and 0 if there is no overlap at all. That last part is exactly what callers trip over, and the signature can't express it.
The author's yardstick: a user should understand a module's abstraction from its externally visible declarations and interface comments alone. If they can't, either the comments are missing something or the interface itself is too complex.
The main job of implementation comments is to help readers understand what the code is doing, not how — the code shows how. Many short methods need none at all. In a long method, a one-line "what this block does" before each major block helps a lot; so does a note before a loop on what each iteration does.
Also write down why: if code that looks redundant exists to fix a bug or work around an odd problem, say so (with a bug number if there is one), or someone will later delete it as useless.
The hardest comments cover conventions that span modules — for example, adding a new status code also requires adding its text description to a table in another file. That dependency lives in no single module, so developers easily miss it. The author's remedies:
Table 1 · Four kinds of comment: audience, content, typical mistake
| Kind | Reader | Should say | Typical mistake |
|---|---|---|---|
| Interface | Users | Behavior, argument and return meaning, side effects, exceptions, preconditions | Leaking implementation (red flag); echoing the method name |
| Member / variable | Users and modifiers | Units, bounds, meaning of null, ownership, invariants (think nouns) | Describing how code manipulates it (thinking verbs) |
| Implementation | Modifiers | What each block does; why unusual code exists | Narrating "how," line by line |
| Cross-module | Future modifiers | "Change this, and you must also change that," with the reason | Skipping it because there's no obvious place |
Table 2 · Finer or higher: which to reach for
| Aspect | Lower level: precision | Higher level: intuition |
|---|---|---|
| Where | Variables, arguments, return values | Before code blocks; class and method overviews |
| Gap it fills | Details names and types can't carry | Intent you can't reassemble even after reading every line |
| Hard part | Thinking of every edge case | Stepping outside the code to find the simplest phrasing |
| Cost | Many details to keep in sync | Abstract wording can go vague; needs a reviewer's check |
One test: after writing a comment, ask "could someone who never saw this code write it from the code?" If yes, delete it or replace it with something finer or higher.
The chapter's "keep interface and implementation apart" and "say why, not how" appear nearly word for word as rules in mainstream style guides:
Evidence and sources · publicly verifiable
Day to day: when review turns up an i++ // increment i comment, cite the "Comment Repeats Code" red flag; when a public method doesn't document its return-value edge cases, ask for an interface comment. An interface comment is effectively the API contract, and once published it is costly to change.
① A comment is worth the information the code doesn't carry; one at the code's own level is nearly worthless.
② Red flag one, "Comment Repeats Code": if someone new could write it from the code, delete it; don't reuse the words in the name.
③ Lower level adds precision: units, inclusive bounds, meaning of null, ownership, invariants; think nouns for variables.
④ Higher level adds intuition: what this tries to do, the simplest phrasing, the most important thing.
⑤ Keep interface and implementation apart; red flag two, "Implementation Documentation Contaminates Interface."
⑥ A method interface comment covers five things: behavior, arguments and return value, side effects, exceptions, preconditions.
⑦ Declarations plus interface comments alone should explain the abstraction; if not, comments are missing or the interface is too complex.
⑧ Implementation comments say what and why; cross-module rules go where people pass, or into designNotes.
⑨ Google's C++ guide, PEP 257 and the Linux style all echo this layering; how much to comment inside functions is still debated.