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

Different Layer, Different Abstraction

A Philosophy of Software Design · Ch 7 · Different Layer, Different Abstraction · Ousterhout · 2nd ed 2021

中文 →

What is this chapter about?

Any sizable piece of software is built in layers, stacked on top of each other. Save a photo on your phone: you see "a file," but underneath one layer is juggling "chunks of data cached in memory," and below that another layer is talking to the storage chip. Each layer speaks a different language. This chapter's claim is simple: every layer should offer something different from the layers above and below it; if two neighboring layers speak the same language, the extra one is probably dead weight.

A strange thing first

Some code, when you open it, is a method with a single line: hand the request, unchanged, to some other part. It's like a job at a company where the boss's instructions get forwarded word for word to a subordinate, and the subordinate's reply gets forwarded word for word back. One more person, not one bit more work done, one more hop of communication. Stranger still, code like this is often praised as "nicely layered."

Why the old world was hard

"Layering" sounds good by default, so people add a layer whenever a problem shows up: another wrapper, another class that just hands things along, a setting passed all the way from the top down to the bottom. Each layer looks tidy on its own, but together they form a long chain of hand-offs. A reader has to walk through several layers to find the code that actually does the work, and one change has to be made in several places.

How it works, intuitively

What it buys you / how to choose

To decide whether a layer deserves to exist, ask one thing: does it shield anyone from some trouble? If yes, it earns its place; if it only passes things along, it's a burden. One honest cost: that shared cupboard has a flaw too. Throw everything in, and soon nobody can see who depends on what.

Remember it in one line

Every layer should change the vocabulary; layers that only forward, wrappers that only re-wrap, and messages relayed through every hand are all extra weight on the system.

Want the three fixes for pass-through methods, when duplicated interfaces are fine, why decorators tend to be shallow, "the interface should differ from the implementation," the trade-off between pass-through variables and context objects, and why Go's context does the opposite? → switch to Deep