Skip to content

Open-Closed

Medium — good to knowArchitecture

ELI5 — The Vibe Check

Open-Closed means your code should be open for adding new features but closed for editing old working code. Like a power strip — you can plug in new devices without rewiring the wall socket. Add new behavior by extending, not by hacking into existing code.

Real Talk

The Open-Closed Principle states that software entities should be open for extension but closed for modification. New functionality is added by creating new classes or methods rather than modifying existing ones, typically achieved through abstractions, interfaces, and inheritance.

Show Me The Code

// Closed for modification, open for extension
interface DiscountStrategy {
  calculate(price: number): number;
}
class SummerDiscount implements DiscountStrategy {
  calculate(price: number) { return price * 0.8; }
}
class BlackFridayDiscount implements DiscountStrategy {
  calculate(price: number) { return price * 0.5; }
}

When You'll Hear This

"Adding a new payment method shouldn't require editing the checkout class." / "Follow Open-Closed and you'll never break existing tests adding new features."

Made with passive-aggressive love by manoga.digital. Powered by Claude.