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

  1. Identify: Find switch statements branching on an Enum or Union Type.
  2. Abstract: Define an Interface that represents the action being performed inside the cases.
  3. Implement: Create a class/struct for each Enum case that implements the Interface.
  4. Inject: Replace the switch statement with a map or registry that provides the correct Strategy implementation.

Examples

Installs
1
First Seen
Apr 5, 2026
replace-enum-switching-with-strategy-pattern — mew-ton/coding-guidelines