| 1 | --- |
| 2 | name: codebase-design |
| 3 | description: 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. |
| 4 | --- |
| 5 | |
| 6 | # Codebase Design |
| 7 | |
| 8 | 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. |
| 9 | |
| 10 | ## Glossary |
| 11 | |
| 12 | Use these terms exactly: don't substitute "component," "service," "API," or "boundary." Consistent language is the whole point. |
| 13 | |
| 14 | **Module**: anything with an interface and an implementation. Deliberately scale-agnostic: a function, class, package, or tier-spanning slice. _ |
| 15 | |
| 16 | **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. _ |
| 17 | |
| 18 | **Implementation**: what's inside a module, its body of code. Distinct from ** |
| 19 | |
| 20 | **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 ** |
| 21 | |
| 22 | **Seam** _ |
| 23 | |
| 24 | **Adapter**: a concrete thing that satisfies an interface at a seam. Describes * |
| 25 | |
| 26 | **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. |
| 27 | |
| 28 | **Locality**: what maintainers get from depth. Change, bugs, knowledge, and verification concentrate in one place rather than spreading across callers. Fix once, fixed everywhere. |
| 29 | |
| 30 | ## Deep vs shallow |
| 31 | |
| 32 | **Deep module** = small interface + lots of implementation: |
| 33 | |
| 34 | ``` |
| 35 | ┌─────────────────────┐ |
| 36 | │ Small Interface │ ← Few methods, simple params |
| 37 | ├─────────────────────┤ |
| 38 | │ │ |
| 39 | │ Deep Implementation│ ← Complex logic hidden |
| 40 | │ │ |
| 41 | └─────────────────────┘ |
| 42 | ``` |
| 43 | |
| 44 | **Shallow module** = large interface + little implementation (avoid): |
| 45 | |
| 46 | ``` |
| 47 | ┌─────────────────────────────────┐ |
| 48 | │ Large Interface │ ← Many methods, complex params |
| 49 | ├─────────────────────────────────┤ |
| 50 | │ Thin Implementation │ ← Just passes through |
| 51 | └─────────────────────────────────┘ |
| 52 | ``` |
| 53 | |
| 54 | When designing an interface, ask: |
| 55 | |
| 56 | - Can I reduce the number of methods? |
| 57 | - Can I simplify the parameters? |
| 58 | - Can I hide more complexity inside? |
| 59 | |
| 60 | ## Principles |
| 61 | |
| 62 | - **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 |
| 63 | - **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. |
| 64 | - **The interface is the test surface.** Callers and tests cross the same seam. If you want to test |
| 65 | - **One adapter means a hypothetical seam. Two adapters means a real one.** Don't introduce a seam unless something actually varies across it. |
| 66 | |
| 67 | ## Designing for testability |
| 68 | |
| 69 | Good interfaces make testing natural: |
| 70 | |
| 71 | 1. **Accept dependencies, don't create them.** |
| 72 | |
| 73 | ```typescript |
| 74 | // Testable |
| 75 | function processOrder(order |
| 76 | |
| 77 | // Hard to test |
| 78 | function processOrder(order |
| 79 | const gateway = |
| 80 | } |
| 81 | ``` |
| 82 | |
| 83 | 2. **Return results, don't produce side effects.** |
| 84 | |
| 85 | ```typescript |
| 86 | // Testable |
| 87 | function calculateDiscount(cart |
| 88 | |
| 89 | // Hard to test |
| 90 | function applyDiscount(cart |
| 91 | cart.total -= |
| 92 | } |
| 93 | ``` |
| 94 | |
| 95 | 3. **Small surface area.** Fewer methods = fewer tests needed. Fewer params = simpler test setup. |
| 96 | |
| 97 | ## Relationships |
| 98 | |
| 99 | - A **Module** has exactly one |
| 100 | - **Depth** is a property of a |
| 101 | - A **Seam** is where a |
| 102 | - An **Adapter** sits at a |
| 103 | - **Depth** produces |
| 104 | |
| 105 | ## Rejected framings |
| 106 | |
| 107 | - **Depth as ratio of implementation-lines to interface-lines** (Ousterhout): rewards padding the implementation. We use depth-as-leverage instead. |
| 108 | - **"Interface" as the TypeScript `interface |
| 109 | - **"Boundary"**: overloaded with DDD's bounded context. Say |
| 110 | |
| 111 | ## Going deeper |
| 112 | |
| 113 | - **Deepening a cluster given its dependencies**, see |
| 114 | - **Exploring alternative interfaces**, see |