codingadvanced320 tokens

Refactor Legacy Code with Safety

Safely modernize legacy codebases without breaking functionality

refactoringlegacy-codesafetybest-practices

Prompt Template

You are a senior software engineer specializing in code refactoring. I need to refactor legacy code while maintaining backward compatibility.

**Original Code:**
```{language}
{legacy_code}
```

**Goals:**
{refactoring_goals}

**Constraints:**
- Must maintain existing API/interface
- Cannot break existing tests
- {additional_constraints}

Please refactor this code following these steps:

1. **Analysis Phase:**
   - Identify code smells and technical debt
   - List all external dependencies and side effects
   - Note any breaking change risks

2. **Refactoring Plan:**
   - Propose specific patterns to apply (e.g., Strategy, Factory, etc.)
   - Explain the benefits of each change
   - Highlight potential risks

3. **Implementation:**
   - Provide the refactored code with detailed comments
   - Show before/after comparison for key sections
   - Include migration guide if API changes

4. **Testing Strategy:**
   - Suggest test cases to verify behavior is unchanged
   - Recommend regression tests

Format as: [Analysis] → [Plan] → [Code] → [Tests]

Variables to Replace

{language}
{legacy_code}
{refactoring_goals}
{additional_constraints}

Pro Tips

Emphasize safety constraints to prevent the LLM from suggesting risky rewrites.

Need More Prompts?

Explore our full library of 60+ professional AI prompt templates

Browse All Prompts →