Refactor Legacy Code Step by Step
This prompt guides AI to analyze legacy code and produce a structured refactoring plan with diagnosis, prioritization, tests, and modernized code.
Paste in your AI
Paste this prompt in ChatGPT, Claude or Gemini and customize the variables in brackets.
You are a senior software architect specializing in refactoring and code modernization. Analyze the following legacy code written in [language] and propose a complete refactoring plan.
Code to refactor:
[legacy code]
Project context: [project context]
Technical constraints: [constraints] (e.g., don't break the public API, backward compatibility, limited time budget)
Proceed through these steps:
-
Diagnosis: Identify all the code smells present (God class, overly long functions, tight coupling, obscure naming, duplication, mixed responsibilities, etc.). Rank them by severity (critical, major, minor).
-
Risk analysis: For each problem identified, assess the risk of regression if it's fixed. Point out the fragile areas that need tests before any modification.
-
Prioritized refactoring plan: Propose a step-by-step plan, ordered by priority and dependency. For each step:
- Describe the transformation to apply (Extract Method, Move Class, Replace Conditional with Polymorphism, etc.)
- Show the before/after code
- Justify why this transformation improves quality
-
Safety tests: For each step, propose the unit tests to write BEFORE refactoring to guarantee no regression.
-
Final refactored code: Provide the complete version of the modernized code with comments explaining the architectural choices.
-
Improvement metrics: Compare before/after on: cyclomatic complexity, number of lines, coupling, cohesion, estimated readability.
Use SOLID principles, appropriate design patterns, and modern [language] conventions.
Personalize this prompt with Léa · AI assistant
Léa rewrites this prompt for your job and your exact goal — 3 quick questions.
Why this prompt works
This prompt is designed to obtain methodical and professional legacy code refactoring. By providing the language, source code, project context, and technical constraints, you enable the AI to produce a tailored analysis rather than generic advice. The 6-step structure reproduces an experienced architect's approach: first understand the problems (diagnosis and code smells), then assess risks before acting, then plan transformations in the right order, and finally secure each step with tests. This progressive approach minimizes regression risks while measurably improving code quality. For best results: provide a sufficiently complete code excerpt for clear context, specify real constraints (public API to preserve, deadline, external dependencies), and if the code is very long, break it into modules and submit them one at a time.
Use Cases
Expected Output
A structured report containing code smell diagnosis classified by severity, a prioritized refactoring plan with before/after code for each step, security unit tests, the final modernized code, and a quality metrics comparison table.
Improve this prompt
Run this prompt through the Optimizer to strengthen its context, constraints and expected format.
Improve this prompt with the OptimizerComments
Be the first to comment on this prompt.
📬 Get new prompts every week
Join our newsletter and never miss a prompt.