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.
Tu es un architecte logiciel senior spécialisé en refactoring et modernisation de code. Analyse le code legacy suivant écrit en [LANGAGE] et propose un plan de refactoring complet. Code à refactorer : ``` [CODE_LEGACY] ``` Contexte du projet : [CONTEXTE_PROJET] Contraintes techniques : [CONTRAINTES] (ex : ne pas casser l'API publique, compatibilité ascendante, budget temps limité) Procède en suivant ces étapes : 1. **Diagnostic** : Identifie tous les code smells présents (God class, fonctions trop longues, couplage fort, nommage obscur, duplication, responsabilités mélangées, etc.). Classe-les par sévérité (critique, majeur, mineur). 2. **Analyse des risques** : Pour chaque problème identifié, évalue le risque de régression si on le corrige. Indique les zones fragiles qui nécessitent des tests avant toute modification. 3. **Plan de refactoring priorisé** : Propose un plan étape par étape, ordonné par priorité et par dépendance. Pour chaque étape : - Décris la transformation à appliquer (Extract Method, Move Class, Replace Conditional with Polymorphism, etc.) - Montre le code avant/après - Justifie pourquoi cette transformation améliore la qualité 4. **Tests de sécurisation** : Pour chaque étape, propose les tests unitaires à écrire AVANT de refactorer pour garantir la non-régression. 5. **Code refactoré final** : Fournis la version complète du code modernisé avec des commentaires expliquant les choix architecturaux. 6. **Métriques d'amélioration** : Compare avant/après sur : complexité cyclomatique, nombre de lignes, couplage, cohésion, lisibilité estimée. Utilise les principes SOLID, les design patterns appropriés, et les conventions modernes du langage [LANGAGE].
Why this prompt works
<p>This prompt is designed to obtain methodical and professional legacy code refactoring. By providing the <strong>language</strong>, <strong>source code</strong>, <strong>project context</strong>, and <strong>technical constraints</strong>, you enable the AI to produce a tailored analysis rather than generic advice.</p><p>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 <strong>minimizes regression risks</strong> while measurably improving code quality.</p><p>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.</p>
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.
Learn more
Check the full skill on Prompt Guide to master this technique from A to Z.
View on Prompt Guide📬 Get new prompts every week
Join our newsletter and never miss a prompt.
Similar Prompts
Write Integration Tests for an API
Create complete API integration tests with database setup, authentication, CRUD, and end-to-end scenarios.
Create an Optimized Production Dockerfile
Create a multi-stage Dockerfile optimized for production with maximum security, lightweight image, and best practices.
Automatically Generate Unit Tests with AI
Automatically generate an exhaustive unit test suite covering nominal cases, edge cases, and error cases for any source code.
Legacy Code Refactoring
Safely and incrementally refactor legacy code following SOLID principles and modern best practices.