SlideShare a Scribd company logo
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

Clean Code 2
Clean Code 2Clean Code 2
Clean Code 2
Fredrik Wendt
 
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
Edorian
 
Refactoring, 2nd Edition
Refactoring, 2nd EditionRefactoring, 2nd Edition
Refactoring, 2nd Edition
jexp
 
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
Laurens 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 code
RiverGlide
 
Escape the legacy code matrix - Vimercate
Escape the legacy code matrix - VimercateEscape the legacy code matrix - Vimercate
Escape the legacy code matrix - Vimercate
Mario 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 Trying
Dan 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 2019
Paulo 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 Legacy
Laurens 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 teams
Christian Heilmann
 
Matlab for a computational PhD
Matlab for a computational PhDMatlab for a computational PhD
Matlab for a computational PhD
AlbanLevy
 
Stop fearing legacy code
Stop fearing legacy codeStop fearing legacy code
Stop fearing legacy code
Yaki Koren
 
Working With Legacy Code
Working With Legacy CodeWorking With Legacy Code
Working With Legacy Code
Andrea Polci
 
Tdd is not about testing
Tdd is not about testingTdd is not about testing
Tdd is not about testing
Gianluca Padovani
 
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 2018
Mike Harris
 
Test Driving Legacy Code Mini Workshop
Test Driving Legacy Code Mini WorkshopTest Driving Legacy Code Mini Workshop
Test Driving Legacy Code Mini Workshop
Fernando Cuenca
 
TDD in Python With Pytest
TDD in Python With PytestTDD in Python With Pytest
TDD in Python With Pytest
Eddy Reyes
 
Software Development Essential Skills
Software Development Essential SkillsSoftware Development Essential Skills
Software Development Essential Skills
John 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

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

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

AI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI App
AI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI AppAI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI App
AI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI App
Google
 
Energy consumption of Database Management - Florina Jonuzi
Energy consumption of Database Management - Florina JonuziEnergy consumption of Database Management - Florina Jonuzi
Energy consumption of Database Management - Florina Jonuzi
Green Software Development
 
How to write a program in any programming language
How to write a program in any programming languageHow to write a program in any programming language
How to write a program in any programming language
Rakesh Kumar R
 
Microservice Teams - How the cloud changes the way we work
Microservice Teams - How the cloud changes the way we workMicroservice Teams - How the cloud changes the way we work
Microservice Teams - How the cloud changes the way we work
Sven Peters
 
E-commerce Development Services- Hornet Dynamics
E-commerce Development Services- Hornet DynamicsE-commerce Development Services- Hornet Dynamics
E-commerce Development Services- Hornet Dynamics
Hornet Dynamics
 
DDS-Security 1.2 - What's New? Stronger security for long-running systems
DDS-Security 1.2 - What's New? Stronger security for long-running systemsDDS-Security 1.2 - What's New? Stronger security for long-running systems
DDS-Security 1.2 - What's New? Stronger security for long-running systems
Gerardo Pardo-Castellote
 
2024 eCommerceDays Toulouse - Sylius 2.0.pdf
2024 eCommerceDays Toulouse - Sylius 2.0.pdf2024 eCommerceDays Toulouse - Sylius 2.0.pdf
2024 eCommerceDays Toulouse - Sylius 2.0.pdf
Łukasz Chruściel
 
Graspan: A Big Data System for Big Code Analysis
Graspan: A Big Data System for Big Code AnalysisGraspan: A Big Data System for Big Code Analysis
Graspan: A Big Data System for Big Code Analysis
Aftab Hussain
 
GraphSummit Paris - The art of the possible with Graph Technology
GraphSummit Paris - The art of the possible with Graph TechnologyGraphSummit Paris - The art of the possible with Graph Technology
GraphSummit Paris - The art of the possible with Graph Technology
Neo4j
 
Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604
Fermin Galan
 
Neo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j - Product Vision and Knowledge Graphs - GraphSummit ParisNeo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j
 
Need for Speed: Removing speed bumps from your Symfony projects ⚡️
Need for Speed: Removing speed bumps from your Symfony projects ⚡️Need for Speed: Removing speed bumps from your Symfony projects ⚡️
Need for Speed: Removing speed bumps from your Symfony projects ⚡️
Łukasz Chruściel
 
LORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOM
LORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOMLORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOM
LORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOM
lorraineandreiamcidl
 
Empowering Growth with Best Software Development Company in Noida - Deuglo
Empowering Growth with Best Software  Development Company in Noida - DeugloEmpowering Growth with Best Software  Development Company in Noida - Deuglo
Empowering Growth with Best Software Development Company in Noida - Deuglo
Deuglo Infosystem Pvt Ltd
 
UI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
UI5con 2024 - Boost Your Development Experience with UI5 Tooling ExtensionsUI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
UI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
Peter Muessig
 
Transform Your Communication with Cloud-Based IVR Solutions
Transform Your Communication with Cloud-Based IVR SolutionsTransform Your Communication with Cloud-Based IVR Solutions
Transform Your Communication with Cloud-Based IVR Solutions
TheSMSPoint
 
Why Choose Odoo 17 Community & How it differs from Odoo 17 Enterprise Edition
Why Choose Odoo 17 Community & How it differs from Odoo 17 Enterprise EditionWhy Choose Odoo 17 Community & How it differs from Odoo 17 Enterprise Edition
Why Choose Odoo 17 Community & How it differs from Odoo 17 Enterprise Edition
Envertis Software Solutions
 
Mobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona InfotechMobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona Infotech
Drona Infotech
 
原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
mz5nrf0n
 
Introducing Crescat - Event Management Software for Venues, Festivals and Eve...
Introducing Crescat - Event Management Software for Venues, Festivals and Eve...Introducing Crescat - Event Management Software for Venues, Festivals and Eve...
Introducing Crescat - Event Management Software for Venues, Festivals and Eve...
Crescat
 

Recently uploaded (20)

AI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI App
AI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI AppAI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI App
AI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI App
 
Energy consumption of Database Management - Florina Jonuzi
Energy consumption of Database Management - Florina JonuziEnergy consumption of Database Management - Florina Jonuzi
Energy consumption of Database Management - Florina Jonuzi
 
How to write a program in any programming language
How to write a program in any programming languageHow to write a program in any programming language
How to write a program in any programming language
 
Microservice Teams - How the cloud changes the way we work
Microservice Teams - How the cloud changes the way we workMicroservice Teams - How the cloud changes the way we work
Microservice Teams - How the cloud changes the way we work
 
E-commerce Development Services- Hornet Dynamics
E-commerce Development Services- Hornet DynamicsE-commerce Development Services- Hornet Dynamics
E-commerce Development Services- Hornet Dynamics
 
DDS-Security 1.2 - What's New? Stronger security for long-running systems
DDS-Security 1.2 - What's New? Stronger security for long-running systemsDDS-Security 1.2 - What's New? Stronger security for long-running systems
DDS-Security 1.2 - What's New? Stronger security for long-running systems
 
2024 eCommerceDays Toulouse - Sylius 2.0.pdf
2024 eCommerceDays Toulouse - Sylius 2.0.pdf2024 eCommerceDays Toulouse - Sylius 2.0.pdf
2024 eCommerceDays Toulouse - Sylius 2.0.pdf
 
Graspan: A Big Data System for Big Code Analysis
Graspan: A Big Data System for Big Code AnalysisGraspan: A Big Data System for Big Code Analysis
Graspan: A Big Data System for Big Code Analysis
 
GraphSummit Paris - The art of the possible with Graph Technology
GraphSummit Paris - The art of the possible with Graph TechnologyGraphSummit Paris - The art of the possible with Graph Technology
GraphSummit Paris - The art of the possible with Graph Technology
 
Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604
 
Neo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j - Product Vision and Knowledge Graphs - GraphSummit ParisNeo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
 
Need for Speed: Removing speed bumps from your Symfony projects ⚡️
Need for Speed: Removing speed bumps from your Symfony projects ⚡️Need for Speed: Removing speed bumps from your Symfony projects ⚡️
Need for Speed: Removing speed bumps from your Symfony projects ⚡️
 
LORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOM
LORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOMLORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOM
LORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOM
 
Empowering Growth with Best Software Development Company in Noida - Deuglo
Empowering Growth with Best Software  Development Company in Noida - DeugloEmpowering Growth with Best Software  Development Company in Noida - Deuglo
Empowering Growth with Best Software Development Company in Noida - Deuglo
 
UI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
UI5con 2024 - Boost Your Development Experience with UI5 Tooling ExtensionsUI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
UI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
 
Transform Your Communication with Cloud-Based IVR Solutions
Transform Your Communication with Cloud-Based IVR SolutionsTransform Your Communication with Cloud-Based IVR Solutions
Transform Your Communication with Cloud-Based IVR Solutions
 
Why Choose Odoo 17 Community & How it differs from Odoo 17 Enterprise Edition
Why Choose Odoo 17 Community & How it differs from Odoo 17 Enterprise EditionWhy Choose Odoo 17 Community & How it differs from Odoo 17 Enterprise Edition
Why Choose Odoo 17 Community & How it differs from Odoo 17 Enterprise Edition
 
Mobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona InfotechMobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona Infotech
 
原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
 
Introducing Crescat - Event Management Software for Venues, Festivals and Eve...
Introducing Crescat - Event Management Software for Venues, Festivals and Eve...Introducing Crescat - Event Management Software for Venues, Festivals and Eve...
Introducing Crescat - Event Management Software for Venues, Festivals and Eve...
 

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