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 includin...
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.
Size: 1.57 MB
Language: en
Added: Apr 29, 2024
Slides: 25 pages
Slide Content
Sanely working with Legacy
Code
Joe Cabrera
Joe Cabrera
●Lead Software Engineer at ASAPP
●Python Programmer since 2009
●Working on various legacy codebases for the
last 5 years
Goal: Give you the tools to succeed when dealing
with legacy code
What is Legacy Code?S
Code without documentation
What is Legacy Code?S
Code without tests
What is Legacy Code?S
Code with poorly written tests that is
hard to change
What is Legacy Code?SaS
Code which you are not familiar with
and often not your idea
What is Legacy Code?
Legacy code is valuable code which you are not comfortable changing
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
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
Increase code documentation
Add class docstrings
Increase code documentation
Add function docstrings
Increase code documentation
Add documentation to explain logic when
it’s not obvious
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
Add tests, then make your changes
●Preprocessing seams
●Link seams
●Object seams
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
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
Use Sprout & Wrap when you have limited time
●For Sprout, create the code elsewhere
●Unit test it
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
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
Use Sprout & Wrap when you have limited time
●Call the new function from the old function
●Now you can unit test the new logic
Share your changes to others on your team
Everyone Profits