The Power of Refactoring




                 Stefan Koopmanschap
    4developers, Krakow, March 7 2009
What will we do?

 What is refactoring?
 Why refactoring?
 Requirements
 HOWTO




                        2
Stefan Koopmanschap
Who is he?




                      3
About me

 Professional Services Consultant at Ibuildings
 10 years of PHP experience
 auto didact
 married, 2 kids, 6 cat...
Refactoring
What is it?




              5
Refactoring is a disciplined technique for restructuring
an existing body of code, altering its internal structure
without...
Code refactoring is the process of changing a computer
program's code to make it amenable to change,
improve its readabili...
What

Code refactoring is the process of changing a computer
program's internal structure without modifying its
external b...
Why

This is usually done to improve code readability, simplify
code structure, change code to adhere to a given
programmi...
Improve readability




                      10
Improve maintainability




                          11
Extend/change/implement




                          12
Is it the holy grail?




                        13
Rewrite

A rewrite in computer programming is the act or result of
re-implementing a large portion of existing functionali...
Refactor vs Rewrite




                      15
Refactor vs Rewrite

 Refactor whenever it is possible




                                    15
Refactor vs Rewrite

 Refactor whenever it is possible
 Big change? Try to cut it into smaller units




                 ...
Refactor vs Rewrite

 Refactor whenever it is possible
 Big change? Try to cut it into smaller units
 Still have to break ...
Refactor vs Rewrite

 Refactor whenever it is possible
 Big change? Try to cut it into smaller units
 Still have to break ...
Refactor vs Rewrite

Refactoring takes less effort
  Nice for you
  Better for the quality of your work
Breaking the API i...
Requirements
for successful refactoring




                             17
Requirements

 Codebase knowledge
 Structured API
 Unit testing




                      18
Codebase knowledge

 Know what to change quickly
 Be aware of your changes
 Be aware of impact of your changes on other co...
Structured API

 Refactoring DOES NOT REPLACE design!
 You need a good API that allows internal changes
 Additions to exis...
Unit testing




               21
Unit testing

 Unit testing is your first point of QA
 They safeguard you from unexpected side effects
 Test - Change - Te...
An example
user authentication




                      23
A test-driven approach




                         24
My first implementation




                          25
My first implementation




                          25
A more dynamic version




                         26
A more dynamic version




                         26
Tips and tricks




                  27
Some tips and tricks

 Write unit tests before you start refactoring
 Code documentation (phpDoc) is your friend
 Don’t tr...
Will you refactor now?




                         29
Looking back

 What is refactoring?
 Why do we want to refactor?
 Is it the holy grail?
 Refactoring vs Rewriting
 Require...
Creative Commons Rocks!
Readability:
http://www.flickr.com/photos/vipulmathur/
471634239/ (by-nc-sa/2.0)
Maintainability:
...
Questions ?




              32
Thank you!
                Contact details:
           Stefan Koopmanschap
  http://www.leftontheweb.com/
      http://www...
Upcoming SlideShare
Loading in...5
×

The Power Of Refactoring (4developers Krakow)

1,845

Published on

The Power of Refactoring presentation I gave at 4developers conference in Krakow, Poland om March 7th.

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

  • Be the first to like this

No Downloads
Views
Total Views
1,845
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
96
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

The Power Of Refactoring (4developers Krakow)

  1. 1. The Power of Refactoring Stefan Koopmanschap 4developers, Krakow, March 7 2009
  2. 2. What will we do? What is refactoring? Why refactoring? Requirements HOWTO 2
  3. 3. Stefan Koopmanschap Who is he? 3
  4. 4. About me Professional Services Consultant at Ibuildings 10 years of PHP experience auto didact married, 2 kids, 6 cats, a bunch of fish Apple lover music addict symfony advocate 4
  5. 5. Refactoring What is it? 5
  6. 6. 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) 6
  7. 7. 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) 7
  8. 8. What Code refactoring is the process of changing a computer program's internal structure without modifying its external behavior or existing functionality. 8
  9. 9. Why This is usually done to improve code readability, simplify code structure, change code to adhere to a given programming paradigm, improve maintainability, or improve extensibility. 9
  10. 10. Improve readability 10
  11. 11. Improve maintainability 11
  12. 12. Extend/change/implement 12
  13. 13. Is it the holy grail? 13
  14. 14. Rewrite A rewrite in computer programming is the act or result of re-implementing a large portion of existing functionality without re-use of its source code. When the rewrite is not using existing code at all, it is common to speak of a rewrite from scratch. When instead only parts are re- engineered, which have otherwise become complicated to handle or extend, then it is more precise to speak of code refactoring. (Wikipedia) 14
  15. 15. Refactor vs Rewrite 15
  16. 16. Refactor vs Rewrite Refactor whenever it is possible 15
  17. 17. Refactor vs Rewrite Refactor whenever it is possible Big change? Try to cut it into smaller units 15
  18. 18. Refactor vs Rewrite Refactor whenever it is possible Big change? Try to cut it into smaller units Still have to break the API? Go ahead, and rewrite! 15
  19. 19. Refactor vs Rewrite Refactor whenever it is possible Big change? Try to cut it into smaller units Still have to break the API? Go ahead, and rewrite! Don’t take the decision in 5 minutes 15
  20. 20. Refactor vs Rewrite Refactoring takes less effort Nice for you Better for the quality of your work Breaking the API is more work Ensure all calling code is altered as well Update your unit tests Update your documentation 16
  21. 21. Requirements for successful refactoring 17
  22. 22. Requirements Codebase knowledge Structured API Unit testing 18
  23. 23. Codebase knowledge Know what to change quickly Be aware of your changes Be aware of impact of your changes on other components 19
  24. 24. Structured API Refactoring DOES NOT REPLACE design! You need a good API that allows internal changes Additions to existing API’s should not change existing API Keep small “units” 20
  25. 25. Unit testing 21
  26. 26. Unit testing Unit testing is your first point of QA They safeguard you from unexpected side effects Test - Change - Test 22
  27. 27. An example user authentication 23
  28. 28. A test-driven approach 24
  29. 29. My first implementation 25
  30. 30. My first implementation 25
  31. 31. A more dynamic version 26
  32. 32. A more dynamic version 26
  33. 33. Tips and tricks 27
  34. 34. Some tips and tricks Write unit tests before you start refactoring Code documentation (phpDoc) is your friend Don’t trust your IDE’s refactoring options 28
  35. 35. Will you refactor now? 29
  36. 36. Looking back What is refactoring? Why do we want to refactor? Is it the holy grail? Refactoring vs Rewriting Requirements How to do it Tips and tricks 30
  37. 37. Creative Commons Rocks! Readability: http://www.flickr.com/photos/vipulmathur/ 471634239/ (by-nc-sa/2.0) Maintainability: http://www.flickr.com/photos/comicbase/ 2117680144/ (by-nc-sa/2.0) Extend: http://www.flickr.com/photos/auntiep/ 221951988/ (by-nc-sa/2.0) Holy Grail: http://www.flickr.com/photos/joebeone/ 153921142/ (by/2.0) Würst test ever: http://www.flickr.com/photos/pingles/ 1341229619/ (by-nc-nd/2.0) 31
  38. 38. Questions ? 32
  39. 39. Thank you! Contact details: Stefan Koopmanschap http://www.leftontheweb.com/ http://www.ibuildings.com/ http://www.twitter.com/skoop http://www.slideshare.net/skoop
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×