Improve code design while maintaining all passing tests in the REFACTOR phase of TDD.
Refactors code to improve design while maintaining all passing tests.
/plugin marketplace add DoubleslashSE/claude-marketplace/plugin install node-tdd@doubleslash-pluginsImprove code design while maintaining all passing tests in the REFACTOR phase of TDD.
/refactor [file-path]
file-path: (Optional) Path to the file or directory to refactorUses the reviewer agent first, then the refactorer agent.
tdd-workflowsolid-principlesclean-codefunctional-patternsReview Current State
Prioritize Refactorings
Apply Incrementally
Verify Quality Gates
| Metric | Target |
|---|---|
| SOLID | 90% |
| Clean Code | 90% |
| Test Coverage | 90% |
| TypeScript Strict | 100% |
// Before
const processUser = (user: User) => {
if (!user.email.includes('@')) throw new Error('Invalid');
if (user.password.length < 8) throw new Error('Weak');
// ... more logic
};
// After
const validateEmail = (email: string): Result<string, ValidationError> =>
email.includes('@')
? Result.ok(email)
: Result.fail({ code: 'INVALID_EMAIL' });
const validatePassword = (password: string): Result<string, ValidationError> =>
password.length >= 8
? Result.ok(password)
: Result.fail({ code: 'WEAK_PASSWORD' });
// Before
class UserService {
constructor(private db: Database) {}
async findUser(id: string) { ... }
}
// After
const createUserService = (db: Database) => ({
findUser: async (id: string) => { ... },
});
// Before
const parseConfig = (input: string): Config => {
return JSON.parse(input); // throws
};
// After
const parseConfig = (input: string): Result<Config, ParseError> => {
try {
return Result.ok(JSON.parse(input));
} catch (error) {
return Result.fail(createParseError(error));
}
};
// Before
const processInput = (input: string) => {
const trimmed = input.trim();
const lower = trimmed.toLowerCase();
const validated = validate(lower);
return transform(validated);
};
// After
const pipe = <T>(...fns: Array<(x: T) => T>) =>
(initial: T) => fns.reduce((v, f) => f(v), initial);
const processInput = pipe(
trim,
toLowerCase,
validate,
transform
);
// Before (hardcoded import)
import { logger } from './logger';
const service = { log: () => logger.info('...') };
// After (injected)
type Logger = { info: (msg: string) => void };
const createService = (logger: Logger) => ({
log: () => logger.info('...'),
});
# Must stay GREEN
npm test
# TypeScript strict check
npx tsc --noEmit --strict
# Lint check
npm run lint
# Coverage verification
npm test -- --coverage
Review → Identify Issue → Plan Fix → Apply → Test → Verify
↑__________________________________________________|
The command produces:
/refactor src/services/order-service.ts
Output:
Initial Scores:
SOLID: 72% (Target: 90%)
Clean Code: 65% (Target: 90%)
Applied Refactorings:
1. Extracted validation into separate function
2. Replaced conditional with strategy pattern
3. Introduced Result pattern for error handling
Final Scores:
SOLID: 94% ✓
Clean Code: 91% ✓
Tests: All passing ✓
/tdd - Complete TDD cycle/review - Code review only/green - Implement minimal code