SlideShare a Scribd company logo
1 of 25
Download to read offline
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

More Related Content

Similar to Sanely working with Legacy Code - PyTexas 2024

Coding Dojo: String Calculator (2013)
Coding Dojo: String Calculator (2013)Coding Dojo: String Calculator (2013)
Coding Dojo: String Calculator (2013)
Peter Kofler
 
Test-Driven Development Reference Card
Test-Driven Development Reference CardTest-Driven Development Reference Card
Test-Driven Development Reference Card
Seapine Software
 
TDD — Are you sure you properly test code?
TDD — Are you sure you properly test code?TDD — Are you sure you properly test code?
TDD — Are you sure you properly test code?
Dmitriy Nesteryuk
 

Similar to Sanely working with Legacy Code - PyTexas 2024 (20)

Coding Dojo: String Calculator (2013)
Coding Dojo: String Calculator (2013)Coding Dojo: String Calculator (2013)
Coding Dojo: String Calculator (2013)
 
Refactoring legacy code
Refactoring legacy codeRefactoring legacy code
Refactoring legacy code
 
Software development best practices & coding guidelines
Software development best practices & coding guidelinesSoftware development best practices & coding guidelines
Software development best practices & coding guidelines
 
Improving your code design using Java
Improving your code design using JavaImproving your code design using Java
Improving your code design using Java
 
Putting the D&D in TDD
Putting the D&D in TDDPutting the D&D in TDD
Putting the D&D in TDD
 
Make a better with clean code
Make a better with clean codeMake a better with clean code
Make a better with clean code
 
Clean code
Clean codeClean code
Clean code
 
Pair Programming (2015)
Pair Programming (2015)Pair Programming (2015)
Pair Programming (2015)
 
May 2021 Spark Testing ... or how to farm reputation on StackOverflow
May 2021 Spark Testing ... or how to farm reputation on StackOverflowMay 2021 Spark Testing ... or how to farm reputation on StackOverflow
May 2021 Spark Testing ... or how to farm reputation on StackOverflow
 
Quick Intro to Clean Coding
Quick Intro to Clean CodingQuick Intro to Clean Coding
Quick Intro to Clean Coding
 
Unit testing legacy code
Unit testing legacy codeUnit testing legacy code
Unit testing legacy code
 
Writing clean scientific software Murphy cleancoding
Writing clean scientific software Murphy cleancodingWriting clean scientific software Murphy cleancoding
Writing clean scientific software Murphy cleancoding
 
Extract Method Refactoring Workshop (2016)
Extract Method Refactoring Workshop (2016)Extract Method Refactoring Workshop (2016)
Extract Method Refactoring Workshop (2016)
 
A Brief Introduction to Test-Driven Development
A Brief Introduction to Test-Driven DevelopmentA Brief Introduction to Test-Driven Development
A Brief Introduction to Test-Driven Development
 
Mobile Code Optimisation Services
Mobile Code Optimisation ServicesMobile Code Optimisation Services
Mobile Code Optimisation Services
 
Deliberate Practice (Agile Slovenia 2015)
Deliberate Practice (Agile Slovenia 2015)Deliberate Practice (Agile Slovenia 2015)
Deliberate Practice (Agile Slovenia 2015)
 
Importance of the quality of code
Importance of the quality of codeImportance of the quality of code
Importance of the quality of code
 
Test-Driven Development Reference Card
Test-Driven Development Reference CardTest-Driven Development Reference Card
Test-Driven Development Reference Card
 
How to quickly add a safety net to a legacy codebase
How to quickly add a safety net to a legacy codebaseHow to quickly add a safety net to a legacy codebase
How to quickly add a safety net to a legacy codebase
 
TDD — Are you sure you properly test code?
TDD — Are you sure you properly test code?TDD — Are you sure you properly test code?
TDD — Are you sure you properly test code?
 

Recently uploaded

Scouring of cotton and wool fabric with effective scouring method
Scouring of cotton and wool fabric with effective scouring methodScouring of cotton and wool fabric with effective scouring method
Scouring of cotton and wool fabric with effective scouring method
vimal412355
 
21P35A0312 Internship eccccccReport.docx
21P35A0312 Internship eccccccReport.docx21P35A0312 Internship eccccccReport.docx
21P35A0312 Internship eccccccReport.docx
rahulmanepalli02
 
Artificial intelligence presentation2-171219131633.pdf
Artificial intelligence presentation2-171219131633.pdfArtificial intelligence presentation2-171219131633.pdf
Artificial intelligence presentation2-171219131633.pdf
Kira Dess
 

Recently uploaded (20)

Raashid final report on Embedded Systems
Raashid final report on Embedded SystemsRaashid final report on Embedded Systems
Raashid final report on Embedded Systems
 
Introduction-to- Metrology and Quality.pptx
Introduction-to- Metrology and Quality.pptxIntroduction-to- Metrology and Quality.pptx
Introduction-to- Metrology and Quality.pptx
 
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptxHOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
 
Scouring of cotton and wool fabric with effective scouring method
Scouring of cotton and wool fabric with effective scouring methodScouring of cotton and wool fabric with effective scouring method
Scouring of cotton and wool fabric with effective scouring method
 
Working Principle of Echo Sounder and Doppler Effect.pdf
Working Principle of Echo Sounder and Doppler Effect.pdfWorking Principle of Echo Sounder and Doppler Effect.pdf
Working Principle of Echo Sounder and Doppler Effect.pdf
 
Worksharing and 3D Modeling with Revit.pptx
Worksharing and 3D Modeling with Revit.pptxWorksharing and 3D Modeling with Revit.pptx
Worksharing and 3D Modeling with Revit.pptx
 
Study of Computer Hardware System using Block Diagram
Study of Computer Hardware System using Block DiagramStudy of Computer Hardware System using Block Diagram
Study of Computer Hardware System using Block Diagram
 
Dr Mrs A A Miraje C Programming PPT.pptx
Dr Mrs A A Miraje C Programming PPT.pptxDr Mrs A A Miraje C Programming PPT.pptx
Dr Mrs A A Miraje C Programming PPT.pptx
 
Theory of Time 2024 (Universal Theory for Everything)
Theory of Time 2024 (Universal Theory for Everything)Theory of Time 2024 (Universal Theory for Everything)
Theory of Time 2024 (Universal Theory for Everything)
 
Instruct Nirmaana 24-Smart and Lean Construction Through Technology.pdf
Instruct Nirmaana 24-Smart and Lean Construction Through Technology.pdfInstruct Nirmaana 24-Smart and Lean Construction Through Technology.pdf
Instruct Nirmaana 24-Smart and Lean Construction Through Technology.pdf
 
What is Coordinate Measuring Machine? CMM Types, Features, Functions
What is Coordinate Measuring Machine? CMM Types, Features, FunctionsWhat is Coordinate Measuring Machine? CMM Types, Features, Functions
What is Coordinate Measuring Machine? CMM Types, Features, Functions
 
21P35A0312 Internship eccccccReport.docx
21P35A0312 Internship eccccccReport.docx21P35A0312 Internship eccccccReport.docx
21P35A0312 Internship eccccccReport.docx
 
Artificial intelligence presentation2-171219131633.pdf
Artificial intelligence presentation2-171219131633.pdfArtificial intelligence presentation2-171219131633.pdf
Artificial intelligence presentation2-171219131633.pdf
 
NEWLETTER FRANCE HELICES/ SDS SURFACE DRIVES - MAY 2024
NEWLETTER FRANCE HELICES/ SDS SURFACE DRIVES - MAY 2024NEWLETTER FRANCE HELICES/ SDS SURFACE DRIVES - MAY 2024
NEWLETTER FRANCE HELICES/ SDS SURFACE DRIVES - MAY 2024
 
Autodesk Construction Cloud (Autodesk Build).pptx
Autodesk Construction Cloud (Autodesk Build).pptxAutodesk Construction Cloud (Autodesk Build).pptx
Autodesk Construction Cloud (Autodesk Build).pptx
 
Independent Solar-Powered Electric Vehicle Charging Station
Independent Solar-Powered Electric Vehicle Charging StationIndependent Solar-Powered Electric Vehicle Charging Station
Independent Solar-Powered Electric Vehicle Charging Station
 
Passive Air Cooling System and Solar Water Heater.ppt
Passive Air Cooling System and Solar Water Heater.pptPassive Air Cooling System and Solar Water Heater.ppt
Passive Air Cooling System and Solar Water Heater.ppt
 
UNIT 4 PTRP final Convergence in probability.pptx
UNIT 4 PTRP final Convergence in probability.pptxUNIT 4 PTRP final Convergence in probability.pptx
UNIT 4 PTRP final Convergence in probability.pptx
 
Adsorption (mass transfer operations 2) ppt
Adsorption (mass transfer operations 2) pptAdsorption (mass transfer operations 2) ppt
Adsorption (mass transfer operations 2) ppt
 
Circuit Breakers for Engineering Students
Circuit Breakers for Engineering StudentsCircuit Breakers for Engineering Students
Circuit Breakers for Engineering Students
 

Sanely working with Legacy Code - PyTexas 2024

  • 1. Sanely working with Legacy Code Joe Cabrera
  • 2. Joe Cabrera ● Lead Software Engineer at ASAPP ● Python Programmer since 2009 ● Working on various legacy codebases for the last 5 years
  • 3. Goal: Give you the tools to succeed when dealing with legacy code
  • 4. What is Legacy Code?S Code without documentation
  • 5. What is Legacy Code?S Code without 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
  • 12. Increase code documentation Add function docstrings
  • 13. Increase code documentation Add documentation to explain logic when it’s not obvious
  • 14. Increase code documentation Explain mysterious variables
  • 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
  • 24. Share your changes to others on your team Everyone Profits