
Open/Closed Doesn't Mean You Never Edit Existing Code
- كاتب موثّق
محتويات المقال
ShippingPolicy, someone adds ShippingPolicyV2 and registers it for checkout. The nightly invoice job still creates the old ShippingPolicy directly, so for a week invoices charge 30 for shipping that checkout showed as free. Meanwhile, adding a wallet payment method meant editing the same switch in checkout, refunds and reports: the one place where OCP would actually have helped.
The common misconception
What actually happens
Meyer's "closed" meant stable and usable
Martin: "This doesn't mean you will never change the source code"
Close against predicted variation, not everything
Wrong code is meant to be edited
The fix: edit the rule, close the real variation
// "OCP says don't touch it", so the new rule became a subclasspublic class ShippingPolicy{ public virtual decimal Fee(decimal subtotal) => 30m;}public sealed class ShippingPolicyV2 : ShippingPolicy{ public override decimal Fee(decimal subtotal) => subtotal >= 500m ? 0m : base.Fee(subtotal);}// Meanwhile each new payment method edits switches like this one// in checkout, refunds and reportspublic static class Fees{ public static decimal Surcharge(string method, decimal total) => method switch { "card" => total * 0.02m, "wallet" => 0m, _ => throw new NotSupportedException(method), };}// The rule was wrong, so fix it where it livespublic sealed class ShippingPolicy{ public decimal Fee(decimal subtotal) => subtotal >= 500m ? 0m : 30m;}// Payment methods really do keep arriving: close checkout against thempublic interface IPaymentMethod{ string Name { get; } decimal Surcharge(decimal total);}public sealed class CardPayment : IPaymentMethod{ public string Name => "card"; public decimal Surcharge(decimal total) => total * 0.02m;}// A new method is a new class plus one registration in Program.csswitch statements. Behind IPaymentMethod, checkout, refunds and reports are closed with respect to new payment methods. Adding one still edits a line of old code, the registration in Program.cs, which is exactly Martin's "little or no old code".
How to apply it in your project
Fix wrong behavior in place When a rule changes or a bug is found, edit the code that owns it, with a test that pins the new behavior. That's not an OCP violation. Find the variation that recurs Search the history for features of the same kind that each touched the same switch,ifchain or type check in several places.Put a stable interface around that point Use an interface and one class per variant, a lookup table, or rules read from configuration, so the next variant is mostly new code. Leave everything else simple Don't add extension points for variations that haven't shown up. Revisit when a second or third change of the same kind arrives.
Key takeaways
OCP doesn't forbid editing code; Martin calls the "never touch it" reading an overstated sound-bite. Meyer's "closed" means a module is stable and usable by others, so clients aren't disturbed. Aim to add expected kinds of change with new code and little or no old code. No program is fully closed: protect the points of predicted variation and pick your battles. Wrong code is meant to be edited; wrapping it in V2 subclasses keeps the bug alive.
📚 المصادر
An Open and Closed Case — Robert C. Martin, The Clean Code Blog (2013) The Open Closed Principle — Robert C. Martin, The Clean Code Blog (2014), quoting Bertrand Meyer's Object Oriented Software Construction (1988) Protected Variation: The Importance of Being Closed — Craig Larman, IEEE Software (2001) Solid Relevance — Robert C. Martin, The Clean Code Blog (2020) Architectural principles — .NET Architecture Guides, Microsoft Learn
التعليقات (0)