The Ultimate Code Refactoring & Optimization Assistant
Every developer has faced it: a legacy codebase, a messy script, or an MVP that was rushed out the door. Refactoring is often tedious, but what if AI could not only clean up your code, but explain exactly why the new version is better?
Today’s Top Prompt: The Senior Code Architect
Copy and paste this prompt into Claude or your favorite AI coding assistant to get a comprehensive, secure, and highly optimized refactor of your code.
Act as a Senior Software Architect and Clean Code expert. I am going to provide you with a snippet of code. Your task is to refactor and optimize it based on the following criteria:
1. **Readability & Maintainability:** Apply clean code principles (e.g., DRY, SOLID).
2. **Performance:** Identify and fix bottlenecks (e.g., Big O time/space complexity).
3. **Security:** Ensure there are no vulnerabilities (e.g., injection flaws, unchecked inputs).
4. **Documentation:** Add clear, concise comments and docstrings where appropriate.
Before providing the refactored code, briefly analyze the original code's flaws. Then, present the optimized code. Finally, provide a bulleted list of the exact changes you made and why.
Here is the code:
[INSERT YOUR CODE HERE]
Why This Prompt Works
- Structured Output: It forces the AI to provide a breakdown of flaws before rewriting, ensuring you understand the underlying issues.
- Multi-Dimensional Optimization: It targets performance, security, and readability all at once, rather than just asking for a superficial cleanup.
- Educational Value: The final bulleted list of changes turns the AI into a mentor, helping you improve your own coding skills.
Pro Tips for Best Results
- Specify the Language/Framework: If you are using specific libraries (like React, Pandas, or Express), mention them in the prompt so the AI uses idiomatic patterns.
- Provide Context: If the code interacts with a database or a specific API, let the AI know what data structures to expect.
- Iterative Refactoring: For very large files, feed the code function by function rather than pasting thousands of lines at once.
Real-World Example
We tested this prompt on a messy Python data parsing script that used nested loops (O(n^2) complexity). The AI not only refactored it into a vectorized Pandas operation (reducing runtime from 45 seconds to 0.2 seconds) but also added comprehensive type hinting and exception handling!