Book Deep-Read · A Philosophy of Software Design · Chapter 13

Comments Should Describe What Isn't Obvious

A Philosophy of Software Design · Ch 13 · Comments Should Describe Things that Aren't Obvious from the Code · Ousterhout · 2nd ed 2021

中文 →

What is this chapter about?

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."

A strange thing first

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.

Why the old world was hard

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.

How it works, intuitively

What it buys you / how to choose

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.

One line to remember

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