Legacy Code Refactoring
Safely and incrementally refactor legacy code following SOLID principles and modern best practices.
Paste in your AI
Paste this prompt in ChatGPT, Claude or Gemini and customize the variables in brackets.
Tu es un expert en refactoring et en amélioration de code legacy. Voici un morceau de code [LANGAGE] écrit il y a [ANCIENNETÉ] qui doit être modernisé : ``` [COLLER_LE_CODE_LEGACY] ``` **Contexte du projet :** - Framework actuel : [EX: Express 4, Django 3] - Version cible après refactoring : [EX: Express 5, Django 5] - Contraintes : [EX: pas de changement d'API publique, tests existants doivent passer] - Dette technique identifiée : [DESCRIPTION_OPTIONNELLE] Effectue le refactoring en plusieurs étapes : **Étape 1 - Analyse :** Identifie les anti-patterns, la duplication de code, les responsabilités multiples (violation du SRP), les dépendances cachées et les risques de régression. **Étape 2 - Stratégie :** Propose un plan de refactoring incrémental qui permet de tester chaque étape indépendamment. Identifie les étapes critiques et les points de rollback. **Étape 3 - Code refactorisé :** Produis le code refactorisé complet avec des commentaires expliquant chaque changement important. Respecte les principes SOLID et les patterns appropriés. **Étape 4 - Tests :** Fournis des tests unitaires pour valider que le comportement est préservé après refactoring.
Why this prompt works
<p>This prompt is designed to approach refactoring as a methodical process rather than a haphazard rewrite. The decomposition into four distinct steps (analysis, strategy, code, tests) ensures the AI doesn't jump straight to code without understanding the constraints and risks.</p><p>Mentioning specific constraints (API compatibility, existing tests) is crucial as it forces the AI to produce a realistic and deployable refactoring, not a theoretically ideal code that would break backward compatibility.</p><p>Requesting validation tests in addition to the refactored code is a best practice that transforms this prompt into a retrospective TDD tool, securing each refactoring step with an automated test suite.</p>
Use Cases
Expected Output
A problem analysis, an incremental refactoring plan, the refactored code, and validation unit tests.
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
Source Code Security Audit
Audit your code security according to the OWASP Top 10 with vulnerability identification, exploitation PoC, and fixes.
Debug a Production Error
Quickly analyze a production error with a structured Root Cause Analysis approach and an immediate action plan.
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.
Create a CLI Tool with Node.js
Create a professional Node.js CLI tool with Commander.js, Inquirer, visual feedback, and npm publishing.