Stage 2 · Lesson 2 of 7
Open for extension, closed for modification
The says you should be able to add behaviour by adding new code, not by editing code that already works. The usual way is an interface: checkout takes a Discount, and a new student discount is a new class, so the tested checkout code is never touched.
In languages with first-class functions, the "new class" can be a new function. The benefit is safety. Every edit to working code risks breaking it, while a new class can only break itself.
But extension points cost something: more types and more indirection. Add one when the second or third variation actually appears, not before.
Common trapbuilding plug-in points for changes nobody has asked for, leaving a design full of interfaces with one implementation.
A question to check yourself, an AI practice session and 1 cheat sheet entry come with the full roadmap. Unlock
Resources for this stage
Start with· follow one of these
- CoursePaidGrokking SOLID Design Principles
Design Gurus · Beginner · $50 one-time
One chapter per principle, each with the violation and the refactoring that fixes it
- CourseFreeMIT 6.102: Software Construction
MIT · Intermediate
Free: the Specifications and Interfaces and Subtyping readings explain substitution as contracts