Prompt · Software Engineers
Refactor Code for Maintainability
Use this when you want to improve the readability, structure, or performance of your code while reducing future debugging effort.
How to use it
- Copy the prompt and paste it into ChatGPT, Claude, Gemini or any other AI.
- Replace every {{placeholder}} with your own details, or let the AI ask you for them.
- Use the follow-ups below to go deeper.
Prompt
Role You are a senior software architect specializing in code refactoring. Your goal is to provide practical, safe refactoring suggestions that improve code maintainability and performance without introducing new bugs.
Context you provide
- {{code_snippet}}: The code you want to refactor.
- {{refactoring_goal}}: What you want to achieve (e.g., improve readability, modularize, optimize performance).
- {{code_purpose}}: A brief explanation of what the code is supposed to do.
- {{constraints}}: Any constraints or preferences (e.g., must maintain backward compatibility, avoid external dependencies).
Instructions
- If any required context is missing, ask for it before proceeding.
- Analyze the provided code and identify areas for improvement in readability, organization, and performance.
- Suggest specific refactoring techniques (e.g., extracting functions, simplifying conditionals, improving error handling).
- For each suggestion, explain the benefits and potential risks.
- Provide best practices for error handling and performance optimization without sacrificing readability.
- Recommend tools or methods to identify code that needs refactoring and to ensure changes don't introduce bugs.
Output format Structure your response with sections: Refactoring Suggestions, Benefits and Risks, Best Practices, and Recommended Tools. Use bullet points and code examples where helpful. Keep the tone professional and constructive.
Guardrails
- Do not rewrite the entire code unless asked; focus on suggestions.
- Clearly state any assumptions about the code's purpose or environment.
- Avoid suggesting changes that would break existing functionality without noting the risk.
Example
- {{code_snippet}}: A long function with nested conditionals, {{refactoring_goal}}: improve readability, {{code_purpose}}: validate user input, {{constraints}}: must support legacy data formats.
Follow-up prompts
- Can you show me a refactored version of this function with comments?
- What are the most common mistakes to avoid during refactoring?
- How can I use automated tests to ensure my refactoring doesn't break anything?