Lesson 9 of 9 · 26m
Structuring Large FP JavaScript Apps
Module boundaries, dependency direction, testing strategy, and the practical compromises that keep functional codebases shippable.
The final exam for functional JavaScript is not whether you can write a clever pipe. It is whether a large app stays understandable after product requirements, deadlines, and three generations of contributors. Good FP architecture is boring on purpose: pure domain modules, explicit dependencies, thin effectful adapters, and ruthless control over what may import what.
Organize by capability
src/
billing/ invoice.ts, invoice-rules.ts, invoice-repo.ts, invoice-http.ts
checkout/ checkout-flow.ts, checkout-rules.ts, payment-client.ts
platform/ result.ts, clock.tsDomain rules live near the feature that owns them. platform contains only truly cross-cutting primitives. A global functional/ or helpers/ directory becomes a dumping ground because nothing there has an owner.
Enforce the dependency direction
ui -> flows -> rules
-> adapters -> network/dbPure rule modules import types and utilities, not IO. Repositories and clients adapt IO into plain data or Result values. Application flows compose rules and adapters. UI reads state and dispatches intent; it does not contain pricing rules. If a rule imports fetch or the DOM, the boundary has already failed.
Test the core without the shell
test("staff discount wins over seasonal discount", () => {
const rule = decidePriceRule(staffUser, cart150, december);
assert.deepEqual(rule, { kind: "discount", percent: 20 });
});Most behavior tests need no browser, network mocks, or database fixtures. Save integration tests for adapter contracts and critical flows. A suite that mocks the world to test addition is architecture failure masquerading as thoroughness.
Dependencies are values
type CheckoutDeps = {
clock: { now: () => Date };
logger: { info: (msg: string, data?: unknown) => void };
payments: PaymentClient;
};Wire dependencies in one composition root. Hidden singleton imports scattered through the tree make every module look pure while secretly depending on process state.
Pragmatism beats purity points
Sometimes a class-based ORM model, framework lifecycle object, or mutable performance-critical structure is the right tool. Functional architecture should isolate those choices, not shame them. The goal is localized reasoning: understand a feature by reading one folder and running fast deterministic tests.
Field notes
- Dependency cycles are the first sign domain boundaries are wrong.
- Narrow public module APIs make refactors possible; exporting every helper does not.
- Framework-specific code belongs at the edge. Frameworks rotate faster than business rules.
- Measure abstraction by replacement cost. If swapping HTTP libraries touches fifty files, the adapter boundary was fiction.
Was this lesson helpful?