What are common misconceptions about OCP?
Answer
Several common misconceptions surround OCP: (1) "Never modify code" — OCP doesn't mean code can never change; it means that once a module is stable and tested, behavior should be extended without modifying that module. Bug fixes and initial development are exceptions. (2) "Use inheritance for extension" — inheritance is just one mechanism; composition and interfaces are often superior. (3) "Anticipate all variations" — you can't predict every future extension point. OCP should be applied where change has already been observed (after the first variation), not speculatively everywhere. (4) "OCP is about open source" — completely unrelated. (5) "OCP applies to every class" — small utility classes and value objects rarely need OCP treatment. Apply OCP proportionally to volatility: design extension points where change is likely and costly.
Previous
How does SRP apply at the module/package level?
Next
How do SOLID principles apply to domain-driven design?
More SOLID Principles Questions
View all →- Intermediate How would you refactor a class that violates SRP?
- Intermediate How does the Strategy pattern support OCP?
- Intermediate What are the conditions for LSP compliance?
- Intermediate How does ISP prevent "fat interfaces"?
- Intermediate What is the difference between DIP and dependency injection containers?