Working with Legacy Code

404 views

Published on

Roberto Hernandez-Pou presented "Working with Legacy Code" at the 2012 DC Agile Engineering Conference

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
404
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
0
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Working with Legacy Code

  1. 1. Working Effectively with Legacy Code Roberto Hernandez-Pou Managing Consultant Excella Consulting @hernandezrobert http://www.overridethis.comExcella Consulting – http://www.excella.com
  2. 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. 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 CodeExcella Consulting – http://www.excella.com #DCAEC12 @HernandezRobert* Based on the Book “Working Effectively with Legacy Code” by Michael Feathers
  4. 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. 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 ChangesExcella Consulting – http://www.excella.com #DCAEC12 @HernandezRobert* Based on the Book “Working Effectively with Legacy Code” by Michael Feathers
  6. 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. 7. How do we approach the work?• Working with Feedback – Edit and pray (Industry standard) – Cover and modifyExcella Consulting – http://www.excella.com #DCAEC12 @HernandezRobert* Based on the Book “Working Effectively with Legacy Code” by Michael Feathers
  8. 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. 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 codeExcella Consulting – http://www.excella.com #DCAEC12 @HernandezRobert* Based on the Book “Working Effectively with Legacy Code” by Michael Feathers
  10. 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 changesExcella Consulting – http://www.excella.com #DCAEC12 @HernandezRobert* Based on the Book “Working Effectively with Legacy Code” by Michael Feathers
  11. 11. The Legacy Code Change Algorithm Identify change points Find test points Break dependencies Write tests Make changes and refactorExcella Consulting – http://www.excella.com #DCAEC12 @HernandezRobert* Based on the Book “Working Effectively with Legacy Code” by Michael Feathers
  12. 12. Sensing & Separation• Sensing: we break dependencies to sense when we cant access values our code computes• Separation: we break dependencies to separate when we cant even get a piece of code into a test harness to runExcella Consulting – http://www.excella.com #DCAEC12 @HernandezRobert* Based on the Book “Working Effectively with Legacy Code” by Michael Feathers
  13. 13. Fake Collaborators• Object that impersonates some collaborator of your class when its being tested. – Demo: Echo ConsoleExcella Consulting – http://www.excella.com #DCAEC12 @HernandezRobert* Based on the Book “Working Effectively with Legacy Code” by Michael Feathers
  14. 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 ReaderExcella Consulting – http://www.excella.com #DCAEC12 @HernandezRobert* Based on the Book “Working Effectively with Legacy Code” by Michael Feathers
  15. 15. ToolsExcella Consulting – http://www.excella.com #DCAEC12 @HernandezRobert* Based on the Book “Working Effectively with Legacy Code” by Michael Feathers
  16. 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 differenceExcella Consulting – http://www.excella.com #DCAEC12 @HernandezRobert* Based on the Book “Working Effectively with Legacy Code” by Michael Feathers
  17. 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 differenceExcella Consulting – http://www.excella.com #DCAEC12 @HernandezRobert* Based on the Book “Working Effectively with Legacy Code” by Michael Feathers
  18. 18. Conclusions• No “silver bullet” here, its hard work but not impossible• Overwhelming at first but things will get better as the number of tests increase• Be pragmatic!
  19. 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. sampleExcella Consulting – http://www.excella.com
  20. 20. Thank You! 1. Blog: http://www.overridethis.com 2. Email: roberto.hernandez@excella.com 3. @hernandezrobert 4.Excella Consulting – http://www.excella.com

×