Your SlideShare is downloading. ×
0
PHP Barcelona 2010 - Architecture and testability
PHP Barcelona 2010 - Architecture and testability
PHP Barcelona 2010 - Architecture and testability
PHP Barcelona 2010 - Architecture and testability
PHP Barcelona 2010 - Architecture and testability
PHP Barcelona 2010 - Architecture and testability
PHP Barcelona 2010 - Architecture and testability
PHP Barcelona 2010 - Architecture and testability
PHP Barcelona 2010 - Architecture and testability
PHP Barcelona 2010 - Architecture and testability
PHP Barcelona 2010 - Architecture and testability
PHP Barcelona 2010 - Architecture and testability
PHP Barcelona 2010 - Architecture and testability
PHP Barcelona 2010 - Architecture and testability
PHP Barcelona 2010 - Architecture and testability
PHP Barcelona 2010 - Architecture and testability
PHP Barcelona 2010 - Architecture and testability
PHP Barcelona 2010 - Architecture and testability
PHP Barcelona 2010 - Architecture and testability
PHP Barcelona 2010 - Architecture and testability
PHP Barcelona 2010 - Architecture and testability
PHP Barcelona 2010 - Architecture and testability
PHP Barcelona 2010 - Architecture and testability
PHP Barcelona 2010 - Architecture and testability
PHP Barcelona 2010 - Architecture and testability
PHP Barcelona 2010 - Architecture and testability
PHP Barcelona 2010 - Architecture and testability
PHP Barcelona 2010 - Architecture and testability
PHP Barcelona 2010 - Architecture and testability
PHP Barcelona 2010 - Architecture and testability
PHP Barcelona 2010 - Architecture and testability
PHP Barcelona 2010 - Architecture and testability
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

PHP Barcelona 2010 - Architecture and testability

64,551

Published on

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

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,551
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
64
Comments
0
Likes
2
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

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

×