Practicecore
Deep modules
Also called deep module, deep methods, deep interfaces, shallow modules.
Prefer modules that give a lot of functionality through a simple interface; a module whose interface is almost as complex as its implementation is shallow and adds little.
Statement
"The best methods are those that provide a lot of functionality but have a very simple interface: they replace a large cognitive load (reading the detailed implementation) with a much smaller cognitive load (learning the interface). I call these methods 'deep'."1
Meaning
John Ousterhout judges a design idea by one question: "When I'm evaluating an idea related to software design, I ask whether it will reduce complexity. This usually means either reducing the amount of information a developer has to know, or making the required information more obvious."1
A module helps when its users can work with it "without learning the details of how the method is implemented; they only need to learn its interface."1 The benefit is the difference between the two costs. A deep module hides much behind a small interface. A shallow module hides little: its interface is almost as large as the code behind it.
Ousterhout uses the idea to judge decomposition. "As methods get smaller and smaller there is less and less benefit to further subdivision. The amount of functionality hidden behind each interface drops, while the interfaces often become more complex. I call these interfaces 'shallow': they don't help much in terms of reducing what the programmer needs to know."1 In the end, "someone using the method needs to understand every aspect of its implementation. Such methods are usually pointless."1
Examples
The example below reads a small configuration text in two ways. In the shallow version, each step is its own function, and the caller must know all four and their order. In the deep version, one function with one parameter does the same work.
// Shallow: each step is its own function; the caller must know them all.
const splitLines = (text) => text.split("\n");
const dropComments = (lines) => lines.filter((l) => !l.startsWith("#"));
const dropBlank = (lines) => lines.filter((l) => l.trim() !== "");
const toPair = (line) => line.split("=").map((s) => s.trim());
// Deep: one simple interface hides the same steps.
function parseConfig(text) {
const result = {};
for (const line of text.split("\n")) {
const s = line.trim();
if (s === "" || s.startsWith("#")) continue;
const [key, value] = s.split("=").map((x) => x.trim());
result[key] = value;
}
return result;
}
const text = "# server\nhost = example.org\n\nport = 8080\n";
const shallow = Object.fromEntries(dropBlank(dropComments(splitLines(text))).map(toPair));
console.log(`shallow: ${JSON.stringify(shallow)}`);
console.log(`deep: ${JSON.stringify(parseConfig(text))}`);
shallow: {"host":"example.org","port":"8080"}
deep: {"host":"example.org","port":"8080"}
Both give the same result. A user of the deep version learns one function. A user of the shallow version learns four, and must also know that comments are dropped before blank lines and before the split at the equals sign.
| Language | What the example shows |
|---|---|
| JavaScript | four small functions against one parseConfig, as above |
| Python | the same two versions, with a dict as the result |
| Go | the same two versions, with a map as the result and strings.Cut |
import json
# Shallow: each step is its own function; the caller must know them all.
def split_lines(text):
return text.split("\n")
def drop_comments(lines):
return [l for l in lines if not l.startswith("#")]
def drop_blank(lines):
return [l for l in lines if l.strip() != ""]
def to_pair(line):
key, value = line.split("=")
return key.strip(), value.strip()
# Deep: one simple interface hides the same steps.
def parse_config(text):
result = {}
for line in text.split("\n"):
s = line.strip()
if s == "" or s.startswith("#"):
continue
key, value = s.split("=")
result[key.strip()] = value.strip()
return result
text = "# server\nhost = example.org\n\nport = 8080\n"
shallow = dict(to_pair(l) for l in drop_blank(drop_comments(split_lines(text))))
print(f"shallow: {json.dumps(shallow, separators=(',', ':'))}")
print(f"deep: {json.dumps(parse_config(text), separators=(',', ':'))}")
shallow: {"host":"example.org","port":"8080"}
deep: {"host":"example.org","port":"8080"}
package main
import (
"encoding/json"
"fmt"
"strings"
)
// Shallow: each step is its own function; the caller must know them all.
func splitLines(text string) []string { return strings.Split(text, "\n") }
func dropComments(lines []string) []string {
var out []string
for _, l := range lines {
if !strings.HasPrefix(l, "#") {
out = append(out, l)
}
}
return out
}
func dropBlank(lines []string) []string {
var out []string
for _, l := range lines {
if strings.TrimSpace(l) != "" {
out = append(out, l)
}
}
return out
}
func toPair(line string) (string, string) {
k, v, _ := strings.Cut(line, "=")
return strings.TrimSpace(k), strings.TrimSpace(v)
}
// parseConfig is deep: one simple interface hides the same steps.
func parseConfig(text string) map[string]string {
result := map[string]string{}
for _, line := range strings.Split(text, "\n") {
s := strings.TrimSpace(line)
if s == "" || strings.HasPrefix(s, "#") {
continue
}
k, v, _ := strings.Cut(s, "=")
result[strings.TrimSpace(k)] = strings.TrimSpace(v)
}
return result
}
func main() {
text := "# server\nhost = example.org\n\nport = 8080\n"
shallow := map[string]string{}
for _, l := range dropBlank(dropComments(splitLines(text))) {
k, v := toPair(l)
shallow[k] = v
}
s, _ := json.Marshal(shallow) // map keys are sorted
d, _ := json.Marshal(parseConfig(text))
fmt.Printf("shallow: %s\n", s)
fmt.Printf("deep: %s\n", d)
}
shallow: {"host":"example.org","port":"8080"}
deep: {"host":"example.org","port":"8080"}
Criticism and limits
- The opposite advice. Robert Martin's Clean Code says "The first rule of functions is that they should be small. The second rule of functions is that they should be smaller than that."1 Ousterhout answers that "decomposition can be taken too far", and that the advice "encourages programmers to create teeny-tiny methods that suffer from both shallow interfaces and entanglement."1 Martin replies: "if I must err, I'd rather err on the side of decomposition."1
- Entanglement. Small methods can also be entangled: "Two methods are entangled (or 'conjoined' in APOSD terminology) if, in order to understand how one of them works internally, you also need to read the code of the other."1 Ousterhout's fix is to combine them, "so that all the code is in one place."1
- A judgement, not a rule. Ousterhout uses the deep and shallow view because "it captures both sides of the tradeoff; it will tell you when a decomposition is good and also when decomposition makes things worse."1 It gives no size limit for a method or a module.
History
Open Library dates the first edition of A Philosophy of Software Design, published by Yaknyam Press, to 6 April 2018.3 Ousterhout released the second edition in July 2021, and in it he "reworked and expanded Chapter 6 ('General-Purpose Modules are Deeper')".2 From September 2024 to February 2025, Ousterhout and Robert Martin held a series of discussions on the differences between the two books, which they published on GitHub.1
References
- John Ousterhout and Robert Martin, A Philosophy of Software Design vs Clean Code, September 2024 to February 2025, sections Introductions and Method Length
- John Ousterhout, A Philosophy Of Software Design, the book page, the notes on the Second Edition
- Open Library, A Philosophy of Software Design, John Ousterhout, Yaknyam Press, 6 April 2018, the edition record
About this page
The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H0 no proof |
|
| Lean | L0 no Lean part |
|
| Visual | V0 none |
|
| Writing | W4 complete |
|
Tag JNQMC0 Edit this page History Report an error Markdown