Code Refactoring
Improve the structure and readability of your code without changing behavior.
Prompt template
Refactor the following [PROGRAMMING LANGUAGE] code and improve its structure and maintainability. **Refactoring Goals:** - [MAIN GOAL: e.g., improve readability, remove duplicates, apply SOLID principles] **Please consider:** 1. **Identify Code Smells:** What problematic patterns do you see? (Long Methods, God Classes, Feature Envy, etc.) 2. **Refactoring Strategy:** Describe your approach step by step 3. **Design Improvements:** Suggest appropriate design patterns where useful 4. **Naming:** Improve variable and function names for better clarity 5. **Modularization:** Break large functions into smaller, reusable units **Important:** - The external behavior of the code must NOT change - Explain the reasoning behind each refactoring step - Show the complete refactored code Code: ```[PROGRAMMING LANGUAGE] [INSERT CODE HERE] ```
Replace placeholders in [square brackets]: [PROGRAMMING LANGUAGE][INSERT CODE HERE]
How to use this template
Copy the prompt, replace the placeholders in square brackets with your details and paste it into the AI assistant of your choice. The more precise your details, the better the result.
Tips for better results
- Define a clear refactoring goal upfront
- Mention existing tests so the AI can ensure correctness
- Ask for a before-and-after complexity comparison
- Request incremental refactoring rather than one big rewrite
Suitable open models
The selection names current open models as a starting point for your own tests. It is not a measurement for this template.
Keywords: #refactoring #clean-code #design-patterns #maintainability