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

Better Together or Better Apart?

A Philosophy of Software Design · Ch 9 · Better Together or Better Apart? · Ousterhout · 2nd ed 2021

中文 →

What is this chapter about?

Programmers make one small decision all day long: should these two pieces of functionality live together, or apart? Many people have a default answer: "split everything finely; the shorter each piece, the better." This chapter says: not necessarily. Splitting has a cost of its own, and sometimes the right move is to combine.

An analogy

Think of a kitchen. Keep the knife next to the cutting board and chopping is easy. Sort strictly by type instead (knives in the east cupboard, boards in the west) and it looks tidy, but every time you chop you walk to two places. On the other hand, putting dish soap and flour in the same jar is combining things that should stay apart. The test of a tidy kitchen is not how finely it is sorted, but how smoothly it works.

A strange thing first

Someone takes a few dozen lines of code and splits them into eight pieces of two or three lines each. Every piece looks "clean." Then a reader has to jump between eight places to work out what the code actually does. Each piece is easy to read; together they are hard to follow.

How it works, intuitively

What it gives you / how to choose

When you're torn between together and apart, ask: which way leaves the reader with less to keep in mind? One honest cost: this is a judgment call that takes experience. There is no simple "split after so many lines" rule to lean on.

One line to remember

Whether to split depends on how easy the whole is to understand, not on how short each piece is.

Want the four costs of splitting and four signals for combining, how undo splits into a general layer and a special layer, how to split a method without making it shallow, and the public debate between the author and the author of Clean Code? → Switch to Deep