Open/Closed Principle
The open/closed principle in SOLID: add new behaviour as new code, without editing classes that already work.
The open/closed principle is the O in SOLID. It asks that a class be open for extension and closed for modification: when a new case arrives, you add a class rather than another branch inside code that already works.
The topic opens with that idea and a shape calculator that breaks it, where every new shape means editing every function that checks a type. A step-by-step refactoring in Python, Java and TypeScript replaces those checks with one interface. Two examples follow: discount rules at a checkout, extended by adding a rule, and Python's json module, extended without editing a line of it.
The topic ends on the limit: extension points cost something, and they belong where change has already happened, not everywhere it might.
By the end you can find the code that changes with every new case, restructure it so that it need not, and decide when an extension point is not yet worth adding.
Contents
8 cardsnot started