SlideShare a Scribd company logo
1 of 25
ENTERING THE
MATRIX
Work with Legacy Code
F. Garavaglia
08/2017
v. 1.0
o Photographer
o High-Aggressive-I-eat-you-
German-Shepherd-
Protected-by
I’m Francesco Garavaglia
o over 10 years of experience in IT consulting
companies.
o Took part in large scale projects (Energy
Markets, Bank, Insurance)
o Pay attention to Software architecture and
Business value
Agenda
◦ What is “Legacy Code”?
◦ See how it’s deep the white rabbit’s hole
◦ Code Samples
WHAT’S “LEGACY CODE”?
What problems are we trying to solve?
1
What’s “Legacy Code”?
Quiz: Legacy Code?
A. Code difficult to change
B. Inherited code difficult to change
C. Valuable code we’re afraid to change
What’s “Legacy Code”?
 Is there a Pragmatic Approach?
 Should I care about it?
 What about your feelings?
What’s “Legacy Code”?
o Nervous (I depend on… )
“engaged” with debugger , complex infrastructure
o Fear / Under pressure
.. so I do changes inside the existing code
o Unsafe
Did i broke something ? Is this working ?
o Resigned
“Edit and pray”
What’s “Legacy Code”?
o My Diagnosis:
You are in Matrix:
Then, you have to
take a pill
Cover and
Modify
Edit and
Pray
THE WHITE RABBIT’S HOLE
See how it is deep and dark
2
The white rabbit’s hole
We need a map for this journey: how
can we face with Legacy code?
UNDERSTAND COVER REFACTOR CHANGE
Each step has different target: focus
on 1 at times.
The white rabbit’s hole: HOW
• Use it before looking at the code
• Find one thing you know
1. look for keywords
2. and trace the actions backward
• Sketch refactoring
• Keep reading someone else code
The white rabbit’s hole: COVER
You need 100% of test coverage
if condition1
….
….
if condition2
…..
…..
if condition3
…..
…..
else
…..
…..
else
…..
…..
else
…..
…..
Start from shortest
to deepest branch
COVER: test the unknown
assume to know what the code is
supposed to do
… but what if we don’t ?
•Test name ???
•Expected result ???
COVER: test the unknown
1. Write a test named “x”
2. Set any expected result
3. Run it and get a Failure
4. Copy the text and make it pass
5. Give a better name
COVER: test the unknown
Extract the code you want to change in a
separate section
- Smaller problem to solve
- Quicker way to the coverage
EXAMPLE: Subclass and Override
public class A {
public void do1(…) {
user = session.getAttribute(“user”);
...
}
}
Souce Code
public void do1(…) {
...
logged = isLogged(“mario")
}
protected Boolean isLogged(..)
public class ATest
{
[TestMEthod]
public void do1WhenLogged() { }
private class TestableA : A
{
protected override boolean isLogged()
{
return true
}
}
}
Test Code
The white rabbit’s hole: REFACTOR
• Related to change phase
Start
End
Add a Test
Hard to
Create or
Make it Pass?
Refactor
Apply the
change
The white rabbit’s hole: REFACTOR
• Start from the deepest brach
if condition1
….
….
if condition2
…..
…..
if condition3
…..
…..
else
…..
…..
else
…..
…..
else
…..
…..
The white rabbit’s hole: CHANGE
Adding a new feature: Common mistake
“Let’s put new feature inside this existing
method because it should happen at the
same time”
Problem : … test old and new code together
EXAMPLE: Adding a new feature
public void do1()
{
…
// new Feature(..).apply(…)
}
Souce Code
public class Feature
{
public void apply(…)
{
…
}
}
public void do1()
{
…
new Feature(..).apply(…)
}
Productive Code
TDD
DON’T REPEAT YOURSELF
◦ Split screen vertically TDD
◦ Continuous testing plugin
◦ Use shortcuts instead of repeating actions
◦ Small commits / steps
Thanks
Thank you for your time
Francesco.garavaglia@gmail.com
REFERENCES
• Refactoring: Improving the Design of Existing Code
https://www.amazon.it/Refactoring-Improving-Design-Existing-
Code/dp/0201485672/ref=sr_1_1?ie=UTF8&qid=1504536816&sr=8-
1&keywords=Refactoring%3A+Improving+the+Design+of+Existing+Code
• M. Feathers- Working Effectively with Legacy Code
https://www.amazon.it/Working-Effectively-Legacy-Michael-
Feathers/dp/0131177052/ref=sr_1_1?s=english-books&ie=UTF8&qid=1504536861&sr=1-
1&keywords=Working+Effectively+with+Legacy+Code%3A+Michael+Feathers

More Related Content

Similar to Entering the matrix

Stop wasting-time-by-applying-clean-code-principles
Stop wasting-time-by-applying-clean-code-principlesStop wasting-time-by-applying-clean-code-principles
Stop wasting-time-by-applying-clean-code-principlesEdorian
 
Refactoring, 2nd Edition
Refactoring, 2nd EditionRefactoring, 2nd Edition
Refactoring, 2nd Editionjexp
 
sitBRU - The Hitchhikers Guide to the Legacy
sitBRU - The Hitchhikers Guide to the LegacysitBRU - The Hitchhikers Guide to the Legacy
sitBRU - The Hitchhikers Guide to the LegacyLaurens van Rijn
 
20191116 DevFest 2019 The Legacy Code came to stay (El legacy vino para queda...
20191116 DevFest 2019 The Legacy Code came to stay (El legacy vino para queda...20191116 DevFest 2019 The Legacy Code came to stay (El legacy vino para queda...
20191116 DevFest 2019 The Legacy Code came to stay (El legacy vino para queda...Antonio de la Torre Fernández
 
Maintainable code
Maintainable codeMaintainable code
Maintainable codeRiverGlide
 
Escape the legacy code matrix - Vimercate
Escape the legacy code matrix - VimercateEscape the legacy code matrix - Vimercate
Escape the legacy code matrix - VimercateMario Russo
 
Showing How Security Has (And Hasn't) Improved, After Ten Years Of Trying
Showing How Security Has (And Hasn't) Improved, After Ten Years Of TryingShowing How Security Has (And Hasn't) Improved, After Ten Years Of Trying
Showing How Security Has (And Hasn't) Improved, After Ten Years Of TryingDan Kaminsky
 
TDD and Simple Design Workshop - Session 1 - March 2019
TDD and Simple Design Workshop - Session 1 - March 2019TDD and Simple Design Workshop - Session 1 - March 2019
TDD and Simple Design Workshop - Session 1 - March 2019Paulo Clavijo
 
sitHVR - The Hitchhikers Guide to the Legacy
sitHVR - The Hitchhikers Guide to the LegacysitHVR - The Hitchhikers Guide to the Legacy
sitHVR - The Hitchhikers Guide to the LegacyLaurens van Rijn
 
Planning JavaScript and Ajax for larger teams
Planning JavaScript and Ajax for larger teamsPlanning JavaScript and Ajax for larger teams
Planning JavaScript and Ajax for larger teamsChristian Heilmann
 
Matlab for a computational PhD
Matlab for a computational PhDMatlab for a computational PhD
Matlab for a computational PhDAlbanLevy
 
Stop fearing legacy code
Stop fearing legacy codeStop fearing legacy code
Stop fearing legacy codeYaki Koren
 
Working With Legacy Code
Working With Legacy CodeWorking With Legacy Code
Working With Legacy CodeAndrea Polci
 
How I Learned to Stop Worrying and Love Legacy Code.....
How I Learned to Stop Worrying and Love Legacy Code.....How I Learned to Stop Worrying and Love Legacy Code.....
How I Learned to Stop Worrying and Love Legacy Code.....Mike Harris
 
How I Learned to Stop Worrying and Love Legacy Code - Ox:Agile 2018
How I Learned to Stop Worrying and Love Legacy Code - Ox:Agile 2018How I Learned to Stop Worrying and Love Legacy Code - Ox:Agile 2018
How I Learned to Stop Worrying and Love Legacy Code - Ox:Agile 2018Mike Harris
 
Test Driving Legacy Code Mini Workshop
Test Driving Legacy Code Mini WorkshopTest Driving Legacy Code Mini Workshop
Test Driving Legacy Code Mini WorkshopFernando Cuenca
 
TDD in Python With Pytest
TDD in Python With PytestTDD in Python With Pytest
TDD in Python With PytestEddy Reyes
 
Software Development Essential Skills
Software Development Essential SkillsSoftware Development Essential Skills
Software Development Essential SkillsJohn Choi
 

Similar to Entering the matrix (20)

Clean Code 2
Clean Code 2Clean Code 2
Clean Code 2
 
Stop wasting-time-by-applying-clean-code-principles
Stop wasting-time-by-applying-clean-code-principlesStop wasting-time-by-applying-clean-code-principles
Stop wasting-time-by-applying-clean-code-principles
 
Refactoring, 2nd Edition
Refactoring, 2nd EditionRefactoring, 2nd Edition
Refactoring, 2nd Edition
 
sitBRU - The Hitchhikers Guide to the Legacy
sitBRU - The Hitchhikers Guide to the LegacysitBRU - The Hitchhikers Guide to the Legacy
sitBRU - The Hitchhikers Guide to the Legacy
 
20191116 DevFest 2019 The Legacy Code came to stay (El legacy vino para queda...
20191116 DevFest 2019 The Legacy Code came to stay (El legacy vino para queda...20191116 DevFest 2019 The Legacy Code came to stay (El legacy vino para queda...
20191116 DevFest 2019 The Legacy Code came to stay (El legacy vino para queda...
 
Maintainable code
Maintainable codeMaintainable code
Maintainable code
 
Escape the legacy code matrix - Vimercate
Escape the legacy code matrix - VimercateEscape the legacy code matrix - Vimercate
Escape the legacy code matrix - Vimercate
 
Showing How Security Has (And Hasn't) Improved, After Ten Years Of Trying
Showing How Security Has (And Hasn't) Improved, After Ten Years Of TryingShowing How Security Has (And Hasn't) Improved, After Ten Years Of Trying
Showing How Security Has (And Hasn't) Improved, After Ten Years Of Trying
 
TDD and Simple Design Workshop - Session 1 - March 2019
TDD and Simple Design Workshop - Session 1 - March 2019TDD and Simple Design Workshop - Session 1 - March 2019
TDD and Simple Design Workshop - Session 1 - March 2019
 
sitHVR - The Hitchhikers Guide to the Legacy
sitHVR - The Hitchhikers Guide to the LegacysitHVR - The Hitchhikers Guide to the Legacy
sitHVR - The Hitchhikers Guide to the Legacy
 
Planning JavaScript and Ajax for larger teams
Planning JavaScript and Ajax for larger teamsPlanning JavaScript and Ajax for larger teams
Planning JavaScript and Ajax for larger teams
 
Matlab for a computational PhD
Matlab for a computational PhDMatlab for a computational PhD
Matlab for a computational PhD
 
Stop fearing legacy code
Stop fearing legacy codeStop fearing legacy code
Stop fearing legacy code
 
Working With Legacy Code
Working With Legacy CodeWorking With Legacy Code
Working With Legacy Code
 
Tdd is not about testing
Tdd is not about testingTdd is not about testing
Tdd is not about testing
 
How I Learned to Stop Worrying and Love Legacy Code.....
How I Learned to Stop Worrying and Love Legacy Code.....How I Learned to Stop Worrying and Love Legacy Code.....
How I Learned to Stop Worrying and Love Legacy Code.....
 
How I Learned to Stop Worrying and Love Legacy Code - Ox:Agile 2018
How I Learned to Stop Worrying and Love Legacy Code - Ox:Agile 2018How I Learned to Stop Worrying and Love Legacy Code - Ox:Agile 2018
How I Learned to Stop Worrying and Love Legacy Code - Ox:Agile 2018
 
Test Driving Legacy Code Mini Workshop
Test Driving Legacy Code Mini WorkshopTest Driving Legacy Code Mini Workshop
Test Driving Legacy Code Mini Workshop
 
TDD in Python With Pytest
TDD in Python With PytestTDD in Python With Pytest
TDD in Python With Pytest
 
Software Development Essential Skills
Software Development Essential SkillsSoftware Development Essential Skills
Software Development Essential Skills
 

More from Francesco Garavaglia (7)

Introduction to DevOps
Introduction to DevOpsIntroduction to DevOps
Introduction to DevOps
 
Workshop - cqrs brief introduction
Workshop - cqrs brief introductionWorkshop - cqrs brief introduction
Workshop - cqrs brief introduction
 
CQRS recepies
CQRS recepiesCQRS recepies
CQRS recepies
 
IOC in Unity
IOC in Unity  IOC in Unity
IOC in Unity
 
IOC in unity
IOC in unityIOC in unity
IOC in unity
 
Work shop eventstorming
Work shop  eventstormingWork shop  eventstorming
Work shop eventstorming
 
Workshop unit test
Workshop   unit testWorkshop   unit test
Workshop unit test
 

Recently uploaded

Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...soniya singh
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataBradBedford3
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024StefanoLambiase
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio, Inc.
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxTier1 app
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - InfographicHr365.us smith
 
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样umasea
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaHanief Utama
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideChristina Lin
 
Intelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmIntelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmSujith Sukumaran
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfAlina Yurenko
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...stazi3110
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackVICTOR MAESTRE RAMIREZ
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...Christina Lin
 
The Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdfThe Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdfPower Karaoke
 
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsAhmed Mohamed
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityNeo4j
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)OPEN KNOWLEDGE GmbH
 

Recently uploaded (20)

Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - Infographic
 
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief Utama
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
 
Intelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmIntelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalm
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStack
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
 
The Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdfThe Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdf
 
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML Diagrams
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered Sustainability
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)
 
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort ServiceHot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
 

Entering the matrix

  • 1. ENTERING THE MATRIX Work with Legacy Code F. Garavaglia 08/2017 v. 1.0
  • 2. o Photographer o High-Aggressive-I-eat-you- German-Shepherd- Protected-by I’m Francesco Garavaglia o over 10 years of experience in IT consulting companies. o Took part in large scale projects (Energy Markets, Bank, Insurance) o Pay attention to Software architecture and Business value
  • 3. Agenda ◦ What is “Legacy Code”? ◦ See how it’s deep the white rabbit’s hole ◦ Code Samples
  • 4. WHAT’S “LEGACY CODE”? What problems are we trying to solve? 1
  • 5. What’s “Legacy Code”? Quiz: Legacy Code? A. Code difficult to change B. Inherited code difficult to change C. Valuable code we’re afraid to change
  • 6. What’s “Legacy Code”?  Is there a Pragmatic Approach?  Should I care about it?  What about your feelings?
  • 7.
  • 8. What’s “Legacy Code”? o Nervous (I depend on… ) “engaged” with debugger , complex infrastructure o Fear / Under pressure .. so I do changes inside the existing code o Unsafe Did i broke something ? Is this working ? o Resigned “Edit and pray”
  • 9. What’s “Legacy Code”? o My Diagnosis: You are in Matrix: Then, you have to take a pill
  • 11. THE WHITE RABBIT’S HOLE See how it is deep and dark 2
  • 12. The white rabbit’s hole We need a map for this journey: how can we face with Legacy code? UNDERSTAND COVER REFACTOR CHANGE Each step has different target: focus on 1 at times.
  • 13. The white rabbit’s hole: HOW • Use it before looking at the code • Find one thing you know 1. look for keywords 2. and trace the actions backward • Sketch refactoring • Keep reading someone else code
  • 14. The white rabbit’s hole: COVER You need 100% of test coverage if condition1 …. …. if condition2 ….. ….. if condition3 ….. ….. else ….. ….. else ….. ….. else ….. ….. Start from shortest to deepest branch
  • 15. COVER: test the unknown assume to know what the code is supposed to do … but what if we don’t ? •Test name ??? •Expected result ???
  • 16. COVER: test the unknown 1. Write a test named “x” 2. Set any expected result 3. Run it and get a Failure 4. Copy the text and make it pass 5. Give a better name
  • 17. COVER: test the unknown Extract the code you want to change in a separate section - Smaller problem to solve - Quicker way to the coverage
  • 18. EXAMPLE: Subclass and Override public class A { public void do1(…) { user = session.getAttribute(“user”); ... } } Souce Code public void do1(…) { ... logged = isLogged(“mario") } protected Boolean isLogged(..) public class ATest { [TestMEthod] public void do1WhenLogged() { } private class TestableA : A { protected override boolean isLogged() { return true } } } Test Code
  • 19. The white rabbit’s hole: REFACTOR • Related to change phase Start End Add a Test Hard to Create or Make it Pass? Refactor Apply the change
  • 20. The white rabbit’s hole: REFACTOR • Start from the deepest brach if condition1 …. …. if condition2 ….. ….. if condition3 ….. ….. else ….. ….. else ….. ….. else ….. …..
  • 21. The white rabbit’s hole: CHANGE Adding a new feature: Common mistake “Let’s put new feature inside this existing method because it should happen at the same time” Problem : … test old and new code together
  • 22. EXAMPLE: Adding a new feature public void do1() { … // new Feature(..).apply(…) } Souce Code public class Feature { public void apply(…) { … } } public void do1() { … new Feature(..).apply(…) } Productive Code TDD
  • 23. DON’T REPEAT YOURSELF ◦ Split screen vertically TDD ◦ Continuous testing plugin ◦ Use shortcuts instead of repeating actions ◦ Small commits / steps
  • 24. Thanks Thank you for your time Francesco.garavaglia@gmail.com
  • 25. REFERENCES • Refactoring: Improving the Design of Existing Code https://www.amazon.it/Refactoring-Improving-Design-Existing- Code/dp/0201485672/ref=sr_1_1?ie=UTF8&qid=1504536816&sr=8- 1&keywords=Refactoring%3A+Improving+the+Design+of+Existing+Code • M. Feathers- Working Effectively with Legacy Code https://www.amazon.it/Working-Effectively-Legacy-Michael- Feathers/dp/0131177052/ref=sr_1_1?s=english-books&ie=UTF8&qid=1504536861&sr=1- 1&keywords=Working+Effectively+with+Legacy+Code%3A+Michael+Feathers