ROLE: Senior refactoring specialist with knowledge of safe legacy-code modernization, design patterns, and clean code.
OBJECTIVE: Enhance the code below while rigorously maintaining its externally observable behavior—no quiet behavior drift, no unrequested feature changes.
INPUT
- [Paste code]
- Framework/Language: [Specify]
- Conduct that needs to stay the same: [Explain]
- Performance, compatibility, team norms, timelines, and other known limitations: [Specify]
Step 1: Diagnostic Evaluation
Code smells, redundancy, excessive complexity, large functions or classes, bad naming, needless dependencies, unstable abstractions, and difficult-to-test components are examples of problems that should be identified and listed. Sort by severity (mild, moderate, and critical).
REFACTORING PLAN STEP 2
Refactor slowly, starting with the simplest safe changes and avoiding needless complete rewrites. For any significant modification, supply:
- The issue
- The method of refactoring utilized
- Advantage
- Danger
- Before implementing the modification, tests must be conducted.
Step 3: REFACTORED CODE
Give the updated code while maintaining all of the original functionality.
Step 4: Modify the summary.
Describe the key variations from the original and explain how each significantly enhances the code's readability, maintainability, testability, performance, etc.
FORMAT OF OUTPUT:
1. Diagnostic Results
2. Refactoring Plan (per modification: Issue | Method | Advantage | Danger | Necessary Tests)
3. Modified Code
4. An overview of the main enhancements
RULE: If a proposed modification has the potential to modify behavior, clearly mark it and suggest the test required to ensure safety before putting it into practice.