Book Deep-Read · A Philosophy of Software Design · Chapter 16
A Philosophy of Software Design · Ch 16 · Modifying Existing Code · Ousterhout · 2nd ed 2021
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.
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.
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.
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.
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
You might think the top rule for changing existing code is "smallest change, lowest risk." Ousterhout says that is exactly the tactical programming Chapter 3 warned about. The real standard: after each change, the system should have the structure it would have had if it had been designed from the start with that change in mind. If you're not making the design better, you are probably making it worse.
Chapter 16. Most earlier chapters ask how to design from scratch; this one turns to maintenance. The first half returns to Chapter 3's strategic-vs-tactical theme and applies it to each individual change. The second half answers Chapter 12's excuse that "comments get out of date" with concrete practices for keeping them current. Chapter 17, on consistency, continues with how to honor existing conventions while changing code.
Chapter 1 said complexity is incremental: no single change ruins a system, but hundreds of small compromises can. Large systems spend most of their lives being modified, so modification is where complexity actually gets in.
A typical case: an import module handles one file format. A second format arrives, so you add an if. A third arrives, so you add a flag parameter. By the sixth, the function is full of special cases, the flags constrain each other, and nobody can say which flag matters in which combination. Every step was a "minimal change"; together they are a tangle.
Meanwhile the comments silently decay: whoever changed the code didn't see a comment far away, or wrote the explanation into the commit message, leaving future readers with nothing. The chapter answers two questions: what standard should guide a change, and how do you keep comments from going stale afterwards?
When modifying code there is a strong pull toward the smallest change that makes the new feature work, touching as little else as possible because it feels safest. The author calls this mindset tactical programming: each change leaves a small special case or a small dependency, and complexity keeps accumulating.
His standard is a thought experiment: if you had known about this requirement when you first designed the system, what would it look like? Ideally, after each change, the system looks like that. Getting there often requires refactoring along the way: folding special cases into a more general mechanism, replacing an abstraction that no longer fits, pulling scattered logic into one place.
This is the concrete form of strategic programming's continual investment: whenever you modify any code, try to improve the design at least a little. A change is the natural moment to improve design, because right now you actually understand that code.
The author is not naive. He admits deadlines can be tight and the ideal refactoring can be too large to finish now. His advice is not to give up but to ask: "Is this the best I can do to create a clean system design, given my current constraints?" Often there is an option that is almost as clean but much cheaper, for example putting the new logic in a well-designed new method instead of wedging it into the middle of an old one.
If a patch is truly all you can afford, know that you are taking on technical debt: write it down and pay it back when time allows. The real danger is not one compromise; it is "minimal change" becoming the team's default habit.
Chapter 12 listed a common excuse: comments get out of date. The author agrees this is a real problem but thinks practice can solve it, starting with placement. The farther a comment sits from the code it describes, the less likely the person changing that code is to see it, and the faster it goes stale.
Then the comment is right in front of you when you change the code, and "update it while you're there" actually happens.
A common habit: you change some subtle logic, write the detailed reasoning into the commit message, and leave nothing in the code. The author calls this a mistake. Future readers of the code will almost never dig through version history, and even if they try, finding the one relevant commit among hundreds or thousands is hard.
His test is simple: if information will help someone understand this code later, put it in the code. The commit log is the right place for the history of what changed and why; it can point to a comment but cannot replace one.
Copy the same explanation into three places and a future change will almost certainly miss one, leaving three versions that contradict each other. The author's rule: document each design decision exactly once, in the place a developer is most likely to look first; elsewhere, write a short "see the comment in X." If the information already lives in a document outside the program, such as a protocol spec, reference it from the code rather than copying it.
A cheap, effective habit: scan the whole diff before you commit, making sure each change is reflected in the documentation, and catching leftover debug code while you are at it. A few minutes keep comments and code from silently drifting apart.
Comments more abstract than the code describe what and why, not line-by-line how, so small code changes usually don't invalidate them. Comments that paraphrase the code line by line go stale with every edit. This matches Chapter 13: comments should capture what the code cannot say.
Table 1 · A new requirement arrives: three ways to change the code
| Approach | What you do | Short term | Long term |
|---|---|---|---|
| Tactical patch | Squeeze in the fewest lines; add a special case or a flag | Fastest, smallest diff to review | Special cases pile up; the next change is costlier and more error-prone |
| Best under constraints (the chapter's fallback) | Ask for the cleanest option given current limits, often a new abstraction plus light refactoring | A bit slower | Structure largely preserved; debt is recorded and can be repaid |
| Strategic change (the chapter's ideal) | Reshape the structure as if known from the start, refactoring along the way | Slowest, larger diff, needs tests as a safety net | The system gets clearer with each change; change cost does not climb |
Table 2 · Where a piece of information should live
| Location | Good for | When readers see it | Staleness risk |
|---|---|---|---|
| Interface comment at the declaration | Abstraction, usage, preconditions, side effects | Every time someone reads or calls it | Low, it is right there when you edit |
| Implementation comment in a function | A block's intent, non-obvious reasons | When reading those lines | Medium; lower the closer and more abstract it is |
| Commit log | What changed this time and why; may point to a comment | Only when tracing history | Never "stale," but rarely read |
| External document | Cross-module specs, protocols, overall architecture | When someone goes looking | High; reference it from code, don't copy it |
Rule of thumb: the longer code will live and the more people will change it, the more it deserves strategic changes. For a throwaway script or a module about to be retired, a tactical patch can be rational. The real problem is never one patch; it is no longer asking the question.
Code review, refactoring, and comment conventions in large engineering organizations all serve one goal: no single change should leave the whole worse off. This chapter pushes that goal down from team process to a question every engineer asks on every change.
Industry evidence · publicly verifiable
Day to day: when a request comes in, spend a few minutes on "what would a from-scratch design look like" before choosing how to change the code; in review, ask not only whether the diff is correct but whether it leaves the surrounding structure better or worse; when an AI assistant edits code for you, ask it for the ideal structure rather than just a minimal patch, and check before committing that the comments kept up.
① Systems spend most of their lives being modified; complexity accumulates one small change at a time.
② The pull toward the "smallest change" is tactical programming.
③ Yardstick: after each change, the structure should look as if designed from the start with that requirement in mind.
④ If you're not making the design better, you're probably making it worse; invest a little with every change.
⑤ Short on time? Ask: is this the cleanest design I can manage under current constraints?
⑥ Keep comments as close to the code as possible, with the narrowest scope.
⑦ Information needed to understand the code belongs in the code, not the commit log.
⑧ Document each design decision once and reference it elsewhere; reference external docs instead of copying them.
⑨ Scan the diff before committing; higher-level comments are the least likely to go stale.