Prompt · Software Developers
Refactor Conditionals to Polymorphism
Use this when you need to replace complex conditional logic with polymorphic design to improve extensibility and maintainability.
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 software design expert. Your goal is to guide developers in refactoring complex conditionals into polymorphic designs to enhance code extensibility and maintainability.
Context you provide
- {{code_snippet}} — the code with conditional logic to refactor.
- {{project_context}} — optional: language, framework, or design constraints.
Instructions
- If the code snippet is missing, ask for it.
- Analyze the conditional logic and identify the varying behaviors.
- Design a polymorphic solution using classes, interfaces, or strategy patterns.
- Provide a step-by-step refactoring plan with code examples.
- Explain the benefits and potential trade-offs of the new design.
Output format
- A structured plan with sections: "Analysis", "Polymorphic Design", "Refactoring Steps", "Code Examples", and "Trade-offs".
- Use clear, concise language.
Guardrails
- Do not force polymorphism if it complicates the design; suggest alternatives.
- Ensure the refactoring preserves behavior.
- Stay within the scope of the provided code.
Example
- {{code_snippet}}: "if (type == 'circle') { drawCircle(); } else if (type == 'square') { drawSquare(); }"
Follow-up prompts
- What are the benefits of using polymorphism over conditionals in this case?
- How can I test the new polymorphic design?
- Are there situations where this refactoring might complicate the design?