refactoring legacy code - alvaro...
TRANSCRIPT
![Page 1: Refactoring Legacy Code - Alvaro Garciaalvarogarcia7.github.io/blog/uploads/refactoring-legacy... · 2020-04-04 · with Legacy Code, ISBN-13: 007-6092025986. Amazon [Refactoring]:](https://reader030.vdocuments.net/reader030/viewer/2022040101/5ed0f751b76ec202342cd7c4/html5/thumbnails/1.jpg)
Refactoring Legacy Code
@alvarobiz, 2015-06-29
![Page 2: Refactoring Legacy Code - Alvaro Garciaalvarogarcia7.github.io/blog/uploads/refactoring-legacy... · 2020-04-04 · with Legacy Code, ISBN-13: 007-6092025986. Amazon [Refactoring]:](https://reader030.vdocuments.net/reader030/viewer/2022040101/5ed0f751b76ec202342cd7c4/html5/thumbnails/2.jpg)
INTRODUCTION
TO LEGACY CODE
![Page 3: Refactoring Legacy Code - Alvaro Garciaalvarogarcia7.github.io/blog/uploads/refactoring-legacy... · 2020-04-04 · with Legacy Code, ISBN-13: 007-6092025986. Amazon [Refactoring]:](https://reader030.vdocuments.net/reader030/viewer/2022040101/5ed0f751b76ec202342cd7c4/html5/thumbnails/3.jpg)
What is legacy code
[…] legacy code as code without tests. It is a good working definition, and it points to a solution […]
M Feathers, in the preface of Working effectively with legacy code
![Page 4: Refactoring Legacy Code - Alvaro Garciaalvarogarcia7.github.io/blog/uploads/refactoring-legacy... · 2020-04-04 · with Legacy Code, ISBN-13: 007-6092025986. Amazon [Refactoring]:](https://reader030.vdocuments.net/reader030/viewer/2022040101/5ed0f751b76ec202342cd7c4/html5/thumbnails/4.jpg)
What is legacy code (II)
Legacy code is code without
tests that provide trust to all
your stakeholders
![Page 5: Refactoring Legacy Code - Alvaro Garciaalvarogarcia7.github.io/blog/uploads/refactoring-legacy... · 2020-04-04 · with Legacy Code, ISBN-13: 007-6092025986. Amazon [Refactoring]:](https://reader030.vdocuments.net/reader030/viewer/2022040101/5ed0f751b76ec202342cd7c4/html5/thumbnails/5.jpg)
What is tested code
Trust (reliable)
Cheap to modify
![Page 6: Refactoring Legacy Code - Alvaro Garciaalvarogarcia7.github.io/blog/uploads/refactoring-legacy... · 2020-04-04 · with Legacy Code, ISBN-13: 007-6092025986. Amazon [Refactoring]:](https://reader030.vdocuments.net/reader030/viewer/2022040101/5ed0f751b76ec202342cd7c4/html5/thumbnails/6.jpg)
CHANGING
LEGACY CODE
![Page 7: Refactoring Legacy Code - Alvaro Garciaalvarogarcia7.github.io/blog/uploads/refactoring-legacy... · 2020-04-04 · with Legacy Code, ISBN-13: 007-6092025986. Amazon [Refactoring]:](https://reader030.vdocuments.net/reader030/viewer/2022040101/5ed0f751b76ec202342cd7c4/html5/thumbnails/7.jpg)
How to get from A to B
Shortest
Safest
![Page 8: Refactoring Legacy Code - Alvaro Garciaalvarogarcia7.github.io/blog/uploads/refactoring-legacy... · 2020-04-04 · with Legacy Code, ISBN-13: 007-6092025986. Amazon [Refactoring]:](https://reader030.vdocuments.net/reader030/viewer/2022040101/5ed0f751b76ec202342cd7c4/html5/thumbnails/8.jpg)
Any long-lived software
0% 20% 40% 60% 80% 100%
Features/ LoC
To Keep To Change
![Page 9: Refactoring Legacy Code - Alvaro Garciaalvarogarcia7.github.io/blog/uploads/refactoring-legacy... · 2020-04-04 · with Legacy Code, ISBN-13: 007-6092025986. Amazon [Refactoring]:](https://reader030.vdocuments.net/reader030/viewer/2022040101/5ed0f751b76ec202342cd7c4/html5/thumbnails/9.jpg)
How to change from A to B
Keep old, risk new
Risk old, keep new
![Page 10: Refactoring Legacy Code - Alvaro Garciaalvarogarcia7.github.io/blog/uploads/refactoring-legacy... · 2020-04-04 · with Legacy Code, ISBN-13: 007-6092025986. Amazon [Refactoring]:](https://reader030.vdocuments.net/reader030/viewer/2022040101/5ed0f751b76ec202342cd7c4/html5/thumbnails/10.jpg)
Any long-lived software (II)
0% 20% 40% 60% 80% 100%
Features/ LoC
To Keep To Change
![Page 11: Refactoring Legacy Code - Alvaro Garciaalvarogarcia7.github.io/blog/uploads/refactoring-legacy... · 2020-04-04 · with Legacy Code, ISBN-13: 007-6092025986. Amazon [Refactoring]:](https://reader030.vdocuments.net/reader030/viewer/2022040101/5ed0f751b76ec202342cd7c4/html5/thumbnails/11.jpg)
THE
LEGACY CODE CHANGE
ALGORITHM
![Page 12: Refactoring Legacy Code - Alvaro Garciaalvarogarcia7.github.io/blog/uploads/refactoring-legacy... · 2020-04-04 · with Legacy Code, ISBN-13: 007-6092025986. Amazon [Refactoring]:](https://reader030.vdocuments.net/reader030/viewer/2022040101/5ed0f751b76ec202342cd7c4/html5/thumbnails/12.jpg)
The Legacy Code Change Algorithm
Identify change points.
Find test points.
Break dependencies.
Write tests.
Make changes and refactor.
Source: [Feathers04], Ch 2: Working with Feedback
![Page 13: Refactoring Legacy Code - Alvaro Garciaalvarogarcia7.github.io/blog/uploads/refactoring-legacy... · 2020-04-04 · with Legacy Code, ISBN-13: 007-6092025986. Amazon [Refactoring]:](https://reader030.vdocuments.net/reader030/viewer/2022040101/5ed0f751b76ec202342cd7c4/html5/thumbnails/13.jpg)
Legacy Code Change, example
Example: Ugly Trivia
We want to remove the
direct printing to the console (System.out.printXXX)
![Page 14: Refactoring Legacy Code - Alvaro Garciaalvarogarcia7.github.io/blog/uploads/refactoring-legacy... · 2020-04-04 · with Legacy Code, ISBN-13: 007-6092025986. Amazon [Refactoring]:](https://reader030.vdocuments.net/reader030/viewer/2022040101/5ed0f751b76ec202342cd7c4/html5/thumbnails/14.jpg)
The Legacy Code Change Algorithm
Identify change points.
Find test points.
Break dependencies.
Write tests.
Make changes and refactor.
Source: [Feathers04], Ch 2: Working with Feedback
![Page 15: Refactoring Legacy Code - Alvaro Garciaalvarogarcia7.github.io/blog/uploads/refactoring-legacy... · 2020-04-04 · with Legacy Code, ISBN-13: 007-6092025986. Amazon [Refactoring]:](https://reader030.vdocuments.net/reader030/viewer/2022040101/5ed0f751b76ec202342cd7c4/html5/thumbnails/15.jpg)
Change points
All direct invocations to
System.out.printXXX
![Page 16: Refactoring Legacy Code - Alvaro Garciaalvarogarcia7.github.io/blog/uploads/refactoring-legacy... · 2020-04-04 · with Legacy Code, ISBN-13: 007-6092025986. Amazon [Refactoring]:](https://reader030.vdocuments.net/reader030/viewer/2022040101/5ed0f751b76ec202342cd7c4/html5/thumbnails/16.jpg)
The Legacy Code Change Algorithm
Identify change points.
Find test points.
Break dependencies.
Write tests.
Make changes and refactor.
Source: [Feathers04], Ch 2: Working with Feedback
![Page 17: Refactoring Legacy Code - Alvaro Garciaalvarogarcia7.github.io/blog/uploads/refactoring-legacy... · 2020-04-04 · with Legacy Code, ISBN-13: 007-6092025986. Amazon [Refactoring]:](https://reader030.vdocuments.net/reader030/viewer/2022040101/5ed0f751b76ec202342cd7c4/html5/thumbnails/17.jpg)
Test points
The console output
![Page 18: Refactoring Legacy Code - Alvaro Garciaalvarogarcia7.github.io/blog/uploads/refactoring-legacy... · 2020-04-04 · with Legacy Code, ISBN-13: 007-6092025986. Amazon [Refactoring]:](https://reader030.vdocuments.net/reader030/viewer/2022040101/5ed0f751b76ec202342cd7c4/html5/thumbnails/18.jpg)
The Legacy Code Change Algorithm
Identify change points.
Find test points.
Break dependencies.
Write tests.
Make changes and refactor.
Source: [Feathers04], Ch 2: Working with Feedback
![Page 19: Refactoring Legacy Code - Alvaro Garciaalvarogarcia7.github.io/blog/uploads/refactoring-legacy... · 2020-04-04 · with Legacy Code, ISBN-13: 007-6092025986. Amazon [Refactoring]:](https://reader030.vdocuments.net/reader030/viewer/2022040101/5ed0f751b76ec202342cd7c4/html5/thumbnails/19.jpg)
Breaking dependencies
Are the execution
reproducible?
![Page 20: Refactoring Legacy Code - Alvaro Garciaalvarogarcia7.github.io/blog/uploads/refactoring-legacy... · 2020-04-04 · with Legacy Code, ISBN-13: 007-6092025986. Amazon [Refactoring]:](https://reader030.vdocuments.net/reader030/viewer/2022040101/5ed0f751b76ec202342cd7c4/html5/thumbnails/20.jpg)
The Legacy Code Change Algorithm
Identify change points.
Find test points.
Break dependencies.
Write tests.
Make changes and refactor.
Source: [Feathers04], Ch 2: Working with Feedback
![Page 21: Refactoring Legacy Code - Alvaro Garciaalvarogarcia7.github.io/blog/uploads/refactoring-legacy... · 2020-04-04 · with Legacy Code, ISBN-13: 007-6092025986. Amazon [Refactoring]:](https://reader030.vdocuments.net/reader030/viewer/2022040101/5ed0f751b76ec202342cd7c4/html5/thumbnails/21.jpg)
Writing tests
Capture the console output
Save and verify it
automatically
![Page 22: Refactoring Legacy Code - Alvaro Garciaalvarogarcia7.github.io/blog/uploads/refactoring-legacy... · 2020-04-04 · with Legacy Code, ISBN-13: 007-6092025986. Amazon [Refactoring]:](https://reader030.vdocuments.net/reader030/viewer/2022040101/5ed0f751b76ec202342cd7c4/html5/thumbnails/22.jpg)
The Legacy Code Change Algorithm
Identify change points.
Find test points.
Break dependencies.
Write tests.
Make changes and refactor.
Source: [Feathers04], Ch 2: Working with Feedback
![Page 23: Refactoring Legacy Code - Alvaro Garciaalvarogarcia7.github.io/blog/uploads/refactoring-legacy... · 2020-04-04 · with Legacy Code, ISBN-13: 007-6092025986. Amazon [Refactoring]:](https://reader030.vdocuments.net/reader030/viewer/2022040101/5ed0f751b76ec202342cd7c4/html5/thumbnails/23.jpg)
Making changes
Global search and replace
“System.out.println” for “log”
into an object
Verify, commit
![Page 24: Refactoring Legacy Code - Alvaro Garciaalvarogarcia7.github.io/blog/uploads/refactoring-legacy... · 2020-04-04 · with Legacy Code, ISBN-13: 007-6092025986. Amazon [Refactoring]:](https://reader030.vdocuments.net/reader030/viewer/2022040101/5ed0f751b76ec202342cd7c4/html5/thumbnails/24.jpg)
Refactoring
Inject the logging
collaborator
Verify, commit
![Page 25: Refactoring Legacy Code - Alvaro Garciaalvarogarcia7.github.io/blog/uploads/refactoring-legacy... · 2020-04-04 · with Legacy Code, ISBN-13: 007-6092025986. Amazon [Refactoring]:](https://reader030.vdocuments.net/reader030/viewer/2022040101/5ed0f751b76ec202342cd7c4/html5/thumbnails/25.jpg)
Refactoring
DRY on the messages
Verify, commit
![Page 26: Refactoring Legacy Code - Alvaro Garciaalvarogarcia7.github.io/blog/uploads/refactoring-legacy... · 2020-04-04 · with Legacy Code, ISBN-13: 007-6092025986. Amazon [Refactoring]:](https://reader030.vdocuments.net/reader030/viewer/2022040101/5ed0f751b76ec202342cd7c4/html5/thumbnails/26.jpg)
The Legacy Code Change Algorithm
Identify change points.
Find test points.
Break dependencies.
Write tests.
Make changes and refactor.
Source: [Feathers04], Ch 2: Working with Feedback
![Page 27: Refactoring Legacy Code - Alvaro Garciaalvarogarcia7.github.io/blog/uploads/refactoring-legacy... · 2020-04-04 · with Legacy Code, ISBN-13: 007-6092025986. Amazon [Refactoring]:](https://reader030.vdocuments.net/reader030/viewer/2022040101/5ed0f751b76ec202342cd7c4/html5/thumbnails/27.jpg)
The Legacy Code Change Algorithm
Rewriting Adapting Writing
Cost
![Page 28: Refactoring Legacy Code - Alvaro Garciaalvarogarcia7.github.io/blog/uploads/refactoring-legacy... · 2020-04-04 · with Legacy Code, ISBN-13: 007-6092025986. Amazon [Refactoring]:](https://reader030.vdocuments.net/reader030/viewer/2022040101/5ed0f751b76ec202342cd7c4/html5/thumbnails/28.jpg)
UNDERSTANDING
LEGACY CODE
![Page 29: Refactoring Legacy Code - Alvaro Garciaalvarogarcia7.github.io/blog/uploads/refactoring-legacy... · 2020-04-04 · with Legacy Code, ISBN-13: 007-6092025986. Amazon [Refactoring]:](https://reader030.vdocuments.net/reader030/viewer/2022040101/5ed0f751b76ec202342cd7c4/html5/thumbnails/29.jpg)
Sensing
[S]ense when we can’t
access values our code
computes
Source: [Feathers04]:
Chapter 3: Sensing and Separation
![Page 30: Refactoring Legacy Code - Alvaro Garciaalvarogarcia7.github.io/blog/uploads/refactoring-legacy... · 2020-04-04 · with Legacy Code, ISBN-13: 007-6092025986. Amazon [Refactoring]:](https://reader030.vdocuments.net/reader030/viewer/2022040101/5ed0f751b76ec202342cd7c4/html5/thumbnails/30.jpg)
Separating
[S]eparate when we can’t
even get a piece of code
into a test harness to run.
Source: [Feathers04]:
Chapter 3: Sensing and Separation
![Page 31: Refactoring Legacy Code - Alvaro Garciaalvarogarcia7.github.io/blog/uploads/refactoring-legacy... · 2020-04-04 · with Legacy Code, ISBN-13: 007-6092025986. Amazon [Refactoring]:](https://reader030.vdocuments.net/reader030/viewer/2022040101/5ed0f751b76ec202342cd7c4/html5/thumbnails/31.jpg)
Solutions
Fake Objects
Mock Objects
Seams
Dependency breaking
Source: [Feathers04]:
Chapter 3: Sensing and Separation
![Page 32: Refactoring Legacy Code - Alvaro Garciaalvarogarcia7.github.io/blog/uploads/refactoring-legacy... · 2020-04-04 · with Legacy Code, ISBN-13: 007-6092025986. Amazon [Refactoring]:](https://reader030.vdocuments.net/reader030/viewer/2022040101/5ed0f751b76ec202342cd7c4/html5/thumbnails/32.jpg)
EXPERIENCES
WITH
LEGACY CODE
![Page 33: Refactoring Legacy Code - Alvaro Garciaalvarogarcia7.github.io/blog/uploads/refactoring-legacy... · 2020-04-04 · with Legacy Code, ISBN-13: 007-6092025986. Amazon [Refactoring]:](https://reader030.vdocuments.net/reader030/viewer/2022040101/5ed0f751b76ec202342cd7c4/html5/thumbnails/33.jpg)
Motivation (I)
![Page 34: Refactoring Legacy Code - Alvaro Garciaalvarogarcia7.github.io/blog/uploads/refactoring-legacy... · 2020-04-04 · with Legacy Code, ISBN-13: 007-6092025986. Amazon [Refactoring]:](https://reader030.vdocuments.net/reader030/viewer/2022040101/5ed0f751b76ec202342cd7c4/html5/thumbnails/34.jpg)
Motivation (II)
I have nothing to
offer but blood, toil,
tears and sweat.
W. Churchill
![Page 35: Refactoring Legacy Code - Alvaro Garciaalvarogarcia7.github.io/blog/uploads/refactoring-legacy... · 2020-04-04 · with Legacy Code, ISBN-13: 007-6092025986. Amazon [Refactoring]:](https://reader030.vdocuments.net/reader030/viewer/2022040101/5ed0f751b76ec202342cd7c4/html5/thumbnails/35.jpg)
How to get from A to B
One step at a time
![Page 36: Refactoring Legacy Code - Alvaro Garciaalvarogarcia7.github.io/blog/uploads/refactoring-legacy... · 2020-04-04 · with Legacy Code, ISBN-13: 007-6092025986. Amazon [Refactoring]:](https://reader030.vdocuments.net/reader030/viewer/2022040101/5ed0f751b76ec202342cd7c4/html5/thumbnails/36.jpg)
How to get from A to B
Low-hanging fruit
![Page 37: Refactoring Legacy Code - Alvaro Garciaalvarogarcia7.github.io/blog/uploads/refactoring-legacy... · 2020-04-04 · with Legacy Code, ISBN-13: 007-6092025986. Amazon [Refactoring]:](https://reader030.vdocuments.net/reader030/viewer/2022040101/5ed0f751b76ec202342cd7c4/html5/thumbnails/37.jpg)
How to get from A to B
Refactor relentlessly
![Page 38: Refactoring Legacy Code - Alvaro Garciaalvarogarcia7.github.io/blog/uploads/refactoring-legacy... · 2020-04-04 · with Legacy Code, ISBN-13: 007-6092025986. Amazon [Refactoring]:](https://reader030.vdocuments.net/reader030/viewer/2022040101/5ed0f751b76ec202342cd7c4/html5/thumbnails/38.jpg)
How to get from A to B
Do not stop the
presses, prefer
along the way
![Page 39: Refactoring Legacy Code - Alvaro Garciaalvarogarcia7.github.io/blog/uploads/refactoring-legacy... · 2020-04-04 · with Legacy Code, ISBN-13: 007-6092025986. Amazon [Refactoring]:](https://reader030.vdocuments.net/reader030/viewer/2022040101/5ed0f751b76ec202342cd7c4/html5/thumbnails/39.jpg)
How to get from A to B
Make it usable, then
forget for a while
![Page 40: Refactoring Legacy Code - Alvaro Garciaalvarogarcia7.github.io/blog/uploads/refactoring-legacy... · 2020-04-04 · with Legacy Code, ISBN-13: 007-6092025986. Amazon [Refactoring]:](https://reader030.vdocuments.net/reader030/viewer/2022040101/5ed0f751b76ec202342cd7c4/html5/thumbnails/40.jpg)
![Page 41: Refactoring Legacy Code - Alvaro Garciaalvarogarcia7.github.io/blog/uploads/refactoring-legacy... · 2020-04-04 · with Legacy Code, ISBN-13: 007-6092025986. Amazon [Refactoring]:](https://reader030.vdocuments.net/reader030/viewer/2022040101/5ed0f751b76ec202342cd7c4/html5/thumbnails/41.jpg)
GETTING
(PROFESSIONAL)
HELP
![Page 42: Refactoring Legacy Code - Alvaro Garciaalvarogarcia7.github.io/blog/uploads/refactoring-legacy... · 2020-04-04 · with Legacy Code, ISBN-13: 007-6092025986. Amazon [Refactoring]:](https://reader030.vdocuments.net/reader030/viewer/2022040101/5ed0f751b76ec202342cd7c4/html5/thumbnails/42.jpg)
![Page 43: Refactoring Legacy Code - Alvaro Garciaalvarogarcia7.github.io/blog/uploads/refactoring-legacy... · 2020-04-04 · with Legacy Code, ISBN-13: 007-6092025986. Amazon [Refactoring]:](https://reader030.vdocuments.net/reader030/viewer/2022040101/5ed0f751b76ec202342cd7c4/html5/thumbnails/43.jpg)
![Page 44: Refactoring Legacy Code - Alvaro Garciaalvarogarcia7.github.io/blog/uploads/refactoring-legacy... · 2020-04-04 · with Legacy Code, ISBN-13: 007-6092025986. Amazon [Refactoring]:](https://reader030.vdocuments.net/reader030/viewer/2022040101/5ed0f751b76ec202342cd7c4/html5/thumbnails/44.jpg)
Bibliography
[Feathers04]: Feathers, M. Working Effectively
with Legacy Code, ISBN-13: 007-6092025986.
Amazon
[Refactoring]: Fowler, M with Beck, Brant, Opdyke,
and Roberts. Refactoring: Improving the Design of
Existing Code,ISBN-13: 978-0201485677 Official
page
[Kerievsky04]: Kerievsky, J. Refactoring to Patterns.
ISBN-13: 078-5342213355 Official page