Skill Learning

codebase-design

1. Bilingual SKILL.md

左右滑动可直接切换英文版与中文版;两种语言按段落自动对齐。英文中的蓝色虚线词语可点击查看解释。共标记 16 处。

入口 skill:codebase-design左右滑动切换语言 · 段落位置自动对齐
# Codebase Design
Design ****: 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 : 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 ).
**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** — : 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. .
## 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 . - **The interface is the test surface.** Callers and tests cross the same seam. If you want to , the module is probably the wrong shape. - **One adapter means a . 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. **** ```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. **.** 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): . 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 . - **Exploring alternative interfaces** — see [DESIGN-IT-TWICE.md](DESIGN-IT-TWICE.md): to design the interface several , then compare on depth, locality, and seam placement.
# 代码库设计
设计**深模块(deep modules)**:把大量行为置于小型 interface 之后,把它放在清晰的 seam 上,并可通过该 interface 测试。凡是设计或重构代码,都应使用这套语言与原则。目标是让 caller 获得 leverage,让 maintainer 获得 locality,并让所有人都能测试。
## 词汇表
必须准确使用以下术语——不要用 “component”“service”“API” 或 “boundary” 替代。一致的语言正是全部意义所在。
**Module(模块)**——任何同时具有 interface 与 implementation 的东西。它刻意与规模无关:可以是 function、class、package 或跨越多个 tier 的 slice。_避免使用_:unit、component、service。
**Interface(接口)**——caller 为正确使用 module 而必须知道的一切:不仅是 type signature,还包括 invariant、ordering constraint、error mode、required configuration 与 performance characteristic。_避免使用_:API、signature(太窄——它们只指类型层面的表面)。
**Implementation(实现)**——module 内部的东西,即它的代码主体。它不同于 **Adapter**:一个东西可以是小 adapter、大 implementation(如 Postgres repo),也可以是大 adapter、小 implementation(如 in-memory fake)。讨论 seam 时使用 “adapter”;其他时候使用 “implementation”。
**Depth(深度)**——interface 处的 leverage:caller(或 test)每学习一单位 interface,能够驱动的行为数量。大量行为位于小型 interface 之后时,module 是 **deep**;interface 几乎与 implementation 一样复杂时,module 是 **shallow**。
**Seam(接缝)** _(Michael Feathers)_——无需在某处编辑代码,就能改变该处行为的位置;也就是 module 的 interface 所在的*位置*。seam 放在哪里本身就是一个设计决定,与其后放什么不同。_避免使用_:boundary(它与 DDD 的 bounded context 含义重叠)。
**Adapter(适配器)**——在 seam 处满足某个 interface 的具体事物。它描述的是*角色*(填入哪个插槽),而不是实质(内部是什么)。
**Leverage(杠杆效应)**——caller 从 depth 获得的收益:每学习一单位 interface,就获得更多能力。一份 implementation 能在 N 个 call site 与 M 个 test 中反复回报。
**Locality(局部性)**——maintainer 从 depth 获得的收益:变更、bug、知识与验证集中在一处,而不是散落在 caller 中。一处修复,处处生效。
## 深模块与浅模块
**深模块** = 小型 interface + 大量 implementation:
``` ┌─────────────────────┐ │ 小型 Interface │ ← 方法少、参数简单 ├─────────────────────┤ │ │ │ 深层实现 │ ← 隐藏复杂逻辑 │ │ └─────────────────────┘ ```
**浅模块** = 大型 interface + 少量 implementation(应避免):
``` ┌─────────────────────────────────┐ │ 大型 Interface │ ← 方法多、参数复杂 ├─────────────────────────────────┤ │ 薄实现 │ ← 只是透传 └─────────────────────────────────┘ ```
设计 interface 时,要问:
- 能否减少 method 数量? - 能否简化 parameter? - 能否在内部隐藏更多复杂度?
## 原则
- **Depth 是 interface 的属性,而非 implementation 的属性。** deep module 内部可以由小型、可 mock、可替换的部分组成——它们只是不属于 interface。module 既可以有 **internal seam**(implementation 私有,由自身 test 使用),也可以在 interface 处有 **external seam**。 - **删除测试。** 想象删掉这个 module。如果复杂度消失,它就是 pass-through;如果复杂度重新出现在 N 个 caller 中,它就证明了自身价值。 - **Interface 就是 test surface。** caller 与 test 穿过同一个 seam。如果你想越过 interface 进行测试,module 的形状很可能不对。 - **一个 adapter 意味着假设性的 seam;两个 adapter 意味着真实的 seam。** 除非确实有东西会跨越它发生变化,否则不要引入 seam。
## 为可测试性而设计
好的 interface 会让测试自然而然:
1. **接收依赖,不要创建依赖。** ```typescript // 可测试 function processOrder(order, paymentGateway) {} // 难以测试 function processOrder(order) { const gateway = new StripeGateway(); } ```
2. **返回结果,不要产生副作用。** ```typescript // 可测试 function calculateDiscount(cart): Discount {} // 难以测试 function applyDiscount(cart): void { cart.total -= discount; } ```
3. **较小的 surface area。** method 越少,需要的 test 越少;parameter 越少,test setup 越简单。
## 关系
- 一个 **Module** 恰好有一个 **Interface**(呈现给 caller 与 test 的表面)。 - **Depth** 是 **Module** 的属性,要相对于其 **Interface** 衡量。 - **Seam** 是 **Module** 的 **Interface** 所在的位置。 - **Adapter** 位于 **Seam** 上,并满足 **Interface**。 - **Depth** 为 caller 产生 **Leverage**,为 maintainer 产生 **Locality**。
## 被拒绝的框架
- **把 Depth 定义为 implementation 行数与 interface 行数之比**(Ousterhout):这会奖励填充 implementation。我们改用 depth-as-leverage。 - **把 “Interface” 理解为 TypeScript 的 `interface` 关键字或 class 的 public method**:太窄——这里的 interface 包含 caller 必须知道的每个事实。 - **“Boundary”**:与 DDD 的 bounded context 含义重叠。应使用 **seam** 或 **interface**。
## 继续深入
- **根据依赖加深一个 cluster**——参见 [DEEPENING.md](DEEPENING.md):依赖类别、seam discipline,以及 replace-don't-layer testing。 - **探索替代 interface**——参见 [DESIGN-IT-TWICE.md](DESIGN-IT-TWICE.md):启动并行 sub-agent,以数种根本不同的方式设计 interface,再从 depth、locality 与 seam placement 三方面比较。

Entry SHA-256: a8d50abac5a4018f60e1d911d4b6f4e36454ca14d6c390c0695a578c7de65dad

2. Why This Skill Is Clever

一句话核心机制

这是一份共享设计词典:它用一组相互咬合的精确定义,把“模块好不好”转化为可讨论的接口杠杆、接缝位置、调用者收益与维护局部性。

触发与边界

直接来源: frontmatter 把触发范围放在模块接口设计、寻找 deepening 机会、决定 seam、改善可测试性或 AI 可导航性,以及其他技能需要这套词汇时。正文没有交付物、循环或自动重构步骤;它更像可被人或其他技能引用的设计基元,而不是独立执行流程。

支持性上下文: 仓库的 docs/engineering/codebase-design.md 明确称它为 “a reference, not a process”,并提醒若把它当作 session driver,agent 可能自行发明流程。这个说明用于解释调用边界,不属于所选 SKILL.md 原文。

解释: 最稳妥的调用方式是先明确正在设计的 module,再用本技能统一术语和判据;需要搜索代码、主持决策或实施改动时,应由另一个 workflow skill 驱动。

信息架构 / 执行顺序

  1. 先给出目标句:a lot of behaviour behind a small interface
  2. 再锁定七个互相关联的核心词,防止讨论在近义词间漂移。
  3. 用 deep / shallow 图示把抽象定义变成视觉模型。
  4. 用四条原则提供判断测试,再落到依赖注入、返回值和小 surface area 三个可测试性动作。
  5. 最后列出概念关系、拒绝的定义,以及按需深入的两个配套文件。

这不是“照步骤执行”的流程,而是从定义 → 对比 → 判据 → 示例 → 关系 → 排除项 → 延伸阅读逐层收紧含义。

最巧妙的设计点

  1. 先治理语言,再治理代码。 直接来源写道:Consistent language is the whole point. 它不仅定义 module / interface / seam,还明确要求不要用 component、service、API、boundary 替代。统一词汇让设计争论能落在同一对象上,而不是各说各话。

  2. 把 depth 从代码体量改写成调用者收益。 直接来源用 leverage at the interface 定义 depth,并拒绝 implementation-lines / interface-lines 比率。这样就避免了一个会“rewards padding the implementation”的错误指标;真正的问题变成:调用者学习一单位接口,能可靠地驱动多少行为?

  3. 用删除测试识别伪抽象。 If complexity vanishes, it was a pass-through. If complexity reappears across N callers, it was earning its keep. 这把“这一层看起来优雅吗”转化为可反事实检验的问题:删除后,复杂度是消失,还是扩散回调用点?

  4. 让生产调用与测试共享同一接缝。 The interface is the test surface. 不为测试另造一套入口,而是让 caller 与 test 通过同一 interface 观察行为。这样测试更接近真实使用方式,也会反向暴露 interface 形状不佳的问题。

  5. 用第二个 adapter 为抽象收费。 One adapter means a hypothetical seam. Two adapters means a real one. 它把“以后可能会换实现”的猜测变成证据门槛,直接抑制 speculative indirection。

  6. 通过 rejected framings 主动防止概念回退。 技能不只说什么是对的,还说明为何 line ratio、TypeScript interface 与 DDD boundary 都不够准确。负面边界让词汇在长期复用时更稳定。

防失败机制与 trade-off

直接来源中的不变量: interface 包含调用者必须知道的全部事实,而不只是 type signature;depth 是相对于 interface 的 leverage;seam 的位置与其后实现是两个设计决定;没有真实变化就不引入 seam;依赖应被接收、结果应被返回、surface area 应尽量小。

失败预防: 精确术语防止概念混用,deletion test 防止 pass-through module,双 adapter 规则防止过早抽象,同一 test surface 防止测试越过接口并绑定内部实现。

trade-off / 可移植性: 这套小词典有意牺牲近义词自由;团队若已有成熟术语,需要显式映射。DESIGN-IT-TWICE.md 提到 parallel sub-agents,属于 harness 能力假设,并非所有 agent 环境都能直接移植。正文也给原则多、给完整实施机制少:如何用 lint、package boundary 或语言特性强制接口,仍需项目自己决定。

可迁移原则

今日实践题

选你代码库中的一个 repository / service wrapper:如果把它删除,复杂度会消失,还是会重新散落到多个 caller?这个答案说明它是 deep module,还是只是一层 pass-through?

Evidence note

直接来源skills/engineering/codebase-design/SKILL.md 的完整内容与引文;支持性上下文仅使用仓库 docs 页确认“reference, not a process”的定位;其余关于驱动方式、失败模式与可移植性的表述均标为解释,没有把 docs 或配套文件伪装成所选原文。

Source & provenance
Repository path
skills/engineering/codebase-design/SKILL.md
Generated
2026-08-12 08:25 Asia/Shanghai
Upstream commit
84fdeffd12f2ee307994d1eb6feb48173b6e0502
Source
View on GitHub