codebase-design
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseCodebase 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.
设计深度模块:在简洁的seam处,用少量接口封装大量行为,且可通过该接口进行测试。无论在设计还是重构代码时,都可使用这套术语和原则。目标是为调用者提供杠杆效应,为维护者实现代码局部性,同时让所有人都能轻松测试代码。
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.
请严格使用以下术语——不要替换为"component"、"service"、"API"或"boundary"。保持语言一致性是核心目的。
Module — 任何具备接口和实现的单元。特意不限制规模:可以是一个函数、类、包,也可以是跨层的代码片段。避免使用:unit、component、service。
Interface — 调用者正确使用模块必须了解的所有信息:不仅包括类型签名,还包括不变量、顺序约束、错误模式、必要配置和性能特征。避免使用:API、signature(范围过窄——仅指代类型层面的表面内容)。
Implementation — 模块内部的代码主体。与Adapter有所区别:某个单元可以是封装大量实现的小型适配器(如Postgres仓库),也可以是封装少量实现的大型适配器(如内存模拟对象)。当讨论的核心是seam时使用"adapter",其他场景使用"implementation"。
Depth — 接口的杠杆效应:调用者(或测试)每学习一个单元的接口,能实现的行为数量。当大量行为隐藏在少量接口背后时,模块是深度的;当接口复杂度几乎和实现相当时,模块是浅层的。
Seam (Michael Feathers) — 无需修改代码即可改变行为的位置;即模块接口所在的位置。seam的放置是独立的设计决策,与接口背后的内容无关。避免使用:boundary(与领域驱动设计中的限界上下文含义重叠)。
Adapter — 在seam处满足接口要求的具体实现。描述的是角色(填补的位置),而非内容(内部的代码)。
Leverage — 深度为调用者带来的价值:每学习一个单元的接口,获得更多能力。一份实现可在N个调用点和M个测试中复用。
Locality — 深度为维护者带来的价值:变更、bug、知识和验证都集中在一处,而非分散在各个调用点。修复一次,处处生效。
Deep vs shallow
深度模块 vs 浅层模块
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?
深度模块 = 简洁接口 + 大量实现:
┌─────────────────────┐
│ Small Interface │ ← 方法少、参数简单
├─────────────────────┤
│ │
│ Deep Implementation│ ← 隐藏复杂逻辑
│ │
└─────────────────────┘浅层模块 = 复杂接口 + 少量实现(应避免):
┌─────────────────────────────────┐
│ Large Interface │ ← 方法多、参数复杂
├─────────────────────────────────┤
│ Thin Implementation │ ← 仅做透传处理
└─────────────────────────────────┘设计接口时,思考以下问题:
- 能否减少方法数量?
- 能否简化参数?
- 能否在内部隐藏更多复杂度?
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.
- 深度是接口的属性,而非实现的属性。 深度模块内部可由小型、可模拟、可替换的部分组成——只是这些部分不属于对外接口。模块可以有内部seam(仅在实现内部使用,供自身测试)和接口处的外部seam。
- 删除测试法。 假设删除该模块。如果复杂度随之消失,说明它只是一个透传模块。如果复杂度分散到N个调用点中重新出现,说明它有存在的价值。
- 接口即测试面。 调用者和测试通过同一个seam交互。如果想要测试接口之外的内容,说明模块的设计可能存在问题。
- 一个适配器意味着假设的seam。两个适配器意味着真实的seam。 除非确实有内容需要跨seam变化,否则不要引入seam。
Designing for testability
为可测试性设计
Good interfaces make testing natural:
-
Accept dependencies, don't create them.typescript
// Testable function processOrder(order, paymentGateway) {} // Hard to test function processOrder(order) { const gateway = new StripeGateway(); } -
Return results, don't produce side effects.typescript
// Testable function calculateDiscount(cart): Discount {} // Hard to test function applyDiscount(cart): void { cart.total -= discount; } -
Small surface area. Fewer methods = fewer tests needed. Fewer params = simpler test setup.
良好的接口让测试变得自然:
-
接收依赖,而非创建依赖。typescript
// 可测试 function processOrder(order, paymentGateway) {} // 难以测试 function processOrder(order) { const gateway = new StripeGateway(); } -
返回结果,而非产生副作用。typescript
// 可测试 function calculateDiscount(cart): Discount {} // 难以测试 function applyDiscount(cart): void { cart.total -= discount; } -
小接口面积。 方法越少,所需测试越少。参数越少,测试设置越简单。
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.
- 一个Module恰好拥有一个Interface(向调用者和测试暴露的表面)。
- Depth是Module的属性,基于其Interface衡量。
- Seam是Module的Interface所在的位置。
- Adapter位于Seam处,满足Interface的要求。
- Depth为调用者带来Leverage,为维护者带来Locality。
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 keyword or a class's public methods: too narrow — interface here includes every fact a caller must know.
interface - "Boundary": overloaded with DDD's bounded context. Say seam or interface.
- 将深度定义为实现代码行数与接口代码行数的比值(Ousterhout):这种方式会鼓励冗余实现。我们采用“深度即杠杆效应”的定义。
- 将"Interface"等同于TypeScript的关键字或类的公共方法:范围过窄——此处的interface包含调用者必须了解的所有信息。
interface - 使用"Boundary":与领域驱动设计中的限界上下文含义重叠。请使用seam或interface。
Going deeper
深入学习
- Deepening a cluster given its dependencies — see DEEPENING.md: dependency categories, seam discipline, and replace-don't-layer testing.
- Exploring alternative interfaces — see 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.
- 基于依赖关系深化代码集群——查看DEEPENING.md:依赖分类、seam规范、“替换而非分层”测试法。
- 探索替代接口设计——查看DESIGN-IT-TWICE.md:启动并行子Agent以多种截然不同的方式设计接口,然后从深度、局部性和seam位置三个维度进行比较。