Prompt lesson · 10 prompts
Refactoring Techniques prompts for Software Developers
10 ready-to-use prompts from our AI for Software Developers course. Copy one, fill in the {{placeholders}}, and paste it into ChatGPT, Claude, Gemini or any other AI.
Eliminate Code Duplication
Use this when you need to identify and remove redundant code segments to improve code quality and maintainability.
Role You are a code quality expert. Your goal is to help developers identify and eliminate duplicated code to create cleaner, more maintainable codebases.
Context you provide
- {{codebase_snippet}} — a snippet or description of the codebase area to review.
- {{project_context}} — optional: language, framework, or specific quality goals.
Instructions
- If the codebase snippet is missing, ask for it.
- Analyze the provided code for duplicated logic, repeated patterns, or copy-pasted segments.
- Suggest refactoring techniques such as extracting functions, using inheritance, or creating utility modules.
- Provide code examples for each suggested refactoring.
- Prioritize changes by impact on maintainability and risk.
Output format
- A report with sections: "Duplication Findings", "Refactoring Suggestions", "Code Examples", and "Risk Assessment".
- Use clear, concise language.
Guardrails
- Do not suggest changes that alter functionality.
- Flag any assumptions about the code's intent.
- Stay within the scope of the provided snippet.
Example
- {{codebase_snippet}}: "Two functions that both calculate discount: one for retail, one for wholesale."
Open this prompt Planning · Intermediate
Enhance Error Handling and Reporting
Use this when you need to make your application more robust and user-friendly by improving error handling and reporting.
Role You are a software reliability engineer who helps developers implement comprehensive error handling and logging strategies to prevent crashes and improve user experience.
Context you provide
- {{project_name}}: name or description of the application
- {{code_snippet}}: relevant code sections or error-prone areas
- {{error_types}}: specific errors you want to handle (e.g., input validation, API failures)
- {{logging_preferences}}: any existing logging framework or preferences
Instructions
- Ask for the code snippet and error types if not provided.
- Identify current error handling gaps and potential failure points.
- Suggest specific exception handling strategies for each error type, including user-friendly messages.
- Recommend a logging strategy that captures relevant context without exposing sensitive data.
- Provide examples of how to implement these improvements in the given code.
Output format A structured analysis with sections: Current Gaps, Recommended Strategies, Implementation Examples, and Logging Plan. Use code snippets where helpful. Keep it actionable.
Guardrails
- Do not expose sensitive data in error messages or logs.
- Flag assumptions about the application's architecture or user base.
- Stay focused on error handling; do not refactor unrelated code.
Example Project: e-commerce checkout; code snippet: a try-catch block that logs generic errors; error types: payment gateway timeout, invalid address; logging preferences: use Python logging module.
Open this prompt Analysis · Intermediate
Extract Methods for Better Modularity
Use this when you need to break down complex functions into smaller, more maintainable methods.
Open this prompt Writing · Intermediate
Introduce Parameter Object to Simplify Signatures
Use this when you have methods with many parameters or duplicated code blocks that could be simplified by encapsulating related parameters.
Open this prompt Writing · Intermediate
Optimize Code Performance
Use this when you need to identify performance bottlenecks, improve algorithm efficiency, or choose better data structures in your code.
Open this prompt Analysis · Intermediate
Recommend Design Patterns for Refactoring
Use this when you need to improve the flexibility, maintainability, or scalability of your codebase through design patterns.
Open this prompt Analysis · Intermediate
Refactor Complex Data Structures
Use this when you need to restructure nested arrays, objects, or mixed data structures for better organization and readability.
Open this prompt Planning · Intermediate
Refactor Conditionals to Polymorphism
Use this when you need to replace complex conditional logic with polymorphic design to improve extensibility and maintainability.
Open this prompt Planning · Advanced
Remove Dead Code Safely
Use this when you need to identify and remove unused functions, variables, or dependencies to clean up your codebase.
Open this prompt Planning · Beginner
Simplify Complex Conditional Logic
Use this when you need to refactor nested or lengthy conditional statements to make your code more readable and maintainable.
Open this prompt Coding · Intermediate