Advertisement

The Power of Refactoring

Developer, Consultant, Trainer at Ingewikkeld
Oct. 30, 2008
Advertisement

More Related Content

Advertisement
Advertisement

The Power of Refactoring

  1. The Power of Refactoring Why doing things three times may speed up development The Power of Refactoring
  2. What will we do? • Intro • What is refactoring? • Why refactoring? • It saves time? • Some examples The Power of Refactoring
  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. Refactoring The Power of Refactoring
  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 - Refactoring.com) The Power of Refactoring
  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. So basically... • Improve small parts of the code for... • Readability • Performance improvement • Implementation or change of logic The Power of Refactoring
  8. But watch out! • It should not break existing functionality • Remain a consistent API/interface The Power of Refactoring
  9. Refactoring v Rewriting • Refactoring • Consistent API • Internal logic changes • No need to alter unit tests or API docs The Power of Refactoring
  10. Refactoring v Rewriting • Rewriting • Refactoring won’t help you anymore • More complex: alter unit tests, alter calling code, alter documentation The Power of Refactoring
  11. Why refactoring? • Prototyping • Unclear specs • Project overview • Improve bad code The Power of Refactoring
  12. 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
  13. 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
  14. 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
  15. Why refactoring? • Improve bad code (PHP4 code, anyone?) • Security • Performance • Best Practices The Power of Refactoring
  16. by-nc/2.0 - gilest - http://www.flickr.com/photos/gilest/ The Power of Refactoring
  17. by-nc-sa/2.0 - evymoon - http://www.flickr.com/photos/evymoon_quatrain_quotidien/ The Power of Refactoring
  18. 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
  19. 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
  20. 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
  21. Does it save time? • Project overview • Know the big picture • Keep focus on the small part you work on The Power of Refactoring
  22. Does it save time? • Improve bad code • Better readability means faster coding • More stable code reduces bug fixing The Power of Refactoring
  23. 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
  24. Some examples The Power of Refactoring
  25. 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
  26. 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
  27. Project A: Unclear specs The Power of Refactoring
  28. Project A: Unclear specs The Power of Refactoring
  29. 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
  30. Project A: Unclear specs The Power of Refactoring
  31. Nothing changes • This still works! The Power of Refactoring
  32. Documentation helps • Good rule of thumb • If you need to change the method documentation, reconsider The Power of Refactoring
  33. 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
  34. Project B: Code improvement The Power of Refactoring
  35. Project B: Code improvement The Power of Refactoring
  36. Nothing changes • This still works! The Power of Refactoring
  37. Ensuring your API • Documentation • Changing your @param values? • Changing your @return value? • Adding/changing/removing @throws? The Power of Refactoring
  38. Ensuring your API • Unit Tests • PHPUnit/SimpleTest/lime • Test for all situations (also exceptions) • Test for the unexpected! The Power of Refactoring
  39. Ensuring your API • Continuous Integration • CruiseControl/phpUnderControl/Xinc • Continuously test your code • Alert through e-mail, SMS, Nabaztag The Power of Refactoring
  40. Further reading • http://www.refactoring.com/ • http://phpimpact.wordpress.com/ 2008/09/14/code-refactoring-guidelines/ • http://chrissterling.gettingagile.com/ 2008/10/13/refactoring-how-far-should-i-go/ The Power of Refactoring
  41. Commercial break • http://www.leftontheweb.com/ - my personal blog • http://www.symfony-framework.nl/ - my dutch symfony advocacy site • http://www.ibuildings.com/ - my employer (we’re hiring! contact me!) The Power of Refactoring
  42. Questions? The Power of Refactoring
Advertisement