
الـ Open/Closed مش معناه إنك متعدّلش الكود القديم أبدًا
- كاتب موثّق
محتويات المقال
ShippingPolicy، ضاف ShippingPolicyV2 وسجّلها للـ checkout. والـ invoice job اللي بيشتغل بالليل لسه بيعمل ShippingPolicy القديمة بنفسه، فلمدة أسبوع الفواتير بتحاسب على 30 شحن، والـ checkout كان عارضه ببلاش. وفي نفس الوقت، إضافة wallet كـ payment method احتاجت تعديل نفس الـ switch في الـ checkout والـ refunds والـ reports: المكان الوحيد اللي الـ OCP كان هيفيد فيه فعلًا.
الفكرة الغلط الشائعة
اللي بيحصل فعلًا
الـ "closed" عند Meyer كان معناها ثابت وجاهز للاستخدام
Martin: "This doesn't mean you will never change the source code"
اقفل قدام التغيير المتوقع، مش قدام كل حاجة
الكود الغلط معمول عشان يتعدّل
الحل: عدّل القاعدة، واقفل قدام التغيير الحقيقي
// "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. ومع IPaymentMethod، الـ checkout والـ refunds والـ reports بقوا closed with respect to الـ payment methods الجديدة. وإضافة واحدة لسه بتعدّل سطر في كود قديم، الـ registration في Program.cs، وده بالظبط "little or no old code" اللي Martin بيقول عليه.
تطبقها إزاي في مشروعك
صلّح السلوك الغلط في مكانه لما قاعدة تتغيّر أو تلاقي bug، عدّل الكود اللي ماسكها، ومعاه test يثبّت السلوك الجديد. ده مش مخالفة للـ OCP. دوّر على التغيير اللي بيتكرر دوّر في الـ history على features من نفس النوع كل واحدة فيهم لمست نفس الـ switchأو سلسلة الـifأو الـ type check في كذا مكان.حط interface ثابتة حوالين المكان ده استخدم interface و class لكل نوع، أو lookup table، أو rules بتتقري من الـ configuration، عشان النوع الجاي يبقى معظمه كود جديد. سيب الباقي بسيط ماتضيفش extension points لتغييرات لسه ماظهرتش. وارجع بُص تاني لما تيجي تاني أو تالت مرة من نفس النوع.
الخلاصة
الـ OCP مش بيمنع تعديل الكود، و Martin بيقول إن قراية "ماتلمسوش" دي جملة مبالغ فيها. الـ "closed" عند Meyer معناها إن الـ module ثابت وجاهز يستخدمه غيره، فالـ clients مايتأثروش. الهدف إن أنواع التغيير المتوقعة تتضاف بكود جديد، وبتعديل قليل أو مفيش في الكود القديم. مفيش برنامج مقفول بالكامل: احمي أماكن التغيير المتوقع واختار معاركك. الكود الغلط معمول عشان يتعدّل، ولفّه في V2 subclasses بيسيب الـ bug عايش.
📚 المصادر
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)