For many developers, they will inevitably have to work with legacy code at some point in their careers. It requires a different set of skills to navigate these codebases as opposed to greenfield code where you start with a clean slate. Join me in exploring techniques to approach legacy code including how to improve documentation, increase testing coverage, refactoring code into smaller more testable units and techniques when you lack time to add tests.
6. What is Legacy Code?S
Code with poorly written tests that is
hard to change
7. What is Legacy Code?SaS
Code which you are not familiar with
and often not your idea
8. What is Legacy Code?
Legacy code is valuable code which you are not comfortable changing
9. We want to improve the code however…
● We don’t understand it
● We don’t want to break existing functionality
● We don’t want to make things worse
10. The Plan
● Beginning with understanding the code we want to improve
● Check that our understanding of the code matches what is expected
● Add tests
● Make changes
● Communicate our changes to the code effective to others on our team
15. Add tests, then make your changes
● Identify seams in your code
● A seam is a place where you can alter behavior in your program without
changing the code
16. Add tests, then make your changes
● Preprocessing seams
● Link seams
● Object seams
17. Trade-offs to consider with using Seams
Pros
● Break code into smaller dependencies that are easier to test
● Helps get just enough tests in place to support more aggressive changes
Cons
● The tests that depend on them can be hard to maintain
● You can create too many seams that the code becomes fragmented and an
overall refactor may be necessary
18. Add tests, then make your changes
● Sometimes legacy code is hard to understand in these cases you can use
characterization tests, also called golden master test
● Write a test that exercises the code you want to test and compare the output
to the golden master output
19.
20. Use Sprout & Wrap when you have limited time
● For Sprout, create the code elsewhere
● Unit test it
21. Use Sprout & Wrap when you have limited time
● Identify where you should call the new code from the existing code
● Call the new code from the legacy code
22. Use Sprout & Wrap when you have limited time
● For Wrap rename the old function you will be wrapping
● Create a new function with the same name and signature as the old function
23. Use Sprout & Wrap when you have limited time
● Call the new function from the old function
● Now you can unit test the new logic