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

Choosing Names

A Philosophy of Software Design · Ch 14 · Choosing Names · Ousterhout · 2nd ed 2021

中文 →

What is this chapter about?

Every variable and every function in a program needs a name. This chapter is about one thing: how to choose those names. It sounds minor, yet the author gives it a whole chapter, because names are the first and most frequent thing a reader of code sees, like room numbers in a building. Mix up the numbers and people walk into the wrong room.

A strange story first

Years ago the author helped build an operating system. For a while, files on disk were mysteriously being overwritten by other data, and the team spent a long time hunting the cause. It turned out the code had one name, "block", that sometimes meant "which block of the file" and sometimes "which block on the disk". Two meanings shared one name, and somewhere one was used as the other. One fuzzy name, one very hard bug.

Why the old way is hard

The person naming things knows exactly what they mean, so "data", "result" or "status" feels good enough. But readers aren't inside the writer's head; they can only guess from the name. Guess wrong, and they use it wrong. These small fuzzy spots don't break anything right away; they pile up until the whole program is hard to read and hard to change.

How it works, intuitively

What you get / how to choose

With good names, readers guess less, dig less and make fewer mistakes, and since one name gets read hundreds of times, a few extra minutes choosing it pays off. One honest cost: there's no single right length for a name; some teams prefer short ones, and the author admits that in the end the reader has to be the judge.

One line to remember

A name is the smallest piece of documentation: make it precise and consistent; if you can't name something well, you probably haven't thought it through.

Want to see how the file system bug happened, what "too vague" and "too specific" look like, the three consistency rules, the two red flags, and the debate between the Go community's short names and the author? → Switch to Deep