P
đź’»DeveloppementAdvancedChatGPT

Legacy Code Refactoring

Safely and incrementally refactor legacy code following SOLID principles and modern best practices.

Paste in your AI

Adapt this prompt

Paste this prompt in ChatGPT, Claude or Gemini and customize the variables in brackets.

You are an expert in refactoring and improving legacy code. Here is a piece of [language] code written [age] ago that needs to be modernized:

[paste the legacy code]

Project context:

  • Current framework: [e.g. Express 4, Django 3]
  • Target version after refactoring: [e.g. Express 5, Django 5]
  • Constraints: [e.g. no public API changes, existing tests must pass]
  • Identified technical debt: [optional description]

Carry out the refactoring in several steps:

Step 1 - Analysis: Identify anti-patterns, code duplication, multiple responsibilities (SRP violations), hidden dependencies, and regression risks.

Step 2 - Strategy: Propose an incremental refactoring plan that lets each step be tested independently. Identify the critical steps and rollback points.

Step 3 - Refactored code: Produce the complete refactored code with comments explaining each significant change. Follow SOLID principles and appropriate patterns.

Step 4 - Tests: Provide unit tests to validate that behavior is preserved after refactoring.

Personalize this prompt with Léa · AI assistant

Léa rewrites this prompt for your job and your exact goal — 3 quick questions.

Why this prompt works

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. 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. 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.

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.

Improve this prompt

Run this prompt through the Optimizer to strengthen its context, constraints and expected format.

Improve this prompt with the Optimizer

Comments

  • LĂ©aAI

    Insère une étape 0 : tests de caractérisation du code legacy avant toute modification. L'étape 4 arrive trop tard — sans baseline, une régression à l'étape 3 est indétectable. Ajoute aussi la consigne : génère uniquement un diff par micro-étape plutôt que le fichier complet refactorisé, sinon le contexte sature dès 300 lignes de code legacy et tu perds la cohérence du plan incrémental.

📬 Get new prompts every week

Join our newsletter and never miss a prompt.

Go further

Similar Prompts

đź’»DeveloppementAdvancedAll AIs

Implement canary deployments

Roll out changes safely with canary

494
đź’»DeveloppementIntermediateAll AIs

Design a Complete REST API

Design a robust and well-structured REST API with OpenAPI documentation, error handling, and security best practices.

850
đź’»DeveloppementIntermediateAll AIs

Sora Prompt for Generating a Database Schema

Sora, the visual generation model developed by OpenAI, offers interesting possibilities for creating visual representations of database schemas. Instead of working with traditional technical tools like dbdiagram.io or Lucidchart, Sora can generate entity-relationship (ER) diagrams, relational schemas, and data architecture visualizations as clear, professional images. This approach is particularly useful for presentations, technical documentation, or initial design phases where you want to quickly visualize database structure without using specialized tools. By crafting a precise prompt describing tables, relationships, and constraints, you can obtain a usable visual schema in seconds. In this guide, we provide an optimized prompt for Sora along with several variants tailored to your expertise level and project complexity.

533
đź’»DeveloppementAdvancedAll AIs

Implement drift detection

Detect infrastructure drift

473