IntermediateClaude
Refactor legacy Python code
Analyze legacy Python code, identify code smells and produce a refactored commented version with prioritized action plan and non-regression tests.
Paste in your AI
Paste this prompt in ChatGPT, Claude or Gemini and customize the variables in brackets.
Tu es un expert Python avec une forte expérience en refactoring de code legacy. Analyse et refactorise le code suivant. **Version Python cible :** [VERSION_PYTHON] (ex: 3.11+) **Librairies autorisées :** [LIBRAIRIES_AUTORISEES] **Contrainte principale :** [CONTRAINTE] (ex: pas de changement d'interface publique) **Code legacy à refactoriser :** ```python [CODE_LEGACY] ``` Suis cette démarche : 1. **Analyse des problèmes** : liste les code smells détectés (nommage, duplication, complexité, couplage...) 2. **Plan de refactoring** : étapes ordonnées par priorité et risque 3. **Code refactorisé** : version modernisée commentée 4. **Comparaison** : tableau avant/après pour chaque changement majeur 5. **Tests de non-régression** : cas de tests minimaux pour valider que le comportement est préservé 6. **Métriques** : complexité cyclomatique avant/après si possible Priorise : lisibilité > performance > élégance.
Why this prompt works
Explicit prioritization (readability > performance > elegance) frames the AI's judgment according to real field priorities. The before/after comparison table makes the value of refactoring immediately visible. The request for non-regression tests ensures that refactoring remains safe.
Use Cases
Modernize Python 2 code to Python 3Improve maintainability of a critical modulePrepare code for onboarding new developers
Expected Output
List of code smells, prioritized refactoring plan, commented modernized code, before/after comparison table and non-regression tests.
Learn more
Check the full skill on Prompt Guide to master this technique from A to Z.
View on Prompt Guide