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

Consistency

A Philosophy of Software Design · Ch 17 · Consistency · Ousterhout · 2nd ed 2021

中文 →

What is this chapter about?

You can find the checkout and the exit in almost any supermarket within seconds, because supermarkets are laid out in much the same way. Software works the same. In a program with hundreds of thousands of lines, if the same kind of thing is always done the same way, a newcomer who learns it once can read it in a hundred places. This chapter is about that: consistency, and why "I have a better idea" is usually not a good enough reason to break it.

An analogy

Think of a city's traffic lights: red means stop and green means go, everywhere, so drivers never have to think about it. Now one engineer decides blue is more visible and makes blue mean stop at their intersection. Blue may really be more visible, but every driver in town will make mistakes at that corner. A good idea that differs from everything around it becomes a trap.

Why the old way is hard

Software is written by many people over many years. Everyone has habits, and each habit makes sense on its own. The result is three or four ways of doing the same thing. Readers must work out each spot from scratch. Worse, they guess based on what they learned elsewhere, guess wrong, and a bug slips in.

How it works, intuitively

What it buys you / how to choose

Code becomes faster to read, mistakes become rarer, and people spend their energy on real problems instead of arguing about style. One honest cost: sticking to the rules sometimes means living with an old approach that is not the best.

One line to remember

Do similar things the same way; "I have a better idea" is not enough unless it is good enough to change every old place too.

Want the five places consistency shows up, the four ways to keep it, when consistency goes too far, and how Google, Python and Go wrote it into their own guides? → Switch to Deep