The Power of
Refactoring
Why doing things three times may speed up
development
The Power of
Refactoring
What will we do?
• Intro
• What is refactoring?
• Why refactoring?
• It saves time?
• Some examples
The Power of
Refactoring
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
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
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
So basically...
• Improve small parts of the code for...
• Readability
• Performance improvement
• Implementation or change of logic
The Power of
Refactoring
But watch out!
• It should not break existing functionality
• Remain a consistent API/interface
The Power of
Refactoring
Refactoring v Rewriting
• Refactoring
• Consistent API
• Internal logic changes
• No need to alter unit tests or API docs
The Power of
Refactoring
Refactoring v Rewriting
• Rewriting
• Refactoring won’t help you anymore
• More complex: alter unit tests, alter
calling code, alter documentation
The Power of
Refactoring
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 long as structure is OK, refactoring
into final product is easy
The Power of
Refactoring
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
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
Why refactoring?
• Improve bad code (PHP4 code, anyone?)
• Security
• Performance
• Best Practices
The Power of
Refactoring
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 quality of your code
• Unit testing to ensure stability
The Power of
Refactoring
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
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
Does it save time?
• Project overview
• Know the big picture
• Keep focus on the small part you work
on
The Power of
Refactoring
Does it save time?
• Improve bad code
• Better readability means faster coding
• More stable code reduces bug fixing
The Power of
Refactoring
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
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
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
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
Documentation helps
• Good rule of thumb
• If you need to change
the method
documentation,
reconsider
The Power of
Refactoring
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
Ensuring your API
• Documentation
• Changing your @param values?
• Changing your @return value?
• Adding/changing/removing @throws?
The Power of
Refactoring
Ensuring your API
• Unit Tests
• PHPUnit/SimpleTest/lime
• Test for all situations (also exceptions)
• Test for the unexpected!
The Power of
Refactoring
Ensuring your API
• Continuous Integration
• CruiseControl/phpUnderControl/Xinc
• Continuously test your code
• Alert through e-mail, SMS, Nabaztag
The Power of
Refactoring
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
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