Back to Academy
coding principles
Open/Closed Principle
The Open/Closed Principle (OCP) states that software entities should be open for extension but closed for modification. Instead of editing existing, tested code every time a new requirement appears, you design abstractions (interfaces, base classes, or higher-order functions) that allow new behavior to be added by writing new code. This minimizes regression risk and keeps the existing codebase stable while still allowing the system to evolve.
Use Cases
- •Adding new payment methods without modifying the checkout pipeline
- •Plugin architectures where third parties extend functionality without touching core code
- •Strategy pattern implementations for interchangeable algorithms
- •Middleware stacks in web frameworks (Express, Koa) where new behavior is added as new middleware
Visualization
Implementation
Output
Click "Run Code" to see output...