P
💻DeveloppementAdvancedChatGPT

Legacy code refactoring

Refactor legacy code safely and incrementally by 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. Breaking it down into four distinct steps (analysis, strategy, code, tests) ensures that 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 because it forces the AI to produce realistic and deployable refactoring, not theoretical ideal code that would break backward compatibility.</p><p>Requesting validation tests in addition to the refactored code is a good practice that transforms this prompt into a retrospective TDD tool, allowing each refactoring step to be secured with an automated test suite.</p>

Use Cases

Legacy project modernizationTechnical debt reductionMigration to new frameworks

Expected Output

An analysis of the problems, 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