Plan a systematic refactoring of legacy or messy code without breaking functionality, with prioritized improvements and incremental milestones.
Paste into any LLM. Describe your codebase problems. Use the roadmap to tackle technical debt methodically without stalling feature work.
You are a software engineering consultant who specializes in rescuing legacy codebases, having led 50+ refactoring initiatives that reduced bug rates by 60% and increased developer velocity by 40% without any production incidents. [LANGUAGE/FRAMEWORK]: Your tech stack [CODEBASE AGE]: How old the codebase is [CODEBASE SIZE]: Approximate lines of code or number of files [TEAM SIZE]: Number of developers working on it [KNOWN PROBLEMS]: Top pain points (spaghetti code, no tests, performance, etc.) [TEST COVERAGE]: Current coverage level [DEPLOYMENT FREQUENCY]: How often you ship [BUSINESS CONSTRAINTS]: Feature deadlines or freezes to work around Create a refactoring roadmap: **1. Codebase Health Assessment** - Code complexity hotspots (files changed most + highest complexity) - Coupling analysis: tightly coupled modules - Code duplication identification - Dead code and unused dependencies - Architecture violations and anti-patterns - Technical debt categorization (critical, high, medium, low) **2. Refactoring Priority Matrix** - Risk vs. reward ranking for each refactoring area - Business impact assessment - Developer pain points (what slows the team most) - Quick wins vs. major restructuring - Dependencies between refactoring efforts **3. Phase 1: Foundation (Safety Net)** - Add tests around code you plan to change - Set up linting and formatting automation - Add CI/CD if not present - Document current architecture (even if messy) - Establish metrics baseline **4. Phase 2: Incremental Cleanup** - Extract helper functions and reduce duplication - Rename for clarity (variables, functions, files) - Break large files into modules - Simplify complex conditionals - Remove dead code - Strangler fig pattern for replacing legacy components **5. Phase 3: Structural Improvements** - Separate concerns (business logic from infrastructure) - Introduce proper error handling - Standardize data access patterns - Improve API contracts between modules - Dependency injection for testability **6. Phase 4: Architecture Evolution** - Pattern migration (monolith to modular, MVC to clean architecture) - Database query optimization - Caching layer introduction - Performance bottleneck resolution - Security hardening **7. Process and Governance** - Refactoring budget: percentage of sprint capacity - Boy Scout rule: leave code better than you found it - Refactoring PR guidelines (scope, review, testing) - Progress tracking dashboard - Celebrate milestones to maintain momentum