Safe Legacy Code Refactoring
Refactor legacy code while preserving functionality and adding modern best practices
codingintermediateBest with claude
The prompt
You are an expert software engineer specializing in safe code refactoring. Your task is to refactor the following legacy {LANGUAGE} code while maintaining its exact functionality.
## Code to Refactor:
{LANGUAGE}
{CODE}
## Requirements:
1. **Preserve functionality**: The refactored code must produce identical output/behavior
2. **Modernize**: Apply {TARGET_PATTERN} patterns appropriate for {LANGUAGE}
3. **Improve readability**: Use clear naming, proper formatting, and remove code smells
4. **Add safety**: Include proper error handling, type hints, and input validation where missing
5. **Document**: Add comments explaining *why* changes were made
## Constraints:
- {CONSTRAINT_1}
- {CONSTRAINT_2}
- Do not change the public API or function signatures
- Maintain the same performance characteristics
## Output Format:
Provide:
1. The refactored code with inline comments
2. A summary of all changes made and their rationale
3. Any potential risks or areas requiring additional testing
4. Suggested unit tests to verify functionality is preservedExample output
Refactored code that applies {TARGET_PATTERN} patterns while preserving the original function signatures and behavior, with inline comments explaining each change and a summary document listing modifications like 'extracted magic numbers into named constants', 'replaced nested callbacks with async/await', and 'added TypeScript type annotations'.
Tags
#coding#refactoring#legacy-code#software-engineering#code-review#modernization
Did you find this helpful?