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

Why Write Comments? The Four Excuses

A Philosophy of Software Design · Ch 12 · Why Write Comments? The Four Excuses · Ousterhout · 2nd ed 2021

中文 →

What is this chapter about?

You buy a new washing machine. The buttons say "Quick", "Heavy", "Delicate". From the buttons alone, can you tell whether "Quick" is fine for jeans, or whether detergent goes in first? The buttons tell you what exists; the manual tells you how to use it and why. Comments in a program are that manual. This chapter is about why many programmers skip them, and why each of their four reasons falls apart.

A strange thing first

Many programmers believe that "good code speaks for itself; it needs no comments." The author calls this a delicious myth, like a rumor that ice cream is good for your health: we would love to believe it, but it simply isn't true.

Why the old world was hard

Whoever writes code carries a lot in their head: why it was done this way, where the traps are, when not to use it. The code captures only part of that. If they don't write the rest down, it stays in their head; months later even they forget, and whoever comes next has to read the code line by line and guess. Guess wrong, and you get a bug.

How it works, intuitively

Four common excuses, and the author's answers:

What it buys you / how to choose

Good comments move what's in the designer's head onto paper: the next person can use the code safely without reading all of it, and is less likely to hit traps they didn't know existed. One honest cost: comments do need to be maintained alongside the code, and left unattended they really do go stale.

One line to remember

Code says what it does; comments say what code can't. Without them, everyone else is guessing.

Want to see why comments are part of the abstraction itself, what a substring method's signature leaves out, how much time comments really cost, and the public debate between Ousterhout and the author of Clean Code? → Switch to Deep