advanced
Open/Closed Principle
Design stable extension seams for real variation while avoiding speculative plugin points that make simple code harder to understand.
OCP: software entities should be open for extension, closed for modification—add new behavior without editing stable core code. Achieve via strategy interfaces, plugin registries, configuration-driven handlers, or composition instead of growing switch statements.
Extension points should match real variation frequency—not every `if (type)` deserves a factory.
On interviews: replace a payment switch with extensible registration and discuss when OCP is overkill.
Common pitfalls: speculative plugin frameworks, abstract factories for two cases, and "closed" code that still requires core edits for every feature.
The trade-off is extensibility versus comprehension cost of indirection.
Checklist:
- Identify real variation dimensions.
- Extend via new types, not editing switches.
- Skip speculative extension hooks.