SlideShare a Scribd company logo
&
FÉLIX-ANTOINE
BOURBONNAIS
B.ING., M.SC, PSM
Version: Mai 2017 (DEV)
Characterization Testing :
regaining control
of your Legacy Code
PASCAL ROY
ING., CSM, PSM, PMP
Think how Legacy code affects
your work in everyday life…
Imagine a tool that helps you
explore, understand and
refactor it without fear.
… a nice dream, isn’t?
4
Qui sommes-nous ?
Pascal Roy
Ing., PSM, CSM, PMP
Félix-Antoine Bourbonnais
B.ing., PSM, M.Sc.
Training Mentoring Diagnostics Talks
Speakers
Trainers
Coaches
&
Mentors
Tech.
Team &
Business
Mgmt.
TDD
Emergent
architecture
Automated
tests
DDD
…
Scrum
Agile QA
Project
management
Agility
BDD
> We are
Strategic
advice
> What we do
Am I the only one to
have Legacy Code ?
Code that is difficult to
modify and evolve.
Regardless how old it is or
why it got this way.
What is Legacy Code ?
• Code written by others
• Code nobody wants to touch anymore
• Code no longer supported by the people who wrote it
• Code that could be rewritten using better coding practices, tools
or languages
• ...
Other popular definitions …
Code without tests
Michael Feathers,
Working Effectively with Legacy Code
Two basic
strategies…
What to do with my Legacy Code ?
Fear : software
development’s worst
enemy
Tired of the continuous stress
of delivery, of endless
debugging sessions, of the fear
of breaking something?
Please, give me a
new project
!@/$%!/%
The cycle of death…
Legacy Code No tests
No
refactoringPatches
Why not
rejuvenate
your code?
Correctly equipped,
yes you can !
Gradually,
while still producing
business value
Pick your next « Story »
and begin paying out
some of that debt!
Characterization
test
A characterization test describes the actual behavior
of an existing piece of code.
- Michael Feathers
Overcome fear!
To…
Understand and
document what
the code does
Protect against
unintended
changes when
refactoring
+
1. Find and isolate a piece of code that you need to analyze or
modify
2. Write a test that uses that code, use an assertion you know
will fail
3. Execute the test and let it tell you the actual behavior
4. Change the test assertion and the naming to match the actual
behavior
5. Repeat…
5 steps for writing a characterization test
public class SalesUtil {
double BQ = 1000.0;
double BCR = 0.20;
double OQM1 = 1.5;
double OQM2 = OQM1 * 2;
public double calculate(double tSales) {
if (tSales <= BQ) {
return tSales * BCR;
} else if (tSales <= BQ * 2) {
return (BQ) * BCR + (tSales - BQ) * BCR * OQM1;
} else {
return (BQ) * BCR +
(tSales - BQ) * BCR * OQM1 +
(tSales - BQ * 2) * BCR * OQM2;
}
}
}
http://s3.amazonaws.com/giles/demons_010609/wtfm.jpg
An example of Legacy Code?
WTF?
WTF?
WTF?
public class SalesUtil {
double BQ = 1000.0;
double BCR = 0.20;
double OQM1 = 1.5;
double OQM2 = OQM1 * 2;
public double calculate(double tSales){
if (tSales <= BQ) {
return tSales * BCR;
} else if (tSales <= BQ * 2) {
return (BQ) * BCR +
(tSales - BQ) * BCR * OQM1;
} else {
return (BQ) * BCR +
(tSales - BQ) * BCR * OQM1 +
(tSales - BQ * 2) * BCR * OQM2;
}
}
}
@Test
public void test… {
assert(...)
}
Step 1: identify a piece of code to characterize
1
?
2
? ?
1
2
public class SalesUtil {
double BQ = 1000.0;
double BCR = 0.20;
double OQM1 = 1.5;
double OQM2 = OQM1 * 2;
double calculate(double tSales) {
if (tSales <= BQ) {
return tSales * BCR;
} else if (tSales <= BQ * 2) {
return (BQ) * BCR +
(tSales - BQ) * BCR * OQM1;
} else {
return (BQ) * BCR +
(tSales - BQ) * BCR * OQM1+
(tSales - BQ*2)*BCR * OQM2;
}
}
}
@Test
public void testCalculate() {
assertEquals(
0.0,
SalesUtil.calculate(1000.0)
);
}
Step 2: write a failing assertion
1
?
2
? ?
1
2
public class SalesUtil {
double BQ = 1000.0;
double BCR = 0.20;
double OQM1 = 1.5;
double OQM2 = OQM1 * 2;
double calculate(double tSales) {
if (tSales <= BQ) {
return tSales * BCR;
} else if (tSales <= BQ * 2) {
return (BQ) * BCR +
(tSales - BQ) * BCR * OQM1;
} else {
return (BQ) * BCR +
(tSales - BQ) * BCR * OQM1 +
(tSales - BQ * 2) * BCR * OQM2;
}
}
}
@Test
public void testCalculate() {
assertEquals(
0.0,
SalesUtil.calculate(1000.0)
);
}
Step 3: execute the test
+ find out the actual behavior is
> junit.framework.AssertionFailedError:
expected:<0.0> but was:<200.0>1
?
2
? ?
public class SalesUtil {
double BQ = 1000.0;
double BCR = 0.20;
double OQM1 = 1.5;
double OQM2 = OQM1 * 2;
double calculate(double tSales) {
if (tSales <= BQ) {
return tSales * BCR;
} else if (tSales <= BQ * 2) {
return (BQ) * BCR +
(tSales - BQ) * BCR * OQM1;
} else {
return (BQ) * BCR +
(tSales - BQ) * BCR * OQM1+
(tSales – BQ*2)*BCR * OQM2;
}
}
}
@Test
public void
lessThanBaseQuota_useBaseCommissionRate() {
assertEquals(
200.0,
SalesUtil.calculate(1000.0)
);
}
Step 4: Replace the assertion by the actual behavior
1
2
1
200
2
? ?
public class SalesUtil {
double BQ = 1000.0;
double BCR = 0.20;
double OQM1 = 1.5;
double OQM2 = OQM1 * 2;
double calculate(double tSales) {
if (tSales <= BQ) {
return tSales * BCR;
} else if (tSales <= BQ * 2) {
return (BQ) * BCR +
(tSales - BQ) * BCR * OQM1;
} else {
return (BQ) * BCR +
(tSales - BQ) * BCR*OQM1+
(tSales – BQ*2) * BCR*OQM2;
}
}
}
…
@Test
public void testCalculate () {
assertEquals(
0.0,
SalesUtil.calculate(2000.0)
);
}
Step 5: Repeat
1
2
1
200
2
? ?
Watch out for :
« While we’re at it, we might as well… » !
Work only on what you need to modify now.
We really don’t have the time
for this ?!?
How long does it take to understand
a piece of Legacy code before you
can change it (reasonably safely)?
• Specifies required
behavior
• Known behavior
and new code
• Permanent
• Specifies actual
behavior
• Legacy code, Lost or
forgotten behavior
• Temporary
Unit test Characterization test
How is a CT different?
Will end-to-end tests around my
application help me characterize
my code?
Refactoring is not a
rewrite on a small scale!
Why are
managers/customers/project
managers so afraid of refactoring?
Watch out for Big Bang refactoring !
46
A big expense
Big Bang
Very high risk
No new business value
Regular payments (debt)
Step by Step
Lower risk
Still produce value
Rewrite Renovate / Rejuvenate
Rewrite or refactoring ?!?
A strategy
> stabilize the patient
and focus on what is most
critical
As in the emergency room…
Let what you are
currently working on
guide your efforts
This talk was about understanding and securing your Legacy Code….
Now, you can learn how to mercilessly renovate your Legacy Code:
• Sprout Methods/Classes
• Instance Delegator
• Extract to Method
• …
Next… Refactoring techniques
Maintainability: a great
challenge for the future
Code rot
is avoidable
Although our first joy of programming may have
been intense, the misery of dealing with legacy code
is often sufficient to extinguish that flame.
Michael Feathers,
Working Effectively with Legacy Code
Legacy code can kill the flame!
Imagede http://beinweb.fr/wp-content/uploads/2014/04/boite-a-outils-entrepreneurs.jpg
Characterization testing…
Another tool to add to your toolbox!
« Legacy code » can be treated !
Thank
you.
Site
elapsetech.com
Twitter
@fbourbonnais
Email
fbourbonnais@elapsetech.com
pascalroy@elapsetech.com
LinkedIn
linkedin.com/in/fbourbonnais
ca.linkedin.com/in/roypa
conferences.elapsetech.com
All our conferences (french)
conferences.elapsetech.com
/legacy-tests-caracterisation
Slides and references (french)
Félix-Antoine Bourbonnais
Pascal Roy

More Related Content

Similar to Characterization Testing : regaining control of your Legacy Code [EN]

Meg bernal insight2014 4219
Meg bernal insight2014 4219Meg bernal insight2014 4219
Meg bernal insight2014 4219
Peter Schouboe
 
From clever code to better code
From clever code to better codeFrom clever code to better code
From clever code to better code
Dror Helper
 
Evolving a Clean, Pragmatic Architecture at JBCNConf 2019
Evolving a Clean, Pragmatic Architecture at JBCNConf 2019Evolving a Clean, Pragmatic Architecture at JBCNConf 2019
Evolving a Clean, Pragmatic Architecture at JBCNConf 2019
Victor Rentea
 
C:\Fakepath\Combating Software Entropy 2
C:\Fakepath\Combating Software Entropy 2C:\Fakepath\Combating Software Entropy 2
C:\Fakepath\Combating Software Entropy 2Hammad Rajjoub
 
C:\Fakepath\Combating Software Entropy 2
C:\Fakepath\Combating Software Entropy 2C:\Fakepath\Combating Software Entropy 2
C:\Fakepath\Combating Software Entropy 2
Hammad Rajjoub
 
20.1 Java working with abstraction
20.1 Java working with abstraction20.1 Java working with abstraction
20.1 Java working with abstraction
Intro C# Book
 
Mutation Testing with PIT
Mutation Testing with PITMutation Testing with PIT
Mutation Testing with PIT
Rafał Leszko
 
關於測試,我說的其實是......
關於測試,我說的其實是......關於測試,我說的其實是......
關於測試,我說的其實是......
hugo lu
 
Mutation testing with PIT
Mutation testing with PITMutation testing with PIT
Mutation testing with PIT
Rafał Leszko
 
2-Functions.pdf
2-Functions.pdf2-Functions.pdf
2-Functions.pdf
YekoyeTigabuYeko
 
How I Learned to Stop Worrying and Love Legacy Code
How I Learned to Stop Worrying and Love Legacy CodeHow I Learned to Stop Worrying and Love Legacy Code
How I Learned to Stop Worrying and Love Legacy Code
Gene Gotimer
 
Avoiding cursors with sql server 2005 tech republic
Avoiding cursors with sql server 2005   tech republicAvoiding cursors with sql server 2005   tech republic
Avoiding cursors with sql server 2005 tech republicKaing Menglieng
 
Navigating the xDD Alphabet Soup
Navigating the xDD Alphabet SoupNavigating the xDD Alphabet Soup
Navigating the xDD Alphabet Soup
Dror Helper
 
Presentation slides: "How to get 100% code coverage"
Presentation slides: "How to get 100% code coverage" Presentation slides: "How to get 100% code coverage"
Presentation slides: "How to get 100% code coverage"
Rapita Systems Ltd
 
Test driven development
Test driven developmentTest driven development
Test driven development
christoforosnalmpantis
 
Code Refactoring
Code RefactoringCode Refactoring
Code Refactoring
Milan Vukoje
 
Cis 115 Education Redefined-snaptutorial.com
Cis 115 Education Redefined-snaptutorial.comCis 115 Education Redefined-snaptutorial.com
Cis 115 Education Redefined-snaptutorial.com
robertledwes38
 
Bdd for-dso-1227123516572504-8
Bdd for-dso-1227123516572504-8Bdd for-dso-1227123516572504-8
Bdd for-dso-1227123516572504-8Frédéric Delorme
 

Similar to Characterization Testing : regaining control of your Legacy Code [EN] (20)

Meg bernal insight2014 4219
Meg bernal insight2014 4219Meg bernal insight2014 4219
Meg bernal insight2014 4219
 
From clever code to better code
From clever code to better codeFrom clever code to better code
From clever code to better code
 
Evolving a Clean, Pragmatic Architecture at JBCNConf 2019
Evolving a Clean, Pragmatic Architecture at JBCNConf 2019Evolving a Clean, Pragmatic Architecture at JBCNConf 2019
Evolving a Clean, Pragmatic Architecture at JBCNConf 2019
 
C:\Fakepath\Combating Software Entropy 2
C:\Fakepath\Combating Software Entropy 2C:\Fakepath\Combating Software Entropy 2
C:\Fakepath\Combating Software Entropy 2
 
C:\Fakepath\Combating Software Entropy 2
C:\Fakepath\Combating Software Entropy 2C:\Fakepath\Combating Software Entropy 2
C:\Fakepath\Combating Software Entropy 2
 
20.1 Java working with abstraction
20.1 Java working with abstraction20.1 Java working with abstraction
20.1 Java working with abstraction
 
Mutation Testing with PIT
Mutation Testing with PITMutation Testing with PIT
Mutation Testing with PIT
 
關於測試,我說的其實是......
關於測試,我說的其實是......關於測試,我說的其實是......
關於測試,我說的其實是......
 
Mutation testing with PIT
Mutation testing with PITMutation testing with PIT
Mutation testing with PIT
 
2-Functions.pdf
2-Functions.pdf2-Functions.pdf
2-Functions.pdf
 
How I Learned to Stop Worrying and Love Legacy Code
How I Learned to Stop Worrying and Love Legacy CodeHow I Learned to Stop Worrying and Love Legacy Code
How I Learned to Stop Worrying and Love Legacy Code
 
Avoiding cursors with sql server 2005 tech republic
Avoiding cursors with sql server 2005   tech republicAvoiding cursors with sql server 2005   tech republic
Avoiding cursors with sql server 2005 tech republic
 
Navigating the xDD Alphabet Soup
Navigating the xDD Alphabet SoupNavigating the xDD Alphabet Soup
Navigating the xDD Alphabet Soup
 
Presentation slides: "How to get 100% code coverage"
Presentation slides: "How to get 100% code coverage" Presentation slides: "How to get 100% code coverage"
Presentation slides: "How to get 100% code coverage"
 
Test driven development
Test driven developmentTest driven development
Test driven development
 
Write bulletproof trigger code
Write bulletproof trigger codeWrite bulletproof trigger code
Write bulletproof trigger code
 
Code Refactoring
Code RefactoringCode Refactoring
Code Refactoring
 
Cis 115 Education Redefined-snaptutorial.com
Cis 115 Education Redefined-snaptutorial.comCis 115 Education Redefined-snaptutorial.com
Cis 115 Education Redefined-snaptutorial.com
 
Bdd for-dso-1227123516572504-8
Bdd for-dso-1227123516572504-8Bdd for-dso-1227123516572504-8
Bdd for-dso-1227123516572504-8
 
Apex Unit Testing in the Real World
Apex Unit Testing in the Real WorldApex Unit Testing in the Real World
Apex Unit Testing in the Real World
 

Recently uploaded

FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdfFIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance
 
UiPath Community Day Dubai: AI at Work..
UiPath Community Day Dubai: AI at Work..UiPath Community Day Dubai: AI at Work..
UiPath Community Day Dubai: AI at Work..
UiPathCommunity
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
Alan Dix
 
Monitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR EventsMonitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR Events
Ana-Maria Mihalceanu
 
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdfSAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
Peter Spielvogel
 
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Albert Hoitingh
 
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptx
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptxSecstrike : Reverse Engineering & Pwnable tools for CTF.pptx
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptx
nkrafacyberclub
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
James Anderson
 
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 previewState of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
Prayukth K V
 
PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)
Ralf Eggert
 
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance
 
The Metaverse and AI: how can decision-makers harness the Metaverse for their...
The Metaverse and AI: how can decision-makers harness the Metaverse for their...The Metaverse and AI: how can decision-makers harness the Metaverse for their...
The Metaverse and AI: how can decision-makers harness the Metaverse for their...
Jen Stirrup
 
Pushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 daysPushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 days
Adtran
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance
 
UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4
DianaGray10
 
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
SOFTTECHHUB
 
Free Complete Python - A step towards Data Science
Free Complete Python - A step towards Data ScienceFree Complete Python - A step towards Data Science
Free Complete Python - A step towards Data Science
RinaMondal9
 
Generative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to ProductionGenerative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to Production
Aggregage
 
Quantum Computing: Current Landscape and the Future Role of APIs
Quantum Computing: Current Landscape and the Future Role of APIsQuantum Computing: Current Landscape and the Future Role of APIs
Quantum Computing: Current Landscape and the Future Role of APIs
Vlad Stirbu
 

Recently uploaded (20)

FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdfFIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
 
UiPath Community Day Dubai: AI at Work..
UiPath Community Day Dubai: AI at Work..UiPath Community Day Dubai: AI at Work..
UiPath Community Day Dubai: AI at Work..
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
 
Monitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR EventsMonitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR Events
 
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdfSAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
 
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
 
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptx
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptxSecstrike : Reverse Engineering & Pwnable tools for CTF.pptx
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptx
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
 
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 previewState of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
 
PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)
 
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
 
The Metaverse and AI: how can decision-makers harness the Metaverse for their...
The Metaverse and AI: how can decision-makers harness the Metaverse for their...The Metaverse and AI: how can decision-makers harness the Metaverse for their...
The Metaverse and AI: how can decision-makers harness the Metaverse for their...
 
Pushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 daysPushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 days
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
 
UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4
 
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
 
Free Complete Python - A step towards Data Science
Free Complete Python - A step towards Data ScienceFree Complete Python - A step towards Data Science
Free Complete Python - A step towards Data Science
 
Generative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to ProductionGenerative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to Production
 
Quantum Computing: Current Landscape and the Future Role of APIs
Quantum Computing: Current Landscape and the Future Role of APIsQuantum Computing: Current Landscape and the Future Role of APIs
Quantum Computing: Current Landscape and the Future Role of APIs
 

Characterization Testing : regaining control of your Legacy Code [EN]

  • 1. & FÉLIX-ANTOINE BOURBONNAIS B.ING., M.SC, PSM Version: Mai 2017 (DEV) Characterization Testing : regaining control of your Legacy Code PASCAL ROY ING., CSM, PSM, PMP
  • 2. Think how Legacy code affects your work in everyday life… Imagine a tool that helps you explore, understand and refactor it without fear. … a nice dream, isn’t?
  • 3.
  • 4. 4 Qui sommes-nous ? Pascal Roy Ing., PSM, CSM, PMP Félix-Antoine Bourbonnais B.ing., PSM, M.Sc.
  • 7.
  • 8. Am I the only one to have Legacy Code ?
  • 9. Code that is difficult to modify and evolve. Regardless how old it is or why it got this way. What is Legacy Code ?
  • 10. • Code written by others • Code nobody wants to touch anymore • Code no longer supported by the people who wrote it • Code that could be rewritten using better coding practices, tools or languages • ... Other popular definitions …
  • 11. Code without tests Michael Feathers, Working Effectively with Legacy Code
  • 12.
  • 13. Two basic strategies… What to do with my Legacy Code ?
  • 15. Tired of the continuous stress of delivery, of endless debugging sessions, of the fear of breaking something?
  • 16. Please, give me a new project !@/$%!/%
  • 17. The cycle of death… Legacy Code No tests No refactoringPatches
  • 18.
  • 22. Pick your next « Story » and begin paying out some of that debt!
  • 23.
  • 25.
  • 26. A characterization test describes the actual behavior of an existing piece of code. - Michael Feathers
  • 27. Overcome fear! To… Understand and document what the code does Protect against unintended changes when refactoring +
  • 28.
  • 29. 1. Find and isolate a piece of code that you need to analyze or modify 2. Write a test that uses that code, use an assertion you know will fail 3. Execute the test and let it tell you the actual behavior 4. Change the test assertion and the naming to match the actual behavior 5. Repeat… 5 steps for writing a characterization test
  • 30. public class SalesUtil { double BQ = 1000.0; double BCR = 0.20; double OQM1 = 1.5; double OQM2 = OQM1 * 2; public double calculate(double tSales) { if (tSales <= BQ) { return tSales * BCR; } else if (tSales <= BQ * 2) { return (BQ) * BCR + (tSales - BQ) * BCR * OQM1; } else { return (BQ) * BCR + (tSales - BQ) * BCR * OQM1 + (tSales - BQ * 2) * BCR * OQM2; } } } http://s3.amazonaws.com/giles/demons_010609/wtfm.jpg An example of Legacy Code? WTF? WTF? WTF?
  • 31. public class SalesUtil { double BQ = 1000.0; double BCR = 0.20; double OQM1 = 1.5; double OQM2 = OQM1 * 2; public double calculate(double tSales){ if (tSales <= BQ) { return tSales * BCR; } else if (tSales <= BQ * 2) { return (BQ) * BCR + (tSales - BQ) * BCR * OQM1; } else { return (BQ) * BCR + (tSales - BQ) * BCR * OQM1 + (tSales - BQ * 2) * BCR * OQM2; } } } @Test public void test… { assert(...) } Step 1: identify a piece of code to characterize 1 ? 2 ? ? 1 2
  • 32. public class SalesUtil { double BQ = 1000.0; double BCR = 0.20; double OQM1 = 1.5; double OQM2 = OQM1 * 2; double calculate(double tSales) { if (tSales <= BQ) { return tSales * BCR; } else if (tSales <= BQ * 2) { return (BQ) * BCR + (tSales - BQ) * BCR * OQM1; } else { return (BQ) * BCR + (tSales - BQ) * BCR * OQM1+ (tSales - BQ*2)*BCR * OQM2; } } } @Test public void testCalculate() { assertEquals( 0.0, SalesUtil.calculate(1000.0) ); } Step 2: write a failing assertion 1 ? 2 ? ? 1 2
  • 33. public class SalesUtil { double BQ = 1000.0; double BCR = 0.20; double OQM1 = 1.5; double OQM2 = OQM1 * 2; double calculate(double tSales) { if (tSales <= BQ) { return tSales * BCR; } else if (tSales <= BQ * 2) { return (BQ) * BCR + (tSales - BQ) * BCR * OQM1; } else { return (BQ) * BCR + (tSales - BQ) * BCR * OQM1 + (tSales - BQ * 2) * BCR * OQM2; } } } @Test public void testCalculate() { assertEquals( 0.0, SalesUtil.calculate(1000.0) ); } Step 3: execute the test + find out the actual behavior is > junit.framework.AssertionFailedError: expected:<0.0> but was:<200.0>1 ? 2 ? ?
  • 34. public class SalesUtil { double BQ = 1000.0; double BCR = 0.20; double OQM1 = 1.5; double OQM2 = OQM1 * 2; double calculate(double tSales) { if (tSales <= BQ) { return tSales * BCR; } else if (tSales <= BQ * 2) { return (BQ) * BCR + (tSales - BQ) * BCR * OQM1; } else { return (BQ) * BCR + (tSales - BQ) * BCR * OQM1+ (tSales – BQ*2)*BCR * OQM2; } } } @Test public void lessThanBaseQuota_useBaseCommissionRate() { assertEquals( 200.0, SalesUtil.calculate(1000.0) ); } Step 4: Replace the assertion by the actual behavior 1 2 1 200 2 ? ?
  • 35. public class SalesUtil { double BQ = 1000.0; double BCR = 0.20; double OQM1 = 1.5; double OQM2 = OQM1 * 2; double calculate(double tSales) { if (tSales <= BQ) { return tSales * BCR; } else if (tSales <= BQ * 2) { return (BQ) * BCR + (tSales - BQ) * BCR * OQM1; } else { return (BQ) * BCR + (tSales - BQ) * BCR*OQM1+ (tSales – BQ*2) * BCR*OQM2; } } } … @Test public void testCalculate () { assertEquals( 0.0, SalesUtil.calculate(2000.0) ); } Step 5: Repeat 1 2 1 200 2 ? ?
  • 36. Watch out for : « While we’re at it, we might as well… » ! Work only on what you need to modify now.
  • 37.
  • 38. We really don’t have the time for this ?!?
  • 39. How long does it take to understand a piece of Legacy code before you can change it (reasonably safely)?
  • 40.
  • 41. • Specifies required behavior • Known behavior and new code • Permanent • Specifies actual behavior • Legacy code, Lost or forgotten behavior • Temporary Unit test Characterization test How is a CT different?
  • 42. Will end-to-end tests around my application help me characterize my code?
  • 43.
  • 44. Refactoring is not a rewrite on a small scale!
  • 46. Watch out for Big Bang refactoring ! 46
  • 47. A big expense Big Bang Very high risk No new business value Regular payments (debt) Step by Step Lower risk Still produce value Rewrite Renovate / Rejuvenate Rewrite or refactoring ?!?
  • 48. A strategy > stabilize the patient and focus on what is most critical As in the emergency room…
  • 49. Let what you are currently working on guide your efforts
  • 50.
  • 51. This talk was about understanding and securing your Legacy Code…. Now, you can learn how to mercilessly renovate your Legacy Code: • Sprout Methods/Classes • Instance Delegator • Extract to Method • … Next… Refactoring techniques
  • 54. Although our first joy of programming may have been intense, the misery of dealing with legacy code is often sufficient to extinguish that flame. Michael Feathers, Working Effectively with Legacy Code Legacy code can kill the flame!
  • 56. « Legacy code » can be treated !