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
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:
- Problem analysis: list the code smells you detect (naming, duplication, complexity, coupling...)
- Refactoring plan: steps ordered by priority and risk
- Refactored code: a modernized, commented version
- Comparison: a before/after table for each major change
- Regression tests: minimal test cases to confirm behavior is preserved
- 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
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 OptimizerComments
Be the first to comment on this prompt.
Glossary Terms
📬 Get new prompts every week
Join our newsletter and never miss a prompt.