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

General-Purpose Modules are Deeper

A Philosophy of Software Design · Ch 6 · General-Purpose Modules are Deeper · Ousterhout · 2nd ed 2021

中文 →

What is this chapter about?

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 strange thing first

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.

Why the old world was hard

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.

How it works, intuitively

What it buys you / how to choose

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.

Remember it in one line

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