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

Modifying Existing Code

A Philosophy of Software Design · Ch 16 · Modifying Existing Code · Ousterhout · 2nd ed 2021

中文 →

What is this chapter about?

Every app on your phone shipped a first version, and then spent years being changed: new features, bug fixes, a fresh look. Programmers spend far more time changing code than writing it from scratch. When a new request arrives, the easiest move is to find the fewest lines you can touch, squeeze the new thing in, and call it done once it works. This chapter says: don't change code that way.

An analogy

Picture an old house that needs one more power outlet. The quickest fix is to run a cable from the neighboring outlet and tape it along the wall. Once or twice is fine. Ten years later the walls are covered in loose cables and nobody dares touch them, because nobody knows which one goes where. The better way starts with a question: "If we had known about this outlet when the house was built, where would the wire run?" Then you make the change look like that, even if it means opening up the wall.

Why the old way is hard

Each taped-on cable makes sense on its own: fast, leaves everything else alone, looks like the lowest risk. The trouble is that they pile up. No single change is the culprit, but after a hundred of them every edit feels dangerous. The house's instructions suffer the same fate: if the note sits far from the wiring, the person rewiring never sees it, and the note quietly becomes wrong.

How it works, intuitively

What it buys you / how to choose

Changed this way, a system gets clearer with every change instead of messier, and the next person has an easier time. One honest cost: each change takes a bit longer, sometimes a lot longer than a quick patch, especially under a deadline.

One line to remember

Don't aim for the fewest lines changed: after the change, the system should look as if it had been designed for this need from the start.

Want to see why tactical patches snowball, why comments belong in the code instead of the commit log, the habit of checking the diff, and the same principle in Google's code review guide? → Switch to Deep