P
IntermediateClaude

Refactor Legacy Python Code

Analyzes legacy Python code, identifies code smells and produces a commented refactored version with a prioritized action plan and regression tests.

Paste in your AI

Adapt this prompt

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

You are a Python expert with extensive experience refactoring legacy code. Analyze and refactor the following code.

Target Python version: [target python version] (e.g., 3.11+)
Allowed libraries: [allowed libraries]
Main constraint: [main constraint] (e.g., no changes to the public interface)

Legacy code to refactor:

[legacy code]

Follow this approach:

  1. Problem analysis: list the code smells you detect (naming, duplication, complexity, coupling...)
  2. Refactoring plan: steps ordered by priority and risk
  3. Refactored code: a modernized, commented version
  4. Comparison: a before/after table for each major change
  5. Regression tests: minimal test cases to confirm behavior is preserved
  6. Metrics: cyclomatic complexity before/after if possible

Prioritize: readability > performance > elegance.

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

The explicit prioritization (readability > performance > elegance) frames AI judgment according to real-world priorities. The before/after comparison table makes refactoring value immediately visible. Requesting regression tests ensures the refactoring remains safe.

Use Cases

Modernizing Python 2 code to Python 3Improving maintainability of a critical modulePreparing code for new developer onboarding

Expected Output

A list of code smells, prioritized refactoring plan, commented modernized code, before/after comparison table and regression 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

    Précisez la version Python cible (ex 3.11+) et la liste exacte des librairies autorisées. Sans cela, le modèle peut proposer des fonctionnalités (match/case, structural pattern matching) ou des dépendances non disponibles dans votre environnement, rendant le refactoring inapplicable.

📬 Get new prompts every week

Join our newsletter and never miss a prompt.

Go further