P
💻DeveloppementAdvancedAll AIs

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

Adapt this prompt

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:

  1. 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).

  2. 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.

  3. 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
  4. Safety tests: For each step, propose the unit tests to write BEFORE refactoring to guarantee no regression.

  5. Final refactored code: Provide the complete version of the modernized code with comments explaining the architectural choices.

  6. 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

Modernize an inherited codebase before adding new featuresPrepare a technical audit by identifying and fixing code smellsProgressively migrate procedural code to object-oriented or functional architecture

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 Optimizer

Comments

Be the first to comment on this prompt.

📬 Get new prompts every week

Join our newsletter and never miss a prompt.

Go further

Similar Prompts

💻DeveloppementIntermediateAll AIs

Set up distributed tracing

Implement distributed tracing

570
💻DeveloppementIntermediateAll AIs

Set up semantic versioning automation

Automate semantic versioning

515
💻DeveloppementIntermediateAll AIs

Generate a code coverage report plan

Plan test coverage improvements

625
💻DeveloppementIntermediateAll AIs

Create a complete REST API

Quickly bootstrap a new API endpoint

446