Legacy Code Safe Refactoring with Characterization Tests
Safely modernize fragile legacy codebases using Martin Fowler refactoring patterns and golden master tests.
Interactive Prompt Playground
{{LEGACY_CODE}}{{GOALS}}{{TARGET_STANDARD}}You are a Principal Software Engineer specializing in legacy modernization and Martin Fowler refactoring techniques.
Refactor the following legacy code safely without breaking existing behavior:
Legacy Code:
```
function processReport(data, type, flags, user) {
var result = {};
if (type == 1) {
if (flags & 2) {
if (user.role == 'ADMIN') {
for (var i = 0; i < data.length; i++) {
if (data[i].active && data[i].val > 0) {
result[data[i].id] = data[i].val * 1.15;
}
}
}
}
} else if (type == 2) {
// 80 more lines of nested ifs and magic numbers...
}
return result;
}
```
Refactoring Goals: Decompose into strategy pattern, eliminate magic numbers, introduce strict TypeScript types
Target Language & Standard: Modern TypeScript 5.5 with pure functions
Execute a zero-regression refactoring plan:
1. **Characterization Test Strategy (Golden Master)**:
- Provide a set of characterization tests that capture the current exact behavior (including quirks and legacy edge cases) to serve as a safety net.
2. **Code Smells & Architectural Anti-Patterns**:
- Identify specific smells (God Class, Feature Envy, Long Method, Primitive Obsession, Deep Nesting).
3. **Step-by-Step Micro-Refactoring Sequence**:
- Break the refactoring into 3-5 atomic, safe micro-steps (e.g. Extract Method, Replace Conditional with Polymorphism, Introduce Parameter Object).
4. **Refactored, Idiomatic Code**:
- Provide the clean, modern, fully decoupled implementation.How to Use This Prompt
- Paste your legacy method or module.
- Specify your refactoring goals and language standard.
- Receive characterization tests and a safe, step-by-step refactored solution.
Engineering Tips & Best Practices
- Never refactor code and change its business behavior in the same pull request.
What This Prompt Inspects
Key failure modes, design principles, and quality standards evaluated during execution.
Golden Master Safety Net
Creates characterization tests before touching a single line of production code.
De-nesting & Guard Clauses
Flattens nested arrow anti-patterns into early returns and guards.
Strategy / Polymorphism
Replaces brittle switch/type conditionals with clean extensible strategies.
SprintKit Workflow Integrations
Complementary interactive tools and workflows across SprintKit to accelerate your engineering process.
Related Prompts
Explore related developer prompts in this workflow domain.
God Class & Long Method Decomposition with Design Patterns
Decompose monolithic 1,000+ line classes into Single Responsibility components using SOLID design patterns.
Unit Test Suite Generator with Edge Cases
Generate comprehensive, mutation-tested unit tests with AAA pattern, boundary cases, and proper mock boundaries.
TypeScript Strict Type Safety & Clean Code Review
Audit TypeScript code for type safety, type narrowing, generics, mutation traps, and runtime boundary leaks.