Loading…

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

Like this presentation? Why not share!

PHP Barcelona 2010 - Architecture and testability

on

  • 67,071 views

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

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

Statistics

Views

Total Views
67,071
Views on SlideShare
67,009
Embed Views
62

Actions

Likes
2
Downloads
63
Comments
0

4 Embeds 62

http://www.giorgiosironi.com 23
http://phpcoe.mimos.my 18
http://giorgiosironi.blogspot.com 15
http://www.scoop.it 6

Accessibility

Categories

Upload Details

Uploaded via as OpenOffice

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

PHP Barcelona 2010 - Architecture and testability PHP Barcelona 2010 - Architecture and testability Presentation Transcript

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