Book Deep-Read · A Philosophy of Software Design · Chapter 7
A Philosophy of Software Design · Ch 7 · Different Layer, Different Abstraction · Ousterhout · 2nd ed 2021
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.
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."
"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.
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.
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
You'd think more layers means cleaner structure; Ousterhout says a layer is worth exactly as much as the abstraction it changes. In a well-designed system, each layer provides a different abstraction from the layers above and below it; when adjacent layers have similar abstractions, the decomposition has gone wrong. Pass-through methods, pass-through variables and shallow decorators are all symptoms of that.
Chapter 7. The previous three chapters (deep modules, information hiding, general-purpose interfaces) looked at a single module; this one turns to the boundaries between layers, and the next, "Pull Complexity Downward," asks which layer the complexity should land in. In practice it maps to every review where someone asks "should we wrap this in one more layer / add one more Service class?"
Layering is one of software engineering's oldest tools, and the author opens with the classic examples. A file system's top layer offers "files" (variable-length arrays of bytes); the layer below is "an in-memory cache of fixed-size disk blocks"; the bottom is device drivers that move blocks between storage and memory. TCP provides "a reliably delivered stream of bytes," built on a layer that can only send bounded-size packets between machines on a best-effort basis. At each step up, the abstraction changes and some complexity is absorbed.
The trouble is that layering easily becomes a goal in itself. In many codebases you can count five or six layers (Controller, Service, Manager, Repository, DAO...), yet several of them just rename the same call and pass it down. Every layer's interface has to be read, remembered and maintained, and this one shields nobody from anything. The chapter gives the signals for spotting such "fake layering" and how to fix it.
A pass-through method does little except invoke another method with the same or a similar signature. The author shows a TextDocument class from a GUI text editor: it holds a TextArea, and several public methods (getting the cursor offset, inserting a string, and so on) have a one-line body that forwards the call unchanged to the TextArea.
Why is that a bad sign? Pass-through methods make a class shallower: the interface grows wider while functionality doesn't grow at all. They also create dependencies between the two classes: change the TextArea signature and TextDocument must change too. Most fundamentally, they reveal confused division of responsibility: nobody can say clearly which class does what. The author flags it as a red flag: Pass-Through Method.
There are three fixes (the book's figure also shows the original state):
Identical signatures aren't guilty by themselves. The test is: does each method with that signature provide useful and distinct functionality? Two legitimate cases. A dispatcher: a web server gets a request and forwards it to a handler chosen by URL; its signature often matches the handlers', but "choosing which one" is real functionality. And several implementations of one interface: an operating system's disk drivers all provide the same interface with different implementations, which lowers cognitive load for upper layers: learn one interface and you can use every device.
A decorator wraps another object, keeps exactly the same interface, and adds a little functionality. The book's example is Java I/O: BufferedInputStream wraps FileInputStream to add buffering. The motive is sound (separate special-purpose extensions from a generic core), but the cost is that to add one small feature, the decorator must pass through every method of the underlying object, spawning lots of shallow methods. Decorator classes also tend to multiply, and callers have to assemble each combination themselves.
The author lists questions to ask first. Could you add the feature directly to the underlying class (buffering, say, arguably belongs there by default)? If the feature serves one specific use case, could you merge it into that use case? Could you merge it into an existing decorator? And finally, does it really need to wrap the underlying object at all; could it be a stand-alone class? To give a window scroll bars, for instance, the scroll bars can be implemented separately instead of wrapping every method of the window.
The same principle applied inside a single class: a class's interface should normally be different from its implementation. If the text is stored internally as lines and the interface copies that as getLine / putLine, then higher layers that want to insert or delete in the middle of a line must split and join lines themselves. The implementation detail has been pushed straight onto callers, and the class is shallow. Switch to a character-oriented interface (insert at any position, delete any range) and the class does more mapping internally, but callers lose a pile of split-and-join code. The more an interface resembles its implementation, the more likely the class is shallow.
Another form of pass-through is the pass-through variable: a value that enters at a top-level method and is handed down through a long chain of methods that don't use it, just so the bottom method can get it. The book's example: a command-line argument names the certificate to use for secure communication; main reads it and passes it through m1 and m2 down to m3, which actually opens the connection. Every intermediate layer is forced to know it exists, and the day you add another such variable, every signature on the chain changes.
Three remedies. If the top and bottom already share some object, store the variable in that object. Use a global variable, though that makes it impossible to run two independent instances of the system in one process (in tests, for example). The author's usual choice is a context object: it stores a system instance's global state (configuration options, shared subsystems, performance counters), one per instance; most objects receive a reference at construction and keep it in a field, so adding a new piece of global state means adding a field to the context.
The author admits contexts are not ideal: without discipline, a context turns into a grab-bag of data that creates non-obvious dependencies, and with multiple threads there are concurrency issues, so he recommends keeping context variables immutable where possible. But he says he hasn't found a better solution.
The chapter's conclusion fits in one sentence: every design element added to a system (an interface, argument, function, class, definition) adds complexity, because developers must learn about it; to be a net gain, it must eliminate some complexity that would otherwise exist. What pass-through methods, pass-through variables and shallow decorators have in common is that they add things to learn and remove nothing.
Table 1 · Four signals of a layering failure, and their fixes
| Signal | What it looks like | Preferred fix | Cost of the fix |
|---|---|---|---|
| Pass-through method | One-line body forwarding to a same-named method below | Expose lower / redistribute / merge | Exposing makes callers know one more class; merging may yield an oversized class |
| Shallow decorator | Dozens of methods forwarded to add one small feature | Fold the feature into the base class or the use case; or make it stand-alone | A bit more code and options in the base class |
| Interface = implementation | Stored as lines inside, so the API is getLine / putLine | Redesign the interface around the abstraction callers need | The class must translate internally; harder to write |
| Pass-through variable | A parameter threaded through layers that ignore it | Shared object or context | Implicit dependencies; contexts bloat and need care with concurrency |
Table 2 · Where a pass-through variable can live
| Home | Fits when | Benefit | Cost |
|---|---|---|---|
| An already-shared object | Top and bottom happen to hold the same object | No new structure | Rarely available |
| Global variable | Scripts, one-off programs | Least effort | No two instances in one process; tests pollute each other |
| Context object | Systems with many subsystems that need multiple instances | New global state changes one place; multiple instances work | Dependencies less visible; becomes a junk drawer; prefer immutable fields |
One test: if you delete this layer, what more must its callers know? If the answer is "almost nothing," the layer should go or be merged.
The most common anti-pattern in backend projects is layering by template: every entity gets Controller → Service → Repository, and half the Service methods are return repo.findById(id). In review you can poke at it with this chapter's questions: does this layer change the abstraction? What does it shield callers from? Don't overcorrect, though: a layer that owns transaction boundaries, permission checks or cross-entity orchestration really does provide a different abstraction, and similar signatures don't make it a pass-through.
Evidence and sources · publicly verifiable
io.Reader has a single method, so decorating it costs almost no pass-through; apply the same move to a class with dozens of methods and you get the shallow decorator this chapter warns about. Source: pkg.go.dev · bufioFileInputStream, wrap it in a BufferedInputStream, then wrap that in an ObjectInputStream; buffering, which nearly everyone wants, is a decorator you must remember to add by hand. Source: Ousterhout, A Philosophy of Software Design, Chapters 4 and 7① In a good system each layer provides an abstraction different from its neighbors; similar adjacent abstractions signal a broken decomposition.
② Classic good examples: the file system (files → block cache → drivers) and TCP (a reliable byte stream on top of best-effort packets).
③ Pass-through methods make classes shallow and create cross-class dependencies; fix by exposing the lower class, redistributing functionality, or merging.
④ Duplicate signatures aren't always guilty: dispatchers and multiple implementations of one interface each add functionality.
⑤ Decorators are shallow by nature: first ask whether the feature can go into the base class, the use case, an existing decorator, or a stand-alone class.
⑥ A class's interface should differ from its implementation; an interface that mirrors the internal representation is shallow.
⑦ Homes for pass-through variables: a shared object, a global, or a context object; context is the usual choice, but guard against bloat and prefer immutable fields.
⑧ Go's context does the opposite (explicit parameters), showing this is a real trade-off between visibility and convenience.
⑨ The master test: every design element must remove more complexity than it adds, or it's a burden.