Complementary slide deck for my hands-on coding session on testing and refactoring legacy code.
Code can be found here: https://github.com/sandromancuso/trip-service-kata
3. Business Requirements
Imagine a social networking website for
travellers
• You need to be logged in to see the content
• You need to be a friend to see someone else’s
trips
4. Legacy Code Rules
• You cannot change production code if not
covered by tests
– Just automated refactorings (via IDEs) are allowed,
if needed to write the test
5. Tips
• Use a code coverage tool
• Commit as often as possible
– Enables you to commit (push) improvements
quickly
– Allows you to roll back (reset) if you get lost
• When refactoring, try to stay in the green for
as long as possible
9. Legacy Code Common Problems
• Hard-wired dependencies
– Create a “seam” to break dependencies
• Extract dependency in a protected method
• In our test, extend the class under test and override the
method with the dependency so you can mock the
result
• Identify feature envy
– Move behaviour out to the appropriate class
11. Remember
Start refactoring from
deepest to shortest
branch
Start testing from
shortest to
deepest branch
12. Refactoring vs Re-design
• After localised refactoring, consider fixing the
design
• Writing tests for existing code may perpetuate
a bad design
13. Craftsmen at work
• Write readable and maintainable code
– Code must express business rules
• Strive for simplicity
• Know your tools well (i.e. frameworks, shortcuts)
• Work in small and safe increments
– Commit often
• Embrace changes, be brave
• Boy scout rule / No broken windows