The Power Of Refactoring (PHPNW)


Published on

The "The Power of Refactoring" talk as given at PHPNW08 in Manchester on November 22nd, 2008

Published in: Technology
1 Comment
No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

The Power Of Refactoring (PHPNW)

  1. 1. The Power of Refactoring Why doing things three times may speed up development The Power of Refactoring
  2. 2. What will we do? • What is refactoring? • Why refactoring? • It saves time? • Some examples The Power of Refactoring
  3. 3. Introduction • Professional Services Consultant at Ibuildings • Secretary of Dutch PHP Usergroup • symfony advocate • husband and father (2 times) • 7 cats and a bunch of fish The Power of Refactoring
  4. 4. Refactoring The Power of Refactoring
  5. 5. Refactoring is a disciplined technique for restructuring an existing body of code, altering its internal structure without changing its external behavior. Its heart is a series of small behavior preserving transformations. Each transformation (called a 'refactoring') does little, but a sequence of transformations can produce a significant restructuring. Since each refactoring is small, it's less likely to go wrong. The system is also kept fully working after each small refactoring, reducing the chances that a system can get seriously broken during the restructuring. (Martin Fowler - The Power of Refactoring
  6. 6. Code refactoring is the process of changing a computer program's code to make it amenable to change, improve its readability, or simplify its structure, while preserving its existing functionality. (wikipedia) The Power of Refactoring
  7. 7. So basically... • Improve small parts of the code for... • Readability • Performance improvement • Implementation or change of logic The Power of Refactoring
  8. 8. But watch out! • It should not break existing functionality • Remain a consistent API/interface The Power of Refactoring
  9. 9. Why refactoring? • Prototyping • Unclear specs • Project overview • Improve bad code The Power of Refactoring
  10. 10. Why refactoring? • Prototyping • Quickly have a clickable interface • No need for full functionality yet • As long as structure is OK, refactoring into final product is easy The Power of Refactoring
  11. 11. Why refactoring? • Unclear specs • Something needs to be implemented • but you don’t know how! • Create a basic API and call it • Implement it later The Power of Refactoring
  12. 12. Why refactoring? • Project overview • Similar to unclear specs • Create your project API skeleton with dummy responses • Bit by bit, work on actually adding functionality The Power of Refactoring
  13. 13. Why refactoring? • Improve bad code (PHP4 code, anyone?) • Security • Performance • Best Practices The Power of Refactoring
  14. 14. by/2.0 - Flying Cloud - The Power of Refactoring
  15. 15. by-nc-sa/2.0 - evymoon - The Power of Refactoring
  16. 16. Does it save time? • Yes! Yes it does! • Readability of your code • Maintainability of your code • Higher quality of your code • Unit testing to ensure stability The Power of Refactoring
  17. 17. Does it save time? • Prototyping • Don’t start from scratch, use your proto • Only rewrite those things that really need it The Power of Refactoring
  18. 18. Does it save time? • Unclear specs • Don’t write things fully until you need to • Think of a good API saves changing all calls The Power of Refactoring
  19. 19. Does it save time? • Project overview • Know the big picture • Keep focus on the small part you work on The Power of Refactoring
  20. 20. Does it save time? • Improve bad code • Better readability means faster coding • More stable code reduces bug fixing The Power of Refactoring
  21. 21. Some requirements • Unit/functional tests need to be in place • API needs to remain exactly the same • API needs to be good enough • if not, then rewrite instead of refactor The Power of Refactoring
  22. 22. Some examples The Power of Refactoring
  23. 23. Project A: Unclear specs • authentication system is still unknown • project or iteration deadline is nearing • let’s first simulate the authentication The Power of Refactoring
  24. 24. Project A: Unclear specs • Same could also apply for writing a prototype • Not all details are known • Short deadline • Doesn’t need to fully work yet The Power of Refactoring
  25. 25. Project A: Unclear specs • Halfway the project, client finally decides • Easy to implement: just refactor the authenticate method • Rest of the code does not care, as long as you keep the API the same The Power of Refactoring
  26. 26. Documentation helps • Good rule of thumb • If you need to change the method documentation, reconsider The Power of Refactoring
  27. 27. Project B: Code improvement • You took over management of another developer’s code • You wrote some crappy code yourself • Advancements in technology made better things possible (much more likely! ;-) The Power of Refactoring
  28. 28. Ensuring your API • Documentation • Changing your @param values? • Changing your @return value? • Adding/changing/removing @throws? The Power of Refactoring
  29. 29. Ensuring your API • Unit Tests • PHPUnit/SimpleTest/lime • Test for all situations (also exceptions) • Test for the unexpected! The Power of Refactoring
  30. 30. Ensuring your API • Continuous Integration • CruiseControl/phpUnderControl/Xinc • Continuously test your code • Alert through e-mail, SMS, Nabaztag The Power of Refactoring
  31. 31. Further reading • • 2008/09/14/code-refactoring-guidelines/ • 2008/10/13/refactoring-how-far-should-i-go/ The Power of Refactoring
  32. 32. Commercial break • - my personal blog • - my dutch symfony advocacy site • - my employer (we’re hiring! contact me!) The Power of Refactoring
  33. 33. Questions? The Power of Refactoring