Working Effectively with Legacy Code was presented at the 2012 DC Agile Engineering Conference on 12/7/2012 by Excella Managing Consultant Roberto Hernandez-Pou (@hernandezrobert).
DevoxxFR 2024 Reproducible Builds with Apache Maven
Working Effectively With Legacy Code
1. Working Effectively with
Legacy Code
Roberto Hernandez-Pou
Managing Consultant
Excella Consulting
@hernandezrobert
http://www.overridethis.com
Excella Consulting – http://www.excella.com
2. Legacy Code: Characteristics
• Poor Architecture
• Non-Uniform coding styles
• Poor or Non-Existing Documentation
• Mythical “Oral” Documentation
• No Tests (or Minimal Test Coverage)
• Extremely Valuable!
– Only Successful code becomes Legacy Code.
Excella Consulting – http://www.excella.com #DCAEC12 @HernandezRobert
* Based on the Book “Working Effectively with Legacy Code” by Michael Feathers
3. Agenda
• How does code become Legacy Code?
• Reasons to work with Legacy Code
• What are my options?
• How do we approach the work?
– Edit and Pray „or‟ Cover and Modify
• The Legacy Code Change Algorithm
• Sensing and Separation
• Fake Collaborators
• Seams
• Tools
• TDD for Legacy Code
Excella Consulting – http://www.excella.com #DCAEC12 @HernandezRobert
* Based on the Book “Working Effectively with Legacy Code” by Michael Feathers
4. How does code become Legacy Code?
• New Features or New Requirements
– Developers usually turn to shortcuts and hacks to make deadlines
and deliver on time to market.
• Software Team Turnover or Large Development Teams
– Multiple styles of coding.
– Lack of vision.
– Code duplication.
• Poor Team Communication.
Excella Consulting – http://www.excella.com #DCAEC12 @HernandezRobert
* Based on the Book “Working Effectively with Legacy Code” by Michael Feathers
5. Reasons to “Work with Legacy Code”
• Four reasons to change software:
– Adding a feature
– Fixing a bug
– Refactoring
– Optimizing resource usage
Adding a Feature Fixing a Bug Refactoring Optimizing
resource usage
Structure Changes Changes Changes
New Functionality Changes
Functionality Changes
Resource Usage Changes
Excella Consulting – http://www.excella.com #DCAEC12 @HernandezRobert
* Based on the Book “Working Effectively with Legacy Code” by Michael Feathers
6. As a Developer what are my options?
• Look for a new Job?
• Start from scratch.
– Big bang approach!
• Have somebody else do the job:
– Members of the original development team
– Consultants ($$$)
• Deal with it!
Excella Consulting – http://www.excella.com #DCAEC12 @HernandezRobert
* Based on the Book “Working Effectively with Legacy Code” by Michael Feathers
7. How do we approach the work?
• Working with Feedback
– Edit and pray
(Industry standard)
– Cover and modify
Excella Consulting – http://www.excella.com #DCAEC12 @HernandezRobert
* Based on the Book “Working Effectively with Legacy Code” by Michael Feathers
8. Edit and Pray
• Carefully plan the changes
• Make sure you understand the code
• Start making changes
• Poke around to see if you broke anything
• Deploy to production environment
• Wait for feedback from customers
9. Cover and Modify
• Build a safety net (Covering = Testing)
• Unit Testing
• Higher level testing
The Legacy Code Dilemma
When we change code, we should have tests in place.
To put tests in place we need to change code
Excella Consulting – http://www.excella.com #DCAEC12 @HernandezRobert
* Based on the Book “Working Effectively with Legacy Code” by Michael Feathers
10. Cover and Modify
• Introduce Unit Tests
• Unit Tests are:
– Fast
– Localise Problems
– Do not talk to a database
– Do not communicate over the network
– Do not communicate with local resources
– Do not need config changes
Excella Consulting – http://www.excella.com #DCAEC12 @HernandezRobert
* Based on the Book “Working Effectively with Legacy Code” by Michael Feathers
11. The Legacy Code Change Algorithm
Identify change points
Find test points
Break dependencies
Write tests
Make changes and refactor
Excella Consulting – http://www.excella.com #DCAEC12 @HernandezRobert
* Based on the Book “Working Effectively with Legacy Code” by Michael Feathers
12. Sensing & Separation
• Sensing: we break dependencies to sense when we can't access
values our code computes
• Separation: we break dependencies to separate when we can't
even get a piece of code into a test harness to run
Excella Consulting – http://www.excella.com #DCAEC12 @HernandezRobert
* Based on the Book “Working Effectively with Legacy Code” by Michael Feathers
13. Fake Collaborators
• Object that impersonates some collaborator of your class when its
being tested.
– Demo: Echo Console
Excella Consulting – http://www.excella.com #DCAEC12 @HernandezRobert
* Based on the Book “Working Effectively with Legacy Code” by Michael Feathers
14. The Seam Model
• Seam: a place where you can alter behavior of your program
without editing in that place.
• Enabling Point: Every seam has an enabling point, a place where
you can make the decision to use one behavior or another.
• Looking for existing seams in legacy code allow us to break
dependencies (for sensing or separation) without refactoring.
• Demo: Echo Console
• Demo: Fake Data Reader
Excella Consulting – http://www.excella.com #DCAEC12 @HernandezRobert
* Based on the Book “Working Effectively with Legacy Code” by Michael Feathers
15. Tools
Excella Consulting – http://www.excella.com #DCAEC12 @HernandezRobert
* Based on the Book “Working Effectively with Legacy Code” by Michael Feathers
16. Test Driven Development
• Test-Driven Development (TDD)
1. Write a failing test
2. Get it to compile
3. Make it pass
4. Remove duplication
5. Repeat
• Programming by difference
Excella Consulting – http://www.excella.com #DCAEC12 @HernandezRobert
* Based on the Book “Working Effectively with Legacy Code” by Michael Feathers
17. Test Driven Development
• Test-Driven Development (TDD) for legacy
0. Get the class you want to change under test
1. Write a failing test
2. Get it to compile
3. Make it pass (try not to change existing code)
4. Remove duplication
5. Repeat
• Programming by difference
Excella Consulting – http://www.excella.com #DCAEC12 @HernandezRobert
* Based on the Book “Working Effectively with Legacy Code” by Michael Feathers
18. Conclusions
• No “silver bullet” here, it's hard work but not impossible
• Overwhelming at first but things will get better as the number of
tests increase
• Be pragmatic!
19. Resources
“Working Effectively with Legacy Code” by
1.
Michael Feathers
2. http://www.objectmentor.com
3. http://www.overridethis.com
http://bitbucket.org/rhp_74/overridethis.legacycode
4.
sample
Excella Consulting – http://www.excella.com