Advertisement

Don't be STUPID, Grasp SOLID - North East PHP

Developer Advocate
Aug. 17, 2013
Advertisement

More Related Content

Advertisement
Advertisement

Don't be STUPID, Grasp SOLID - North East PHP

  1. Don’t Be STUPID Grasp SOLID! Anthony Ferrara NorthEastPHP 2013
  2. Let’s Talk Object Oriented Programming
  3. What Is An Object?
  4. Classic View Object == Physical “Thing”
  5. Classic View Object == Physical “Thing” Methods == Actions on “Thing”
  6. Classic View Object == Physical “Thing” Methods == Actions on “Thing” Properties == Description of “Thing”
  7. Animal MammalBird Fish CatCow Dog Lion Feline Cheetah
  8. Classic View $lion = new Lion; $lion->roar(); $lion->walkTo($point); $lion->hunt($zebra); $lion->sleep();
  9. Is This Realistic?
  10. Classic View $lion = new Lion; $lion->roar(); $lion->walkTo($point); $lion->hunt($zebra); $lion->sleep();
  11. Classic View $lion = new Lion; $lion->roar(); $lion->walkTo($point); $lion->hunt($zebra); $lion->sleep(); Does A Lion Have A Button To Make It Roar?
  12. Classic View $lion = new Lion; $lion->roar(); $lion->walkTo($point); $lion->hunt($zebra); $lion->sleep(); Does A Lion Have A Button To Make It Roar?What Does It Mean For An Object To “Hunt”?
  13. Classic View $lion = new Lion; $lion->roar(); $lion->walkTo($point); $lion->hunt($zebra); $lion->sleep(); Does A Lion Have A Button To Make It Roar?What Does It Mean For An Object To “Hunt”? What Is A Lion In Relation To Our Application?
  14. The Classical Model Is Easy To Understand
  15. The Classical Model Is Completely Impractical
  16. “Modern” View Object == Collection Of (Related) Behaviors
  17. “Modern” View Object == Collection Of (Related) Behaviors Methods == Behavior
  18. “Modern” View Object == Collection Of (Related) Behaviors Methods == Behavior Properties == Details Of Behavior
  19. Classic View == “(conceptually) is a” Modern View == “behaves as a”
  20. interface Number { function getValue(); function __toString(); function add(Number $n); function subtract(Number $n); function equals(Number $n); function isLessThan(Number $n); function isGreaterThan(Number $n); }
  21. Number IntegerFloat Decimal longshort long long unsigned signed
  22. But Wait!
  23. We Don’t Even Need Inheritance
  24. All We Need Is Polymorphism And Encapsulation
  25. Polymorphism Behavior Is Determined Dynamically “Dynamic Dispatch”
  26. Procedural Code if ($a instanceof Long) { return new Long($a->getValue() + 1); } elseif ($a instanceof Float) { return new Float($a->getValue() + 1.0); } elseif ($a instanceof Decimal) { return new Decimal($a->getValue() + 1.0); }
  27. Polymorphic Code return $int->add(new Integer(1));
  28. Polymorphic Code class Integer implements Number { public function add(Number $a) { return new Integer( $this->getValue() + (int) $a->getValue() ); } }
  29. Polymorphic Code class Float implements Number { public function add(Number $a) { return new Float( $this->getValue() + (float) $a->getValue() ); } }
  30. Encapsulation Behavior Is Completely Contained By The Object’s API (Information Hiding)
  31. Procedural Code if (5 == $number->value) { print “Number Is 5”; } else { print “Number Is Not 5”; }
  32. Encapsulated Code if ($number->equals(new Integer(5))) { print “Number Is 5”; } else { print “Number Is Not 5”; }
  33. Encapsulated Code class Decimal implements Number { protected $intValue; protected $exponent; public function equals(Number $a) { if ($a instanceof Decimal) { // Compare Directly } else { // Cast } } }
  34. Behavior Is Defined By The API
  35. Two Types Of Primitive APIs Interfaces (Explicit)
  36. Two Types Of Primitive APIs Interfaces (Explicit) Duck Typing (Implicit)
  37. If an Object Is A Collection Of Behaviors...
  38. What Is A Collection Of Classes/Objects?
  39. APIs Method
  40. APIs Method MethodMethod Class
  41. APIs Method MethodMethod Class ClassClass Package
  42. APIs Method MethodMethod Class ClassClass Package PackagePackage Library
  43. APIs Method MethodMethod Class ClassClass Package PackagePackage Library Framework LibraryLibrary
  44. What Makes A Good API?
  45. A Good API Does One Thing
  46. A Good API Never Changes
  47. A Good API Behaves Like Its Contract
  48. A Good API Has A Narrow Responsibility
  49. A Good API Depends Upon Abstractions
  50. And That’s SOLID Code
  51. S - Single Responsibility Principle O- L - I - D- A Good API Does One Thing
  52. S - Single Responsibility Principle O- Open / Closed Principle L - I - D- A Good API Never Changes
  53. S - Single Responsibility Principle O- Open / Closed Principle L - Liskov Substitution Principle I - D- A Good API Behaves Like Its Contract
  54. S - Single Responsibility Principle O- Open / Closed Principle L - Liskov Substitution Principle I - Interface Segregation Principle D- A Good API Has A Narrow Responsibility
  55. S - Single Responsibility Principle O- Open / Closed Principle L - Liskov Substitution Principle I - Interface Segregation Principle D- Dependency Inversion Principle A Good API Depends Upon Abstractions
  56. S - Single Responsibility Principle O- Open / Closed Principle L - Liskov Substitution Principle I - Interface Segregation Principle D- Dependency Inversion Principle
  57. Note That SOLID Does Not Dictate What Is Good OOP
  58. SOLID Emerges From Good OOP
  59. So, What Makes A Bad API?
  60. Global Variables (Spooky Action At A Distance)
  61. Depending On Specifics Of An Implementation
  62. Hidden Dependencies
  63. Unhealthy Focus On Performance
  64. Poorly Named APIs
  65. Duplication
  66. And That’s STUPID Code
  67. S - Singletons T - U - P - I - D- Global Variables (Spooky Action At A Distance)
  68. S - Singletons T - Tight Coupling U - P - I - D- Depending On Specifics Of An Implementation
  69. S - Singletons T - Tight Coupling U - Untestable Code P - I - D- Hidden Dependencies
  70. S - Singletons T - Tight Coupling U - Untestable Code P - Premature Optimization I - D- Unhealthy Focus On Performance
  71. S - Singletons T - Tight Coupling U - Untestable Code P - Premature Optimization I - Indescriptive Naming D- Poorly Named APIs
  72. S - Singletons T - Tight Coupling U - Untestable Code P - Premature Optimization I - Indescriptive Naming D- Duplication Duplication Duplication DuplicationDuplication Duplication Duplication Duplication Duplication
  73. S - Singletons T - Tight Coupling U - Untestable Code P - Premature Optimization I - Indescriptive Naming D- Duplication
  74. STUPID Embodies Lessons Learned From Bad OOP
  75. What Good OOP Gives You Modular Code Reusable Code Extendable Code Easy To Read Code Maintainable Code Easy To Change Code Easy To Understand Code Clean Abstractions (mostly)
  76. What Good OOP Costs You Tends To Have More “Layers” Tends To Be Slower At Runtime Tends To Have Larger Codebases Tends To Result In Over-Abstraction Tends To Require More Effort To Write Tends To Require More Tacit Knowledge
  77. Let’s Look At Some Code!
  78. interface Car { public function turnLeft(); public function turnRight(); public function goFaster(); public function goSlower(); public function shiftUp(); public function shiftDown(); public function start(); }
  79. interface Steerable { public function steer($angle); } interface Acceleratable { public function accelerate($amt); } interface Shiftable { public function shiftDown(); public function shiftUp(); }
  80. interface Transmission { public function getNumberOfGears(); public function getGear(); public function shiftToNeutral(); public function shiftToReverse(); public function shiftToGear($gear); } interface Manual extends Transmission { public function engageClutch(); public function releaseClutch(); } interface Automatic extends Transmission { public function shiftToDrive(); }
  81. Principle Of Good Enough
  82. Anthony Ferrara joind.in/8907 @ircmaxell blog.ircmaxell.com me@ircmaxell.com youtube.com/ircmaxell
Advertisement