Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Code Quality Series-2 SOLID Principles

Code Quality Series-2 SOLID Principles

Slides of PHP Reboot meetup on 'SOLID Principles' under 'Code Quality' meetup series.

Meet up video is also uploaded to PHPReboot youtube channel (Check slide 3).

Kapil Sharma

March 24, 2018
Tweet

More Decks by Kapil Sharma

Other Decks in Technology

Transcript

  1. PHPReboot.com KapilSharma.info About me • Kapil Sharma • VP Technology

    at DJ Alexander • Working in web application development since 13+ years. • Twitter: @kapilsharmainfo • Facebook: /kapilsharmainfo • Linked in: kapilsharmainfo • Website: kapilsharma.info • Slides: speakerdeck.com/kapilsharma 2
  2. PHPReboot.com KapilSharma.info About PHP Reboot • Developers’ community in Pune,

    India. • Conducts regular meet ups • meetup.com/phpreboot • phpreboot.com • Twitter: @phpreboot • Facebook: /PHReboot • Slack: phpreboot.signup.team • Youtube: www.youtube.com/channel/ UCbLir8nYAFJ5YpcbjEFzjVw 3 Please subscribe, so that we can get custom Channel URL like youtube.com/phpreboot (Need 100 subscribers to get custom URL) Sponsored by JetBrains (IDE License) DJ Alexander (Space and snacks) Ansh Systems (Space and snacks)
  3. PHPReboot.com KapilSharma.info – Martin Fowler “Any fool can write code

    that Computer can understand. Good programmers write code that Humans can understand.”
  4. PHPReboot.com KapilSharma.info Easy to extend 9 O of SOLID Open

    Close Principle Our code must be Open for Extension But Close for Modification
  5. PHPReboot.com KapilSharma.info How to achieve? • Readable: Less time to

    understand what code is doing. • Simple - KISS • Single responsibility (SOLID) • Small class/methods • Consistent standards • Commonly agreed Naming convention 10 Easy to read Easy to extend • Extendability: Easy to meet new requirements without breaking existing code • DRY • Open-Close (SOLID) • Small class/method • Moduler • Continuous refactoring (consider TDD)
  6. PHPReboot.com KapilSharma.info What is SOLID? 12 S O L I

    D ingle Responsibility Model pen Close Principle iskov Substitution Principle nterface Segregation Principle ependency Inversion Principle
  7. PHPReboot.com KapilSharma.info Single Responsibility Model 13 Easy to read Easy

    to extend A class should have a single responsibility. Statement A class should have only one reason to change. UNIX Principle Do one thing and do it well.
  8. PHPReboot.com KapilSharma.info Refactoring in SRM • Smells • Long method

    • Large class • Switch statement • Divergent Change • Feature Envy • Inappropriate Intimacy 16
  9. PHPReboot.com KapilSharma.info Open-Close Principle 17 Easy to read Easy to

    extend Open for Extension Statement Close for modification Detailed Example (previous meet up of the series) https://www.youtube.com/watch?v=uVJtkgEr5Sk
  10. PHPReboot.com KapilSharma.info Liskov Substitution Principle 20 Easy to read Easy

    to extend Objects of sub-class must be able to replace object of parent class, without impacting program Statement Child class should never replace functionality of parent.
  11. PHPReboot.com KapilSharma.info Interface Segregation Principle 23 Easy to read Easy

    to extend No client should be forced to depend on methods it do not use. Statement Many client specific interface are better than one general purpose interface How?
  12. PHPReboot.com KapilSharma.info Dependency Inversion Principle 26 Easy to read Easy

    to extend Low level classes (concrete functionality) needed by high level classes (Business logic) What is Dependency? Register -> send registration mail High Level Low Level <<depends>> Mailer Register -> Save data ORM/PDO
  13. PHPReboot.com KapilSharma.info Dependency Inversion Principle 27 Easy to read Easy

    to extend High level module should not depend on low level module Statement Both should depend on Abstraction Abstraction should not depend one details Details should depends on Abstraction
  14. PHPReboot.com KapilSharma.info Dependency Inversion Principle - Example 29 https://code.tutsplus.com/tutorials/solid-part-4-the-dependency-inversion-principle--net-36872 High

    level module should not depend on low level module Both should depend on Abstraction Abstraction should not depend one details Details should depends on Abstraction
  15. PHPReboot.com KapilSharma.info What is SOLID? 31 S O L I

    D ingle Responsibility Model pen Close Principle iskov Substitution Principle nterface Segregation Principle ependency Inversion Principle