Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Got units? @ Osidays 2011 India 11-20-2011

1,416 views

Published on

A TDD tutorial on implementing Dijkstra's shortest path algorithm in PHP with test-driven development and PHPUnit.

Published in: Technology, Self Improvement
  • Be the first to comment

Got units? @ Osidays 2011 India 11-20-2011

  1. 1. Got units? Bengaluru, 20-22 November 2011
  2. 2. TDDBengaluru, 20-22 November 2011
  3. 3. REDadd a test, it wont pass Bengaluru, 20-22 November 2011
  4. 4. GREENimplement the necessary code to make the test pass Bengaluru, 20-22 November 2011
  5. 5. REFACTOR polish the code Bengaluru, 20-22 November 2011
  6. 6. TDD-ing Dijkstrasshortest path algorithm in PHP Bengaluru, 20-22 November 2011
  7. 7. Bengaluru, 20-22 November 2011
  8. 8. Bengaluru, 20-22 November 2011
  9. 9. Bengaluru, 20-22 November 2011
  10. 10. Bengaluru, 20-22 November 2011
  11. 11. Bengaluru, 20-22 November 2011
  12. 12. Bengaluru, 20-22 November 2011
  13. 13. Bengaluru, 20-22 November 2011
  14. 14. Bengaluru, 20-22 November 2011
  15. 15. Bengaluru, 20-22 November 2011
  16. 16. github.com/odino/osidays Bengaluru, 20-22 November 2011
  17. 17. git checkout $stepBengaluru, 20-22 November 2011
  18. 18. Step 0/1● create the directory structure● add Symfony2 classloader as submodule● init and update submodule● (optional) create an empty class Bengaluru, 20-22 November 2011
  19. 19. Step 2● write the test with the smallest amount of code you think its necessary Bengaluru, 20-22 November 2011
  20. 20. Step 3● add the Vertex class● add the Graph class● add an empty solve() method for the algorithm class Bengaluru, 20-22 November 2011
  21. 21. Step 4● implement the solve method Bengaluru, 20-22 November 2011
  22. 22. Step 5● added all the methods needed by the Dijkstra::solve()● need to implement Graph::calculatePotentials() Bengaluru, 20-22 November 2011
  23. 23. Step 6● a test is added for the Graph class Bengaluru, 20-22 November 2011
  24. 24. Step 7● calculatePotentials() is tested● half of our test-suite passes● switch back to the Dijkstras test Bengaluru, 20-22 November 2011
  25. 25. Step 8● minor tweaks to make all the tests pass Bengaluru, 20-22 November 2011
  26. 26. Step 9● potentials are assigned in the Graph class but they should belong to the algorithm, what about refactoring the code?● since we move the only tested method of Graph into Dijkstras class, we can delete the GraphTest Bengaluru, 20-22 November 2011
  27. 27. Step 10● since we like to be OO, we can connect vertices through objects, and not arrays anymore Bengaluru, 20-22 November 2011
  28. 28. Step 11● we now need to fix the code which uses vertices connections as arrays Bengaluru, 20-22 November 2011
  29. 29. Step 12● the library seems pretty complete● PHPDoc is added Bengaluru, 20-22 November 2011
  30. 30. Step 13● Oooops, we forgot to test a scenario: in Dijkstras algorithm we need vertices connected by positive distance● a test is added, verifying an exception is raised through annotations Bengaluru, 20-22 November 2011
  31. 31. Step 14● implementation Bengaluru, 20-22 November 2011
  32. 32. Step 15● enter Mocking objects Bengaluru, 20-22 November 2011
  33. 33. Step 16● Dijkstra::getGraph() seems to be useless so we probably dont need any Graph class Bengaluru, 20-22 November 2011
  34. 34. Step 17● removed Graph class Bengaluru, 20-22 November 2011
  35. 35. Step 18● some tests rely on it, so we need to eliminate old references to Graph Bengaluru, 20-22 November 2011
  36. 36. Step 19● enter Data Providers Bengaluru, 20-22 November 2011
  37. 37. Step 20● what about calculating the path between not-connected vertices? It should return null, so code is refactored Bengaluru, 20-22 November 2011
  38. 38. Step 21● Dijkstra::solve() seems to do too much things, so we split the method● first demand potentials calculation● second demand path finding● third demand raising the negative-potentials exception● enter phploc● enter code-coverage Bengaluru, 20-22 November 2011
  39. 39. Alessandro Nadalin Bengaluru, 20-22 November 2011
  40. 40. odino.orgBengaluru, 20-22 November 2011
  41. 41. Bengaluru, 20-22 November 2011
  42. 42. @_odino_ #osidaysBengaluru, 20-22 November 2011
  43. 43. REST in peace: tomorrow, 12.45 Bengaluru, 20-22 November 2011
  44. 44. Thank YOU! @_odino_ Bengaluru, 20-22 November 2011

×