Complete AI Training

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.

All 10 prompts in this lesson

How to use it

  1. Copy the prompt and paste it into ChatGPT, Claude, Gemini or any other AI.
  2. Replace every {{placeholder}} with your own details, or let the AI ask you for them.
  3. 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

  1. If the code snippet is missing, ask for it.
  2. Analyze the conditional logic and identify the varying behaviors.
  3. Design a polymorphic solution using classes, interfaces, or strategy patterns.
  4. Provide a step-by-step refactoring plan with code examples.
  5. 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?