Share
Embed
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
Transcript
34 slides
-
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 -
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) -
PHPReboot.com KapilSharma.info Why we are here? 4 Code Quality Series
2. SOLID Principles -
PHPReboot.com KapilSharma.info What is quality code? • Easy to read.
5 • Easy to extend. -
PHPReboot.com KapilSharma.info – Martin Fowler “Any fool can write code
that Computer can understand. Good programmers write code that Humans can understand.” -
PHPReboot.com KapilSharma.info – Edsger W. Dijkstra “Simplicity is prerequisite for
reliability” “Simple is better” -
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 -
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) -
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 -
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. -
PHPReboot.com KapilSharma.info Refactoring in SRM • Smells • Long method
• Large class • Switch statement • Divergent Change • Feature Envy • Inappropriate Intimacy 16 -
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 -
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. -
PHPReboot.com KapilSharma.info Liskov Substitution Principle - Example 21 Rectangle -
Square problem Every square is a Rectangle -
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? -
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 -
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 -
PHPReboot.com KapilSharma.info Dependency Inversion Principle - Example 28 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 -
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 -
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 -
PHPReboot.com KapilSharma.info What is quality code? • Easy to read.
32 • Easy to extend.