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.
设计深度模块:以简洁的接口封装大量行为,接口位于清晰的接缝处,可通过该接口进行测试。无论在设计或重构代码时,都可使用这套术语和原则。目标是为调用方提供复用价值,为维护者实现代码本地化,同时让所有人都能轻松测试。
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仓库),也可能是封装了少量实现的大型适配器(如内存模拟实现)。当讨论接缝时使用"adapter",其他场景使用"implementation"。
Depth(深度) — 接口的复用价值:调用方(或测试)每学习一个单元的接口,能够调用的行为数量。当大量行为封装在简洁接口之后时,模块是深度的;当接口复杂度几乎与实现相当时,模块是浅层的。
Seam(接缝) (Michael Feathers) — 无需修改代码即可变更行为的位置;即模块接口所在的位置。接缝的位置是独立的设计决策,与接口背后的实现无关。避免使用:boundary(与DDD中的限界上下文含义重叠)。
Adapter(适配器) — 在接缝处满足接口要求的具体实现。描述的是角色(填补的位置),而非内容(内部的代码)。
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 │ ← Few methods, simple params
├─────────────────────┤
│ │
│ Deep Implementation│ ← Complex logic hidden
│ │
└─────────────────────┘浅层模块 = 复杂接口 + 少量实现(应避免):
┌─────────────────────────────────┐
│ Large Interface │ ← Many methods, complex params
├─────────────────────────────────┤
│ Thin Implementation │ ← Just passes through
└─────────────────────────────────┘设计接口时,思考以下问题:
- 能否减少方法数量?
- 能否简化参数?
- 能否在内部隐藏更多复杂度?
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.
- 深度是接口的属性,而非实现的属性。 深度模块内部可由小型、可模拟、可替换的部分组成——只是这些部分不属于接口。模块既可以有内部接缝(仅在实现内部使用,供自身测试),也可以有位于接口处的外部接缝。
- 删除测试法。 假设删除该模块。如果复杂度随之消失,说明它只是一个透传模块。如果复杂度重新出现在N个调用点,说明它具备存在价值。
- 接口即测试面。 调用方和测试通过同一个接缝。如果你想要测试接口之外的部分,说明模块的设计可能存在问题。
- 一个适配器意味着假设的接缝。两个适配器意味着真实的接缝。 除非确实存在跨接缝的变化,否则不要引入接缝。
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
// Testable function processOrder(order, paymentGateway) {} // Hard to test function processOrder(order) { const gateway = new StripeGateway(); } -
返回结果,而非产生副作用。typescript
// Testable function calculateDiscount(cart): Discount {} // Hard to test 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":与DDD中的限界上下文含义重叠。请使用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:依赖分类、接缝规范以及“替换而非分层”测试法。
- 探索替代接口设计 — 详见DESIGN-IT-TWICE.md:启动并行子Agent以多种截然不同的方式设计接口,然后从深度、本地化和接缝位置三个维度进行比较。