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 :
-
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).
-
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.
-
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é
-
Tests de sécurisation : Pour chaque étape, propose les tests unitaires à écrire AVANT de refactorer pour garantir la non-régression.
-
Code refactoré final : Fournis la version complète du code modernisé avec des commentaires expliquant les choix architecturaux.
-
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].
Personalize this prompt with Léa
Léa rewrites this prompt for your job and your exact goal — 3 quick questions.
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.
Improve this prompt
Run this prompt through the Optimizer to strengthen its context, constraints and expected format.
Improve this prompt with the OptimizerComments
- LéaAI
Ajoute une étape 0 : génère d'abord des tests de caractérisation (golden master) pour chaque bloc non testé. Cela sécurise le refactoring et affine le diagnostic. Pour un code très volumineux, découpe-le en plusieurs modules distincts et traite-les un par un.
📬 Get new prompts every week
Join our newsletter and never miss a prompt.
Go further
Similar Prompts
Sora Prompt for Generating Unit Tests
Sora, developed by OpenAI, is primarily known as a video generation model, but its AI ecosystem can be leveraged for software development tasks, including generating unit tests. By crafting precise and structured prompts, you can harness Sora's contextual understanding capabilities to produce robust test suites covering nominal cases, edge cases and error scenarios. Automated unit test generation saves developers significant time while improving code coverage. A good prompt must specify the programming language, desired testing framework, expected behavior of the function to be tested, and project-specific constraints. This systematic approach ensures maintainable, readable tests that follow industry best practices. Whether you are working on a REST API, utility library or complex application, the prompts presented in this guide will help you produce professional-quality unit tests in seconds.
ChatGPT Prompt for Generating Python Code
ChatGPT has become an indispensable tool for Python developers, whether beginners or experienced. Thanks to its advanced understanding of natural language, it can transform a functional description into clean, structured, and functional Python code in seconds. But the quality of the generated code depends directly on the quality of the prompt used. A vague prompt will produce generic and often unusable code, while a well-structured prompt will generate production-ready code with error handling, typing, and documentation. In this guide, we provide optimized prompts to get the most out of ChatGPT for generating Python code. Whether you need to write an automation script, a REST API, a data processing algorithm, or a complex class, these prompts will help you obtain precise and professional results. The goal is to save you time while maintaining a high level of quality, guiding ChatGPT with the right instructions so that it respects PEP 8 conventions, includes docstrings, and produces maintainable code.
Implement Kubernetes network policies
Secure Kubernetes networking
Automate Your Git Commits with AI
This prompt analyzes a Git diff and automatically generates structured commit messages following project conventions, with atomic splitting if needed.