PHP Barcelona 2010 - Architecture and testability

64,890 views
64,768 views

Published on

Slides for my talk at PHP Barcelona 2010 on October 29th

Published in: Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
64,890
On SlideShare
0
From Embeds
0
Number of Embeds
66
Actions
Shares
0
Downloads
66
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

PHP Barcelona 2010 - Architecture and testability

  1. 1. Architecture and testability Listen to your tests on steroids Giorgio Sironi
  2. 2. Who I am <ul><li>PHP freelancer from 2005, software architect @ Allbus
  3. 3. Writer for DZone[, php|architect]
  4. 4. Bachelor of Computer Engineering @ PoliMi cum laude </li></ul>
  5. 5. This talk <ul><li>Maintainability, and why testing
  6. 6. Various techniques to favor ease of testing AND maintainability </li></ul>
  7. 7. What is maintainability? <ul><li>Being ready to change!
  8. 8. …but not too much
  9. 9. Taking the first bullet </li></ul>
  10. 10. Favored by maintainability <ul><li>New requirements
  11. 11. Evolving requirements
  12. 12. Iterative development (Agile)
  13. 13. Long-term maintenance </li></ul>
  14. 14. Kinds of tests <ul><li>Unit tests (1 object)
  15. 15. Functional tests (N objects), end-to-end tests (All objects)
  16. 16. Integration tests (external resources) </li></ul>
  17. 17. Maintainability (1 of 2) <ul>SOLID principles (not all of them) <li>Single responsibility
  18. 18. Open/closed
  19. 19. Dependency Inversion </li></ul>
  20. 20. Maintainability (2 of 2) <ul>Loose coupling High cohesion Information hiding ...needed from day 1, like readability and testability </ul>
  21. 21. <ul>That was the boring part... </ul>
  22. 22. Good but... <ul>Good buzzwords, but how do I apply that in practice? </ul>
  23. 23. Testing first <ul><li>Testing first -> Testable code
  24. 24. Testable code -> Maintainable code
  25. 25. Maintainable code -> Happy programmer </li></ul>
  26. 26. Problems with tests <ul><li>A test is slow?
  27. 27. A test is brittle?
  28. 28. A test is difficult to write? </li></ul>
  29. 29. Solution to problems with tests <ul>Change the design to ease testing ( listen to your tests ) </ul>
  30. 30. Examples of listening to the tests <ul><li>Small tests => high cohesion
  31. 31. Unit tests => loose coupling
  32. 32. Fast tests => in-memory isolation
  33. 33. 4 pillars... </li></ul>
  34. 34. 1. Dependency injection <ul><li>No new operators outside of Factories
  35. 35. Easy to change lifecycles and implementations </li></ul>
  36. 36. 1. Dependency Injection (without) <?php class Ferrari { private $engine ; public function __construct () { $this ->engine = new Engine (); } }
  37. 37. 1. Dependency Injection (with) <?php class Ferrari { private $engine ; public function __construct (Engine $engine ) { $this ->engine = $engine ; } }
  38. 38. 2. Avoid static methods <ul><li>Explicit concepts as objects
  39. 39. Orthogonality of collaboration </li></ul>
  40. 40. 2. Avoid static methods (before) <?php class Collection { public function find ( $key ) { Sorter:: sort ( $this ); // ..search stuff } }
  41. 41. 2. Avoid using static methods (after) <?php class Collection { public function find ( $key ) { $this ->sorter-> sort ( $this ); // ..search stuff } }
  42. 42. 3. Law of Demeter <ul><li>Do not walk the... arrows
  43. 43. Or: don't talk to strangers </li></ul>
  44. 44. 3. Law of Demeter (without) <?php class PC { public function powerOn () { $this ->mobo->cpu->cache -> powerOn (); } }
  45. 45. 3. Law of Demeter (with) <?php class PC { public function powerOn () { foreach ( $this ->powered as $item ) { /** @var Powered $item */ $item -> powerOn (); } } }
  46. 46. 3. Law of Demeter (with) <?php class PC { public function powerOn () { $this ->mobo-> powerOn (); } }
  47. 47. 4. Singleton vs. Factory <ul><li>A Singleton is hard to mock
  48. 48. Hidden dependency
  49. 49. Global state </li></ul>
  50. 50. 4. Singleton vs. Factory <?php class Authenticator { public function login ( $user , $pass ) { Db:: select (...); } }
  51. 51. 4. Singleton vs. Factory <?php class Authenticator { public function __construct (Db $db ) { ... } public function login ( $user , $pass ) { $this ->db-> select (...); } }
  52. 52. Be honest <ul><li>If you need something, inject it (or inject its factory)
  53. 53. Telling is better than looking around
  54. 54. Tests are a recipe for clean code </li></ul>
  55. 55. TDD is... <ul>Test-Driven Development Design </ul>
  56. 56. References <ul><li>Guide: Writing Testable Code (Misko Hevery)
  57. 57. Growing Object-Oriented Software, Guided by Tests – Freeman, Price #goos </li></ul>
  58. 58. Contacts <ul><li>Personal blog: http://giorgiosironi.blogspot.com
  59. 59. On Twitter: @giorgiosironi </li></ul>
  60. 60. Questions? Q/A

×