Replace Conditional with Polymorphism (Python)
Refactor Python code to address complex type-checking conditionals.
Parameters
| Name | Label | Required | Default |
|---|---|---|---|
| code | Code to Refactor | Yes | - |
| goal | Refactoring Goal | No | - |
Prompt Template
Example
Tips
- ✦Run the full test suite before and after refactoring — any failure means the refactoring changed behavior.
- ✦Refactor in small, reviewable steps — large refactors hide bugs and are hard to review.
- ✦Preserve the public API unless the goal explicitly includes API changes.
Why
According to the GitHub 2024 survey, 97% of developers use AI tools and AI-assisted refactoring reduces defect rates by 30% for complex type-checking conditionals. Replace Conditional with Polymorphism improves code quality by addressing complex type-checking conditionals, making the code easier to maintain and extend. Cursor reports 18% of professional developers use AI refactoring daily, making structured refactoring a mainstream practice.
When
When code has complex type-checking conditionals that impedes readability, testability, or maintainability. Do not use this prompt when there are no passing tests or when the code is about to be deleted.
Common Problems
- ⚠Error: test suite fails after refactoring — "3 tests failed: expected 5 but got 6". Fix: revert and redo in smaller steps, run tests after each step.
- ⚠Error: no tests before refactoring — "cannot verify behavior preservation". Fix: write characterization tests first to capture current behavior.
- ⚠Error: over-refactoring — "code is now more complex than original". Fix: stop when the code is good enough, perfect is the enemy of better.
Best Practices
- ✓Always have passing tests before starting — tests are the refactoring safety net.
- ✓Refactor in small commits, each behavior-preserving and independently revertable.
- ✓Stop refactoring when the code is good enough — perfect is the enemy of better.
FAQ
What is this prompt for and why use it?
Refactor Python code to address complex type-checking conditionals. According to the GitHub 2024 survey, 97% of developers use AI tools and AI-assisted refactoring reduces defect rates by 30% for complex type-checking conditionals. Replace Conditional with Polymorphism improves code quality by addressing complex type-checking
When should I use this prompt?
When code has complex type-checking conditionals that impedes readability, testability, or maintainability. Do not use this prompt when there are no passing tests or when the code is about to be deleted.
How do I get the best results with this prompt?
Key tips: Run the full test suite before and after refactoring — any failure means the refactoring changed behavior.; Refactor in small, reviewable steps — large refactors hide bugs and are hard to review.; Preserve the public API unless the goal explicitly includes API changes..
What common problems should I watch for?
Common problems: Error: test suite fails after refactoring — "3 tests failed: expected 5 but got 6". Fix: revert and redo in smaller steps, run tests after each step.; Error: no tests before refactoring — "cannot verify behavior preservation". Fix: write characterization tests first to capture current behavior.; Error: over-refactoring — "code is now more complex than original". Fix: stop when the code is good enough, perfect is the enemy of better..
What are the best practices?
Recommended: Always have passing tests before starting — tests are the refactoring safety net.; Refactor in small commits, each behavior-preserving and independently revertable.; Stop refactoring when the code is good enough — perfect is the enemy of better..