Sanely working with Legacy Code - PyTexas 2024

JoeCabrera3 13 views 25 slides Apr 29, 2024
Slide 1
Slide 1 of 25
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25

About This Presentation

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...


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

Increase code documentation
Explain mysterious variables

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

Questions?
github.com/greedo
eminorlabs.com