Use this skill when writing new production code from requirements. Enforces TDD, SOLID principles, and clean code constraints before generating any implementation.
Generate production-quality code from requirements by enforcing TDD (test before implementation), SOLID principles (each one explicitly), cyclomatic complexity limits, and explicit type contracts. Every function produced has tests, typed parameters, typed returns, and a usage example. No implementation exists without a test written first.
debugging-master) or refactoring without behavior change (use refactoring-specialist)Write a failing test first (Red). Implement the simplest code that makes it pass (Green). Then improve the code without breaking the test (). Never write implementation before a test. The test defines the contract; the implementation fulfills it.
if/else chains.AdminUser extends User, every function accepting User must work with AdminUser.Readable + Writable > ReadWriteDeleteArchiveStream.UserService imports IUserRepository (interface); PostgresUserRepo implements it. Swap the DB without touching the service.Do not abstract on first use. Do not abstract on second use. Abstract on third use. Premature abstraction creates wrong abstractions that are harder to remove than the duplication they replaced. Wait for the pattern to emerge.
If a function has more than 10 decision branches (if/else/switch/&&/||/for/while/ternary), split it. Each branch is a test case you must write. A function with complexity 15 needs 15 test paths — that's a sign it does too much.
// BAD: complexity ~12
function processOrder(order: Order): Result {
if (!order) throw new Error('No order');
if (!order.items.length) return { status: 'empty' };
if (order.total > 10000) { /* discount logic */ }
if (order.user.isPremium) { /* premium logic */ }
// ... 8 more branches
}
// GOOD: split by responsibility
function validateOrder(order: Order): void { /* 2 branches */ }
function calculateDiscount(order: Order): number { /* 2 branches */ }
function applyPremiumBenefits(order: Order): Order { /* 2 branches */ }
Every function's signature is a contract. Parameters must be typed. Return values must be typed. Unexpected inputs must throw a typed error or return a typed error variant — never silently return undefined.
// BAD: implicit contract
function getUser(id) { return db.find(id); }
// GOOD: explicit contract
function getUser(id: string): Promise<User> {
if (!id) throw new ArgumentError('id is required');
const user = await db.user.findUnique({ where: { id } });
if (!user) throw new NotFoundError(`User ${id} not found`);
return user;
}
[INPUT: Feature/function requirement]
│
├─► [CLASSIFY: Pure function, stateful module, or integration?]
│ │
│ ├─► [BRANCH A: Pure function (no side effects, no I/O)]
│ │ ├── Define input types + output type + error types
│ │ ├── Write 3 tests: happy path, edge case, error case
│ │ ├── Implement simplest code that passes all 3
│ │ ├── Check complexity ≤ 10 → split if over
│ │ ├── Refactor for clarity (names, structure)
│ │ └── [VALIDATE: tests pass + complexity ok]
│ │
│ ├─► [BRANCH B: Stateful module (class, store, state machine)]
│ │ ├── Define state shape (TypeScript interface)
│ │ ├── Define all valid state transitions
│ │ ├── Write tests for each transition (including invalid ones)
│ │ ├── Implement with explicit state management
│ │ └── [VALIDATE: all transitions tested]
│ │
│ └─► [BRANCH C: Integration (API / DB / external service)]
│ ├── Define interface contract first (input/output/errors)
│ ├── Mock external dependency with typed interface
│ ├── Write tests against mock
│ ├── Implement real adapter matching the interface
│ ├── Write integration test with real dependency
│ └── [VALIDATE: unit + integration tests pass]
│
└─► [OUTPUT: Code + tests + decision log]
Name (verb-noun), input types, output type, throwable errors:
function calculateShippingCost(weight: number, destination: Country): Money
throws ArgumentError // if weight <= 0
describe('calculateShippingCost', () => {
it('should_return_standard_rate_when_domestic', () => {
expect(calculateShippingCost(2.5, 'US')).toEqual({ amount: 5.99, currency: 'USD' });
});
it('should_return_zero_when_weight_is_zero', () => {
expect(() => calculateShippingCost(0, 'US')).toThrow(ArgumentError);
});
it('should_return_international_rate_when_foreign', () => {
expect(calculateShippingCost(2.5, 'DE')).toEqual({ amount: 15.99, currency: 'USD' });
});
});
Write the simplest code that passes all 3 tests. Resist adding logic for cases not covered by a test.
Count decision branches. If > 10, extract sub-functions with descriptive names.
Improve naming, extract constants, align with SOLID. Run tests after each change.
// Usage:
const cost = calculateShippingCost(order.totalWeight, order.shippingAddress.country);
console.log(`Shipping: ${cost.amount} ${cost.currency}`);
npm test — confirm no regressions from new code.
| Error | Cause | Recovery |
|---|---|---|
| Test passes but wrong output | Test assertion too loose (truthy instead of strict equality) | Use toEqual() / toStrictEqual(), not toBeTruthy() |
| Function too long to understand | Violates single responsibility | Find the "and" in what it does — that's the split point |
Type 'X' is not assignable to type 'Y' | Interface mismatch between producer and consumer | Align the types at the interface boundary; never as any to suppress |
| Works in isolation, fails when integrated | Implicit dependencies (global state, env vars, singletons) | Make all dependencies explicit parameters or constructor-injected |
| Tests are brittle, break on refactors | Tests test implementation details, not behavior | Test inputs → outputs, not internal method calls |
any without justification)npm test exits 0Deliverable for every code synthesis:
should_<action>_when_<condition>