Book Deep-Read · A Philosophy of Software Design · Chapter 6
A Philosophy of Software Design · Ch 6 · General-Purpose Modules are Deeper · Ousterhout · 2nd ed 2021
Every time you build a part, you face a question: do you build it just for the one job in front of you, or make it a bit more general so it could serve other uses later? Most people's instinct is "build the special one, it's quicker." This chapter gives a counterintuitive answer: making it "somewhat general" actually makes it simpler. Copy and paste on your phone is an example. It doesn't care whether you copied a chat message, a link or a photo, and precisely because it doesn't care, every app can use it.
A group of students builds a text editor. They design the part that stores the text around the keys on the keyboard: one function is "Backspace was pressed," another is "Delete was pressed," a third is "delete the highlighted part." It looks thoughtful, but every new key or menu item forces the text part to grow another function. It should only care about text, yet it's forced to know about the keyboard.
The trouble with special-purpose design is that it's welded to one situation: the part is full of details of today's use, so a new use means rewriting it, and nobody else can use it. But going the other way, trying to build a do-everything part from day one, also fails: you can't guess the future, and what you build tends to be big and awkward. Both ends are traps; the hard part is finding the spot in between.
A somewhat general part has fewer functions to learn, and each one does more work. That is what "deeper" means. Redesigning the screen or adding keys never touches it, because it never knew what the screen looks like. One honest cost: overdo the generality and it backfires. If the part only offers the tiniest moves, users have to assemble a pile of them every time, and that's gone too far.
Build features for today, shape the doorway to be general; push the special stuff outward, and give the part a few basic moves that can build everything else.
Want the line-by-line comparison of the editor's two text APIs, the exact meaning of "somewhat general," how to use the three questions, how undo splits into a general history plus special-purpose actions, eliminating special cases, and the tension with YAGNI? → Switch to Deep
You think general-purpose design is "extra cost paid for the future"; Ousterhout says its biggest payoff arrives today: a somewhat general-purpose interface is usually simpler and deeper than a special-purpose one, with fewer methods that each do more, and with special-purpose knowledge kept out of the module. The recipe: implement functionality for current needs, but design the interface to support multiple uses.
Chapter 6. Chapter 4 set the goal (modules should be deep), Chapter 5 gave the main technique (information hiding), and this chapter settles a specific fork on the way to deep modules: special-purpose or general-purpose interface. Chapter 7, "Different Layer, Different Abstraction," turns to the boundaries between layers. In practice it maps to every review where someone asks "should this utility class grow one more method for this request?"
Every new module forces a choice. The case for special-purpose sounds solid: implement only what's needed today, YAGNI, don't pay for guesses. The case for general-purpose comes from Chapter 3's strategic programming: spend a bit more now, get reuse later. The author says both fail in their own way. Over-specialization makes the module sprout a new method for each new need, so the interface keeps widening and getting shallower while pulling caller details inside. Over-generalization builds machinery nobody uses, and because the future is hard to predict, the general parts are often guessed wrong.
The chapter asks: where is the sweet spot in between? How do you tell you've drifted? And, most importantly, why does the benefit of generality not have to wait for reuse to pay off?
The author's definition is precise: a module's functionality should reflect your current needs, but its interface should not. The interface should be general enough to support multiple uses, without implementing features you don't need today. This isn't "write code for the future"; it's "don't carve today's call sites into the interface."
He stresses that the most important, and perhaps most surprising, benefit of the general-purpose approach is a simpler, deeper interface. Even if the class is never reused elsewhere, it is already easier to use today. Reuse is a bonus.
In CS 190, students build GUI text editors and must separate text storage (a text class) from the UI. Many teams gave the text class methods that mirror the UI:
void backspace(Cursor cursor) · void delete(Cursor cursor) · void deleteSelection(Selection selection)
Each method matches one specific UI action and looks "just enough." Three problems: first, it's shallow. Each method does one narrow thing, and adding "delete word," "cut" or "find and replace" means more methods, so the interface keeps widening. Second, it leaks information: UI knowledge like "Backspace deletes the character before the cursor" now lives in the text class. Third, UI developers must learn a pile of small, scattered methods.
The more general version offers only basic operations:
void insert(Position position, String newText) · void delete(Position start, Position end) · Position changePosition(Position position, int numChars)
The third returns the position a given number of characters away (negative allowed). So Backspace becomes text.delete(text.changePosition(cursor, -1), cursor), Delete becomes text.delete(cursor, text.changePosition(cursor, 1)), and deleting a selection is delete(sel.start, sel.end). The UI writes one more line, but every line reads at a glance.
The author points out that generality leads to better information hiding. The general text class has no notion of a "Backspace key," so UI changes never touch it; the special version ties text and UI together.
backspace) is a red flag; see whether one method could cover several uses.Special-purpose code doesn't vanish; the question is where it lives. The principle is to separate special-purpose code from general-purpose code. The special part goes either up toward the application (in the editor, "which character Backspace deletes" belongs in the UI) or down to the bottom layer (an operating system defines one general interface for all devices, and each device's quirks go into its own driver). The general core in the middle stays clean and becomes a foundation others can depend on.
The same course's editors must support undo / redo. A common design is a history class that knows every undoable operation: how to undo a text insertion, a selection change, a formatting change… and it must change whenever a new operation appears. The better split: the history class is general. It just manages a list of "action" objects and lets you group adjacent actions into a single undo with a marker (the book calls it a fence). Each action knows how to undo and redo itself (implementing a shared undo() / redo() interface) and is created by the text class or the selection code.
Both sides get simpler: the history class can be reused unchanged in another program, and adding a new undoable operation means writing one new action class without touching the history. It's a complete demonstration of "push the special outward, keep the general in the middle."
Another face of specialization is special cases in code: screens full of if statements, each one handling a corner case. The advice is to design so the normal code path handles the special cases automatically. The editor again: if "no selection" is a separate state, the code for delete, copy and replace must each first check "is there a selection?"; if it is represented as an empty selection whose start equals its end, the delete-selection code naturally deletes nothing, and all those checks vanish.
Table 1 · Three points on the spectrum
| Position | What the interface looks like | Upside | Cost |
|---|---|---|---|
| Over-specialized | One method per situation: backspace, deleteSelection | Fastest to write today; callers need one line | Interface widens and gets shallower; caller details leak in; no reuse |
| Somewhat general | A few basic operations compose every current use: insert / delete(range) | Fewer methods that each do more; module doesn't know its callers; reuse is a bonus | You must think through what the basic operations are; callers write a line or two more |
| Over-generalized | Only the lowest primitives, or machinery added for a guessed future | In theory it can do anything | Callers assemble common operations themselves; wrongly guessed generality is pure burden |
Table 2 · Where special-purpose code should live
| Situation | Move | Example | Watch out |
|---|---|---|---|
| Special logic close to the user's scenario | Push up to the app / UI layer | Which character Backspace deletes; concrete undo actions | The upper layer gets a few lines of composition, each plain |
| Special logic close to hardware / backend differences | Push down to the driver / adapter layer | Operating system device drivers | The lower interface must be general enough, or the differences bleed upward |
Corner-case ifs scattered through code | Change the representation so the normal path covers it | Empty selection = start equals end | Not every special case can vanish; centralize the ones that can't |
One test: when a new requirement arrives, ask "does this add a method, or can existing methods compose it?" If every new requirement adds a method, the interface is getting shallower.
The most common specialization in practice is the slow bloat of utility classes and service interfaces: each product team asks for something, and in goes getUserForCheckoutPage, then getUserForProfilePage. In review you can apply this chapter's three questions directly: how many places will use this method? Could one or two general queries cover it? The chapter's vocabulary turns "this interface is getting shallow" into a discussable judgment rather than a matter of taste.
Evidence and sources · publicly verifiable
io.Copy only needs that interface, copying from src to dst until EOF. Files, network connections and compressed streams move data through the same function, with each one's special handling hidden in its implementation. Source: pkg.go.dev · io (Reader, Copy)Sort makes one call to Len and O(n log n) calls to Less and Swap. The algorithm is general; "how to compare" comes from the caller. The same page honestly notes that in many situations the newer slices.SortFunc is more ergonomic and faster: how usable a general interface is gets re-evaluated as the language evolves. Source: pkg.go.dev · sort (Interface, Sort)① Somewhat general-purpose modules are deeper: implement functionality for current needs, design the interface for multiple uses.
② The biggest benefit of generality is being simpler today; reuse is a bonus.
③ Editor case: backspace / deleteSelection is a shallow special-purpose API; insert / delete(range) / changePosition covers every edit with three methods.
④ Generality improves information hiding: the text class doesn't know the keyboard exists, so UI changes never touch it.
⑤ Three questions: what's the simplest interface covering current needs? How many situations use it? Is it easy to use today?
⑥ Push special-purpose code up (UI layer) or down (drivers), separate from the general core.
⑦ Undo: a general history class plus special action objects that each implement undo / redo.
⑧ Eliminate special cases: represent "no selection" as start equals end, and the normal code covers it.
⑨ Both ends are shallow: over-specialization widens the interface; over-generalization makes callers assemble everything.