codebase-design
设计深模块:把大量行为放在一个小接口后面,并把它放在干净的 seam 上。调用者可以通过这个接口使用它,测试也可以通过这个接口进行。无论设计还是重构代码,都要用这套术语和原则。这样做是为了让调用者更省力,让维护者更容易把改动集中起来,也让所有人都更容易测试。
请准确使用这些术语,不要换成「组件」「服务」「API」或「边界」。统一用词是全部重点。
模块:任何同时有接口和实现的东西。这个定义刻意不受规模限制,可以是函数、类、包,也可以是横跨多个层级的一段代码。避免:单元、组件、服务。
接口:调用者要正确使用模块,必须知道的所有信息:类型签名,以及不变量、顺序约束、错误形式、必需配置和性能特征。避免:API、签名(太窄,只指类型层面的表面)。
实现:模块内部的代码主体。它和适配器不同:小适配器也可能有很大的实现(例如 Postgres repo),大适配器也可能只有很小的实现(例如内存中的 fake)。讨论 seam 时用「适配器」,其他情况用「实现」。
深度:接口带来的杠杆。也就是调用者或测试每学会单位接口,能使用多少行为。模块背后有大量行为、接口却很小时,它就是深的;接口几乎和实现一样复杂时,它就是浅的。
Seam(Michael Feathers 的术语):一个可以改变行为、又不用在该处编辑代码的位置。它是模块接口所在的位置。决定 seam 放在哪里,是单独的设计决策,和决定 seam 后面放什么不同。避免:边界(这个词在 DDD 中已经被用于有界上下文)。
适配器:在 seam 上满足接口的具体东西。它描述的是角色(填哪个位置),不是内容(里面有什么)。
杠杆:调用者从深度中得到的好处。调用者学会的每单位接口,能获得更多能力。同一个实现能在 N 个调用点和 M 个测试中发挥作用。
局部性:维护者从深度中得到的好处。改动、bug、知识和验证都集中在一个地方,不再散落到调用者那里。修一次,处处修好。
深模块与浅模块
Section titled “深模块与浅模块”深模块 = 小接口 + 大量实现:
┌─────────────────────┐│ Small Interface │ ← Few methods, simple params├─────────────────────┤│ ││ Deep Implementation│ ← Complex logic hidden│ │└─────────────────────┘浅模块 = 大接口 + 少量实现(应避免):
┌─────────────────────────────────┐│ Large Interface │ ← Many methods, complex params├─────────────────────────────────┤│ Thin Implementation │ ← Just passes through└─────────────────────────────────┘设计接口时,可以问:
- 能不能减少方法数量?
- 能不能简化参数?
- 能不能把更多复杂性藏在内部?
- **深度是接口的属性,不是实现的属性。**深模块内部可以由小型、可 mock、可替换的部分组成,只是这些部分不属于接口。模块可以有内部 seam(只对自己的实现可见,由自己的测试使用),也可以有位于接口处的外部 seam。
- **删除测试。**想象把模块删掉。如果复杂性也消失了,这个模块只是透传;如果复杂性又散落到 N 个调用者那里,它就发挥了作用。
- **接口就是测试面。**调用者和测试跨过的是同一个 seam。如果你想越过接口去测试模块,模块的形状可能就不对。
- **一个适配器意味着假设出来的 seam,两个适配器才意味着真实的 seam。**只有当 seam 两侧确实存在不同情况时,才应该引入 seam。
让模块更容易测试
Section titled “让模块更容易测试”好的接口能让测试自然发生:
-
接收依赖,不要自己创建依赖。
// Testablefunction processOrder(order, paymentGateway) {}// Hard to testfunction processOrder(order) {const gateway = new StripeGateway();} -
返回结果,不要产生副作用。
// Testablefunction calculateDiscount(cart): Discount {}// Hard to testfunction applyDiscount(cart): void {cart.total -= discount;} -
**缩小接口。**方法越少,需要的测试越少。参数越少,测试准备越简单。
- 一个模块恰好有一个接口,也就是它呈现给调用者和测试的表面。
- 深度是模块相对于其接口的属性。
- Seam是模块的接口所在的位置。
- 适配器位于Seam上,并满足接口。
- 深度为调用者带来杠杆,为维护者带来局部性。
不采用的说法
Section titled “不采用的说法”- 把深度定义为实现行数与接口行数之比(Ousterhout):这种算法会奖励给实现凑行数。我们采用「深度就是杠杆」的定义。
- 把「接口」理解成 TypeScript 的
interface关键字或类的公开方法:这个定义太窄。这里的接口包含调用者必须知道的所有信息。 - 「边界」:这个词在 DDD 中容易和有界上下文混淆。请说 seam 或 接口。
- 在给定依赖的情况下加深一组模块:参见 DEEPENING.md,其中说明依赖分类、seam 原则,以及「替换,不叠加」的测试方式。
- 探索不同接口方案:参见 DESIGN-IT-TWICE.md,它会启动并行子代理,用几种截然不同的方式设计接口,再按深度、局部性和 seam 位置进行比较。
原文链接和 commit
Section titled “原文链接和 commit”读者可以点名用它,模型也可以自己调用它。它在 Claude Code 插件里。
用模块、接口、深度和 seam 这套共同词汇,判断模块是否值得抽取、接口是否够小,以及测试和适配器应位于何处。它是设计参考,不是自动推进重构的流程。
Shared vocabulary for designing deep modules. Use when the user wants to design or improve a module's interface, find deepening opportunities, decide where a seam goes, make code more testable or AI-navigable, or when another skill needs the deep-module vocabulary.- 仓库 mattpocock/skills
- 文件
skills/engineering/codebase-design/SKILL.md - 提交
d81f3a1 - 说明页
docs/engineering/codebase-design.md - 同目录文件
skills/engineering/codebase-design/agents/openai.yaml - 同目录文件
skills/engineering/codebase-design/DEEPENING.md - 同目录文件
skills/engineering/codebase-design/DESIGN-IT-TWICE.md
英文 SKILL.md
---name: codebase-designdescription: Shared vocabulary for designing deep modules. Use when the user wants to design or improve a module's interface, find deepening opportunities, decide where a seam goes, make code more testable or AI-navigable, or when another skill needs the deep-module vocabulary.---
# Codebase Design
Design **deep modules**: a lot of behaviour behind a small interface, placed at a clean seam, testable through that interface. Use this language and these principles wherever code is being designed or restructured. The aim is leverage for callers, locality for maintainers, and testability for everyone.
## Glossary
Use these terms exactly: don't substitute "component," "service," "API," or "boundary." Consistent language is the whole point.
**Module**: anything with an interface and an implementation. Deliberately scale-agnostic: a function, class, package, or tier-spanning slice. _Avoid_: unit, component, service.
**Interface**: everything a caller must know to use the module correctly: the type signature, but also invariants, ordering constraints, error modes, required configuration, and performance characteristics. _Avoid_: API, signature (too narrow, they refer only to the type-level surface).
**Implementation**: what's inside a module, its body of code. Distinct from **Adapter**: a thing can be a small adapter with a large implementation (a Postgres repo) or a large adapter with a small implementation (an in-memory fake). Reach for "adapter" when the seam is the topic; "implementation" otherwise.
**Depth**: leverage at the interface. The amount of behaviour a caller (or test) can exercise per unit of interface they have to learn. A module is **deep** when a large amount of behaviour sits behind a small interface, **shallow** when the interface is nearly as complex as the implementation.
**Seam** _(Michael Feathers)_: a place where you can alter behaviour without editing in that place; the *location* at which a module's interface lives. Where to put the seam is its own design decision, distinct from what goes behind it. _Avoid_: boundary (overloaded with DDD's bounded context).
**Adapter**: a concrete thing that satisfies an interface at a seam. Describes *role* (what slot it fills), not substance (what's inside).
**Leverage**: what callers get from depth. More capability per unit of interface they learn. One implementation pays back across N call sites and M tests.
**Locality**: what maintainers get from depth. Change, bugs, knowledge, and verification concentrate in one place rather than spreading across callers. Fix once, fixed everywhere.
## Deep vs shallow
**Deep module** = small interface + lots of implementation:
```┌─────────────────────┐│ Small Interface │ ← Few methods, simple params├─────────────────────┤│ ││ Deep Implementation│ ← Complex logic hidden│ │└─────────────────────┘```
**Shallow module** = large interface + little implementation (avoid):
```┌─────────────────────────────────┐│ Large Interface │ ← Many methods, complex params├─────────────────────────────────┤│ Thin Implementation │ ← Just passes through└─────────────────────────────────┘```
When designing an interface, ask:
- Can I reduce the number of methods?- Can I simplify the parameters?- Can I hide more complexity inside?
## Principles
- **Depth is a property of the interface, not the implementation.** A deep module can be internally composed of small, mockable, swappable parts; they just aren't part of the interface. A module can have **internal seams** (private to its implementation, used by its own tests) as well as the **external seam** at its interface.- **The deletion test.** Imagine deleting the module. If complexity vanishes, it was a pass-through. If complexity reappears across N callers, it was earning its keep.- **The interface is the test surface.** Callers and tests cross the same seam. If you want to test *past* the interface, the module is probably the wrong shape.- **One adapter means a hypothetical seam. Two adapters means a real one.** Don't introduce a seam unless something actually varies across it.
## Designing for testability
Good interfaces make testing natural:
1. **Accept dependencies, don't create them.**
```typescript // Testable function processOrder(order, paymentGateway) {}
// Hard to test function processOrder(order) { const gateway = new StripeGateway(); } ```
2. **Return results, don't produce side effects.**
```typescript // Testable function calculateDiscount(cart): Discount {}
// Hard to test function applyDiscount(cart): void { cart.total -= discount; } ```
3. **Small surface area.** Fewer methods = fewer tests needed. Fewer params = simpler test setup.
## Relationships
- A **Module** has exactly one **Interface** (the surface it presents to callers and tests).- **Depth** is a property of a **Module**, measured against its **Interface**.- A **Seam** is where a **Module**'s **Interface** lives.- An **Adapter** sits at a **Seam** and satisfies the **Interface**.- **Depth** produces **Leverage** for callers and **Locality** for maintainers.
## Rejected framings
- **Depth as ratio of implementation-lines to interface-lines** (Ousterhout): rewards padding the implementation. We use depth-as-leverage instead.- **"Interface" as the TypeScript `interface` keyword or a class's public methods**: too narrow: interface here includes every fact a caller must know.- **"Boundary"**: overloaded with DDD's bounded context. Say **seam** or **interface**.
## Going deeper
- **Deepening a cluster given its dependencies**, see [DEEPENING.md](DEEPENING.md): dependency categories, seam discipline, and replace-don't-layer testing.- **Exploring alternative interfaces**, see [DESIGN-IT-TWICE.md](DESIGN-IT-TWICE.md): spin up parallel sub-agents to design the interface several radically different ways, then compare on depth, locality, and seam placement.非官方学习站,与 Matt Pocock 和 AI Hero 没有隶属关系。本站不提供可安装的中文技能包。
技能原文采用 MIT 许可。Copyright (c) 2026 Matt Pocock