codebase-design

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

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.
请严格使用以下术语——不要替换为"component"、"service"、"API"或"boundary"。保持语言一致性是核心目的。
Module — 指任何拥有接口和实现的事物。刻意不局限于规模:可以是一个函数、类、包,或是跨层级的代码片段。避免使用:unit、component、service。
Interface — 调用者正确使用模块必须了解的所有信息:包括类型签名,还包括不变量、顺序约束、错误模式、所需配置以及性能特征。避免使用:API、signature(范围过窄——它们仅指代类型层面的表层内容)。
Implementation — 模块内部的代码主体。与Adapter有所区别:某个事物可以是实现内容庞大的小型适配器(如Postgres仓库),或是实现内容精简的大型适配器(如内存模拟实现)。当讨论的主题是接缝时使用“adapter”,其他情况使用“implementation”。
Depth(深度)—— 接口的杠杆效应:调用者(或测试)每学习一个单位的接口,能够使用的行为数量。当大量行为隐藏在少量接口之后时,模块是deep(深度)的;当接口复杂度几乎与实现相当时,模块是shallow(浅层)的。
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.
  • 深度是接口的属性,而非实现的属性。 深度模块内部可以由小型、可模拟、可替换的部分组成——只是这些部分不属于接口。模块可以拥有internal seams(内部接缝)(对实现私有,供自身测试使用),以及接口处的external seam(外部接缝)。
  • 删除测试法。想象删除该模块。如果复杂度随之消失,说明它只是一个透传模块。如果复杂度重新出现在N个调用者中,说明它发挥了应有的作用。
  • 接口即测试面。调用者和测试通过同一个接缝。如果想要测试越过接口的内容,说明模块的设计可能存在问题。
  • 一个适配器意味着假设的接缝。两个适配器意味着真实的接缝。 除非确实有内容会跨接缝变化,否则不要引入接缝。

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.
良好的接口让测试更自然:
  1. 接受依赖,而非创建依赖。
    typescript
    // Testable
    function processOrder(order, paymentGateway) {}
    
    // Hard to test
    function processOrder(order) {
      const gateway = new StripeGateway();
    }
  2. 返回结果,而非产生副作用。
    typescript
    // Testable
    function calculateDiscount(cart): Discount {}
    
    // Hard to test
    function applyDiscount(cart): void {
      cart.total -= discount;
    }
  3. 小接口面积。 方法越少,所需测试越少。参数越少,测试设置越简单。

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(向调用者和测试展示的表层)。
  • DepthModule的属性,基于其Interface衡量。
  • SeamModuleInterface所在的位置。
  • 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
    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.
  • 将深度定义为实现代码行数与接口代码行数的比值(Ousterhout提出):这种方式会鼓励填充实现代码。我们改用“深度即杠杆效应”的定义。
  • 将"Interface"等同于TypeScript的
    interface
    关键字或类的公共方法
    :范围过窄——此处的interface包含调用者必须了解的所有信息。
  • 使用"Boundary":因DDD的限界上下文概念而语义过载。请使用seaminterface

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以多种截然不同的方式设计接口,然后从深度、局部性和接缝位置维度进行比较。