advanced · 25 minute study
Give each rule one authoritative home
Duplicating business rules in a form, API and export eventually creates three different answers. Reuse is valuable when it preserves one explicit contract.
Useful preparation · 2 prerequisites
Use this as a map, not a lock. If these ideas are familiar, continue.
- Choose the test that can actually prove the claim — One green test is not evidence for every layer of a feature. The right test reaches the boundary where the claimed behavior is enforced.
- Authorize the actual resource on every entry path — A protected page can still call an unprotected endpoint. Security must survive a direct request that skips the intended interface.
First, build a mental model
The domain describes what the product means. Presentation asks and displays; adapters translate external systems. Dependencies should not make the rule depend on the screen that happens to use it.
- Keep a pure rule independent of React, DOM, network clients and environment secrets when those are not part of its meaning.
- Pass explicit inputs and return explicit results/errors. Validate untrusted values at boundaries before a typed domain function relies on them.
- Sharing a library does not automatically make it the persisted authority. Security, concurrency and atomic invariants still need enforcement where trusted state changes.
- Prefer one small cohesive package over a speculative generic framework. Add an abstraction because actual consumers share a contract, not because a diagram looks elegant.
type Line = { quantity: number; unitCents: number };
export function lineCents(line: Line): number {
if (!Number.isSafeInteger(line.quantity) || line.quantity < 1 ||
!Number.isSafeInteger(line.unitCents) || line.unitCents < 0) {
throw new Error('Invalid synthetic line');
}
const total = line.quantity * line.unitCents;
if (!Number.isSafeInteger(total)) throw new Error('Out of range');
return total;
}- Input contract
The toy rule accepts positive integer quantities and nonnegative integer cents within the safe range.
- Rule
One exported calculation serves its deliberately limited purpose without knowing about React or HTTP.
- Boundary
Real persistence still needs its own authorization and integrity; this function is not a financial engine.
Production thinking
Take this beyond the example.
Map canonical readers/writers before refactoring. Keep persisted precision, authorization, retries and historical compatibility explicit. Synthetic examples teach the pattern without exposing proprietary rules.
Sources and further reading
Your evidence
unseen is a local learning signal, not a credential or proof of production competence.
Explain the mental model, then explicitly acknowledge it. Opening a page is not evidence.
Supported retries can complete this lesson. Independent evidence and delayed review determine the skill stage.
Next lesson · Treat AI output as a hypothesis until verified →