replace-enum-switching-with-strategy-pattern
Installation
SKILL.md
Summary
Do not use switch statements to handle behavior based on Enum types. Instead, define an Interface and implement a concrete Strategy for each Enum case.
Rationale
- Open/Closed Principle (OCP): You should be able to add new behaviors (new enum cases) by adding new classes, without modifying existing code (the huge switch statement).
- Safety: Adding a new Enum value often leads to "missing case" bugs in switch statements scattered across the codebase. Strategies enforce implementation via the interface contract.
- Cognitive Load: Eliminates long, complex procedural branches in favor of small, focused classes.
Guidance
- Identify: Find
switchstatements branching on an Enum or Union Type. - Abstract: Define an Interface that represents the action being performed inside the cases.
- Implement: Create a class/struct for each Enum case that implements the Interface.
- Inject: Replace the switch statement with a map or registry that provides the correct Strategy implementation.