المرجع السريعCheat sheet
المبادئ الخمسة وروائح الكود التي تدلّ عليها في صفحة واحدة.The five principles and the code smells that point to them, on one page.
المبادئ الخمسةThe five principles
S Single Responsibility
صنف واحد = عمل واحد = سبب واحد للتغيير.One class = one job = one reason to change.
O Open / Closed
أضف كودًا جديدًا بدل تعديل القديم.Add new code instead of editing old code.
L Liskov Substitution
الابن يعمل في كل مكان يعمل فيه الأب.A child works everywhere its parent works.
I Interface Segregation
واجهات صغيرة: لا أحد يُجبَر على ما لا يحتاج.Small interfaces: nobody is forced to carry what they do not need.
D Dependency Inversion
اعتمد على الواجهات، ومرّر التفاصيل من الخارج.Depend on interfaces; pass the details in from outside.
الرائحة ← المبدأSmell → principle
class does DB + email + rules
S: افصل كل عمل في صنف.S: split each job into its own class.
switch (type) { ... } keeps growing
O: واجهة + صنف لكل نوع.O: an interface + one class per type.
child.method() { throw ... }
L: لا ترث ما لا تستطيع فعله.L: do not inherit what you cannot do.
throw "Not supported"
I: قسّم الواجهة إلى أدوار صغيرة.I: split the interface into small roles.
new ConcreteClient() inside logic
D: مرّره من الخارج عبر واجهة.D: pass it in from outside through an interface.
لا تبالغDo not overdo it
1 → write it, 2 → if, 3 → interface
قاعدة الثلاثة: استخرج الواجهة عند الحاجة الثالثة.Rule of three: extract the interface at the third need.
tests first, then refactor
اكتب اختبارات قبل إعادة التصميم.Write tests before you refactor.
pain → principle
طبّق المبدأ حيث يؤلمك التغيير فقط.Apply a principle only where change hurts.