The Power of
                Refactoring
         Why doing things three times may speed up
                       develop...
What will we do?

  • Intro
  • What is refactoring?
  • Why refactoring?
  • It saves time?
  • Some examples
The Power o...
Introduction
  • Professional Services Consultant at
       Ibuildings
  • Secretary of Dutch PHP Usergroup
  • symfony ad...
Refactoring


The Power of
 Refactoring
Refactoring is a disciplined technique for
restructuring an existing body of code, altering its
  internal structure witho...
Code refactoring is the process of
 changing a computer program's code to
 make it amenable to change, improve its
 readab...
So basically...

  • Improve small parts of the code for...
   • Readability
   • Performance improvement
   • Implementat...
But watch out!


  • It should not break existing functionality
  • Remain a consistent API/interface

The Power of
 Refac...
Refactoring v Rewriting

  • Refactoring
   • Consistent API
   • Internal logic changes
   • No need to alter unit tests ...
Refactoring v Rewriting

  • Rewriting
   • Refactoring won’t help you anymore
   • More complex: alter unit tests, alter
...
Why refactoring?

  • Prototyping
  • Unclear specs
  • Project overview
  • Improve bad code
The Power of
 Refactoring
Why refactoring?

  • Prototyping
   • Quickly have a clickable interface
   • No need for full functionality yet
   • As ...
Why refactoring?

  • Unclear specs
   • Something needs to be implemented
     • but you don’t know how!
   • Create a ba...
Why refactoring?
  • Project overview
   • Similar to unclear specs
   • Create your project API skeleton with
           ...
Why refactoring?

  • Improve bad code (PHP4 code, anyone?)
   • Security
   • Performance
   • Best Practices
The Power o...
by-nc/2.0 - gilest - http://www.flickr.com/photos/gilest/

The Power of
 Refactoring
by-nc-sa/2.0 - evymoon - http://www.flickr.com/photos/evymoon_quatrain_quotidien/


The Power of
 Refactoring
Does it save time?

  • Yes! Yes it does!
   • Readability of your code
   • Maintainability of your code
   • Higher qual...
Does it save time?

  • Prototyping
   • Don’t start from scratch, use your proto
   • Only rewrite those things that real...
Does it save time?

  • Unclear specs
   • Don’t write things fully until you need to
   • Think of a good API saves chang...
Does it save time?

  • Project overview
   • Know the big picture
   • Keep focus on the small part you work
           o...
Does it save time?

  • Improve bad code
   • Better readability means faster coding
   • More stable code reduces bug fixi...
Some requirements

  • Unit/functional tests need to be in place
   • API needs to remain exactly the same
  • API needs t...
Some examples


The Power of
 Refactoring
Project A: Unclear
                specs

  • authentication system is still unknown
  • project or iteration deadline is ...
Project A: Unclear
                specs
  • Same could also apply for writing a
       prototype
      • Not all details ...
Project A: Unclear
                specs




The Power of
 Refactoring
Project A: Unclear
                specs




The Power of
 Refactoring
Project A: Unclear
                specs
  • Halfway the project, client finally decides
  • Easy to implement: just refact...
Project A: Unclear
                specs




The Power of
 Refactoring
Nothing changes


  •   This still works!




The Power of
 Refactoring
Documentation helps

  •   Good rule of thumb

      •   If you need to change
          the method
          documentatio...
Project B: Code
                improvement
  • You took over management of another
       developer’s code
  • You wrote ...
Project B: Code
                improvement




The Power of
 Refactoring
Project B: Code
                improvement




The Power of
 Refactoring
Nothing changes


  •   This still works!




The Power of
 Refactoring
Ensuring your API

  • Documentation
   • Changing your @param values?
   • Changing your @return value?
   • Adding/chang...
Ensuring your API

  • Unit Tests
   • PHPUnit/SimpleTest/lime
   • Test for all situations (also exceptions)
   • Test fo...
Ensuring your API

  • Continuous Integration
   • CruiseControl/phpUnderControl/Xinc
   • Continuously test your code
   ...
Further reading

  • http://www.refactoring.com/
  • http://phpimpact.wordpress.com/
       2008/09/14/code-refactoring-gu...
Commercial break

  • http://www.leftontheweb.com/ - my
       personal blog
  • http://www.symfony-framework.nl/ - my
   ...
Questions?


The Power of
 Refactoring
Upcoming SlideShare
Loading in...5
×

The Power of Refactoring

2,465

Published on

as used at the International PHP Conference 2008 in Mainz on October 30th 2008

Published in: Technology
0 Comments
6 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
2,465
On Slideshare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
0
Comments
0
Likes
6
Embeds 0
No embeds

No notes for slide

The Power of Refactoring

  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? • Intro • 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 - Refactoring.com) 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. Refactoring v Rewriting • Refactoring • Consistent API • Internal logic changes • No need to alter unit tests or API docs The Power of Refactoring
  10. 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. 11. Why refactoring? • Prototyping • Unclear specs • Project overview • Improve bad code The Power of Refactoring
  12. 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. 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. 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. 15. Why refactoring? • Improve bad code (PHP4 code, anyone?) • Security • Performance • Best Practices The Power of Refactoring
  16. 16. by-nc/2.0 - gilest - http://www.flickr.com/photos/gilest/ The Power of Refactoring
  17. 17. by-nc-sa/2.0 - evymoon - http://www.flickr.com/photos/evymoon_quatrain_quotidien/ The Power of Refactoring
  18. 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. 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. 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. 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. 22. Does it save time? • Improve bad code • Better readability means faster coding • More stable code reduces bug fixing The Power of Refactoring
  23. 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. 24. Some examples The Power of Refactoring
  25. 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. 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. 27. Project A: Unclear specs The Power of Refactoring
  28. 28. Project A: Unclear specs The Power of Refactoring
  29. 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. 30. Project A: Unclear specs The Power of Refactoring
  31. 31. Nothing changes • This still works! The Power of Refactoring
  32. 32. Documentation helps • Good rule of thumb • If you need to change the method documentation, reconsider The Power of Refactoring
  33. 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. 34. Project B: Code improvement The Power of Refactoring
  35. 35. Project B: Code improvement The Power of Refactoring
  36. 36. Nothing changes • This still works! The Power of Refactoring
  37. 37. Ensuring your API • Documentation • Changing your @param values? • Changing your @return value? • Adding/changing/removing @throws? The Power of Refactoring
  38. 38. Ensuring your API • Unit Tests • PHPUnit/SimpleTest/lime • Test for all situations (also exceptions) • Test for the unexpected! The Power of Refactoring
  39. 39. Ensuring your API • Continuous Integration • CruiseControl/phpUnderControl/Xinc • Continuously test your code • Alert through e-mail, SMS, Nabaztag The Power of Refactoring
  40. 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. 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. 42. Questions? The Power of Refactoring

×