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

Write The Comments First

A Philosophy of Software Design · Ch 15 · Write The Comments First · Ousterhout · 2nd ed 2021

中文 →

What is this chapter about?

When programmers write code, they often leave short notes beside it telling the next person "what this part does and how to use it." Those notes are called comments. Most people's habit is to finish the program, get it working, and add the comments at the end — if they remember. This chapter makes one suggestion that runs against that habit: flip the order. Write the explanation first, then the code.

An analogy

Imagine you're inventing a new board game. One way is to build the board and print all the cards, then play and see. Another is to take a sheet of paper and write the rules in a few sentences. If five sentences cover it, the game is probably good. If two pages still don't make it clear, you know the rules are a mess before you've spent a cent on cards. The author says writing a program's explanation is writing that rule sheet.

Why the old way is hard

"I'll add it at the end" sounds efficient, but it's the easiest step to skip: once the program runs, people rush to the next task. Even when the notes do get written, the reasons behind the design are half forgotten by then, so the notes just read the code back aloud, which helps no one. Worse, the explanation could have exposed a design flaw — but written last, the flaw is already built into the walls.

How it works, intuitively

What you get / how to choose

The moment the code is done, the explanation is done too — and it's better, because it was written while your thinking was clearest. The author even says this turns comment-writing into one of the most enjoyable parts of design. One honest cost: when you change your mind mid-design, the early notes have to change with it, which takes some discipline.

One line to remember

Explain first, code second: if the explanation flows, the design is probably right; if it doesn't, fix the design first.

Want the author's exact order, the "Hard to Describe" red flag, how much time comments really cost, and his public debate with the author of Clean Code about comments and test-first development? → Switch to Deep