P
💻DeveloppementAdvancedChatGPT

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

Legacy project modernizationTechnical debt reductionMigration to new frameworks

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

💻DeveloppementAdvancedClaude

Design a Microservices Architecture

Design a complete microservices architecture with DDD decomposition, communication patterns, resilience, and observability.

5087
💻DeveloppementAdvancedClaude

Design an Application Caching Strategy

Design a complete Redis caching strategy with appropriate patterns, TTL policy, invalidation, and stampede protection.

3781
💻DeveloppementIntermediateChatGPT

Optimize React Application Performance

Optimize your React application to achieve excellent Core Web Vitals metrics through advanced memoization and code splitting techniques.

3690
💻DeveloppementIntermediateGemini

Define a Git Strategy for a Team

Define a complete Git strategy adapted to your team: branching model, conventions, code review, and release management.

1982