Refactoring legacy code driven by tests - ITA

Luca Minudel
Luca MinudelExperienced Lean-Agile Coach & Trainer. Agile practitioner since the very beginning of the agile movement. at SmHarter
Refactoring legacy code driven by tests 
Luca Minudel + Ilias Bartolini 
+ Luigi Bozzo 
I’m the 
Refactoring 
Chicken 
I’m the 
TDD egg
Let’s clarify the scope of this Workshop
Languages supported in this Workshop 
C# 
Java 
JavaScr 
ipt 
Ruby 
Pytho 
n 
Sala 
PHP 
Cpp
Automatic Testing Continuum 
Specification 
(documentation) 
Verification Design
Scope of this workshop 
Specification 
(documentation) 
Verification Design
Types of Automatic Tests 
End-to-end, out-of-process, business facing 
Unit, in-process, technology facing
Scope of this workshop 
End-to-end, out-of-process, business facing 
Unit, in-process, technology facing
Exercise 1: Tire Pressure Monitoring System 
Alarm class: 
controlla la pressione di un pneumatico e lancia un allarme se 
la pressione esce dall’intervallo di valori attesi.
Exercise 1: Tire Pressure Monitoring System 
Alarm class: 
controlla la pressione di un pneumatico e lancia un allarme se 
la pressione esce dall’intervallo di valori attesi. 
Sensor class: 
simula un vero sensore di pressione in un pneumatico, 
generando valori casuali ma realistici di pressione.
Exercise 1: Tire Pressure Monitoring System 
Scrivi gli unit test per la Alarm class. 
Fai il Refactoring del codice sino a rendere la Alarm class 
testabile.
Exercise 1: Tire Pressure Monitoring System 
Scrivi gli unit test per la Alarm class. 
Fai il Refactoring del codice sino a rendere la Alarm class 
testabile. 
Minimizza i cambiamenti alla API pubblica più che puoi.
Exercise 1: Tire Pressure Monitoring System 
Scrivi gli unit test per la Alarm class. 
Fai il Refactoring del codice sino a rendere la Alarm class 
testabile. 
Minimizza i cambiamenti alla API pubblica piu che puoi. 
Extra credits: 
Alarm class viola alcuni dei principi SOLID. Prendi nota della 
linea di codice e il principio violato.
The SOLID acronym 
S single responsibility principle 
O open closed principle 
L Liskov substitution principle 
I interface segregation principle 
D dependency inversion principle
Dependency Inversion Principle (DIP) 
Martin Fowler's definition: 
a) High level modules should not depend upon 
low level modules, both should depend upon 
abstractions. 
b) Abstractions should not depend upon details, 
details should depend upon abstractions.
Dependency Inversion Principle (DIP) 
Both low level classes and high level classes 
should depend on abstractions. 
High level classes should not depend on low 
level classes.
DIP Violation In Example Code 
High Level Class 
Dependency 
Low Level Class
Open Closed Principle (OCP) 
Bertrand Meyer's definition: 
Software entities (classes, modules, functions, 
etc.) should be open for extension, but closed for 
modification.
Open Closed Principle (OCP) 
Classes and methods should be 
open for extensions 
& 
strategically closed for modification. 
So that the behavior can be changed and 
extended adding new code instead of changing 
the class.
OCP Violation In Example Code 
Want to use a new type of sensor? 
Must modify code; cannot extend it
Dependency injection
Reference: WELC 
Parametrize Constructor 
Extract Interface
Exercise 2: Unicode File To Htm Text Converter 
UnicodeFileToHtmTextConverter class: 
trasforma del testo semplice in html per essere visualizzato da 
un browser.
Exercise 2: Unicode File To Htm Text Converter 
Scrivi gli unit test per la UnicodeFileToHtmTextConverter. 
Fai il Refactoring del codice sino a rendere la classe 
testabile.
Exercise 2: Unicode File To Htm Text Converter 
Scrivi gli unit test per la UnicodeFileToHtmTextConverter. 
Fai il Refactoring del codice sino a rendere la classe 
testabile. 
Minimizza i cambiamenti alla API pubblica più che puoi.
Exercise 2: Unicode File To Htm Text Converter 
Scrivi gli unit test per la UnicodeFileToHtmTextConverter. 
Fai il Refactoring del codice sino a rendere la classe 
testabile. 
Minimizza i cambiamenti alla API pubblica più che puoi. 
Extra credits: 
UnicodeFileToHtmTextConverter class viola alcuni dei principi 
SOLID. Prendi nota della linea di codice e il principio violato.
Feathers’ rules of thumb. Extended ! 
A test is not a unit test when: 
 It talks to the database 
 It communicates across the network 
 It touches the file system or reads config info 
 It uses DateTime.now() or Random 
 It depends on non-deterministic behavior 
 It can't run at the same time as any of your other unit 
tests 
 You have to do special things to your environment 
(such as editing config files) to run it.
Refactoring and TDD 
Should we inject this dependency?
Behavior of TextReader 
TextReader documentation from MSDN 
Non-idempotent behavior
Dependency injection and 
idempotent behavior
Reference: WELC 
Parametrize Constructor 
Extract Interface 
Skin and Wrap the API
Exercise 3: Ticket Dispenser 
TicketDispenser class: 
gestisce il sistema delle code agli sportelli di un negozio. 
Ci possono essere più distributori dei biglietti col numero del 
turno senza che questi distribuiscano lo stesso numero a due 
clienti diversi.
Exercise 3: Ticket Dispenser 
TurnTicket class: 
rappresenta il biglietto col numero del turno. 
TurnNumberSequence class: 
genera la sequenza dei numeri del turno.
Exercise 3: Ticket Dispenser 
Scrivi gli unit test per la TicketDispenser class. 
Fai il Refactoring del codice sino a rendere la classe 
TicketDispenser testabile.
Exercise 3: Ticket Dispenser 
Scrivi gli unit test per la TicketDispenser class. 
Fai il Refactoring del codice sino a rendere la classe 
TicketDispenser testabile. 
Minimizza i cambiamenti alla API pubblica più che puoi.
Exercise 3: Ticket Dispenser 
Scrivi gli unit test per la TicketDispenser class. 
Fai il Refactoring del codice sino a rendere la classe 
TicketDispenser testabile. 
Minimizza i cambiamenti alla API pubblica più che puoi. 
Extra credits: 
TicketDispenser class viola alcuni dei principi OO e SOLID. 
Prendi nota della linea di codice e il principio violato.
Encapsulation Violation In Example Code
DIP Violation In Example Code 
High Level Class 
Dependencies 
Low Level Classes
OCP Violation In Example Code 
Want to use a new type of sequence or ticket? 
Must modify code; cannot extend it
Dependency injection
Introduce Instance Delegator
Reference: WELC 
Parametrize Constructor 
Extract Interface 
Skin and Wrap the API 
Introduce Instance Delegator 
…
Exercise 4: Telemetry System 
TelemetryDiagnosticControl class: 
establishes a connection to the telemetry server through the 
TelemetryClient, 
sends a diagnostic request and receives the response with 
diagnostic info. 
TelemetryClient class: 
simulates the communication with the Telemetry Server, sends 
requests and then receives and returns the responses
Exercise 4: Telemetry System 
Write the unit tests for the TelemetryDiagnosticControl class. 
Refactor the code as much as you need to make the class 
testable.
Exercise 4: Telemetry System 
Write the unit tests for the TelemetryDiagnosticControl class. 
Refactor the code as much as you need to make the class 
testable. 
Minimize changes to the public API as much as you can.
Exercise 4: Telemetry System 
Write the unit tests for the TelemetryDiagnosticControl class. 
Refactor the code as much as you need to make the class 
testable. 
Minimize changes to the public API as much as you can. 
Extra credits: 
TelemetryClient class fails to follow one or more of the OO and 
SOLID principles. Write down the line number, the principle & 
the violation.
Single Responsibility Principle (SRP) 
A class should have only one reason to change.
Single Responsibility Principle (SRP) 
There should never be more than one reason for 
a class to change. 
A class should have one and only one 
responsibility.
Interface Segregation Principle (IRP) 
Clients should not be forced to depend upon 
interfaces that they do not use.
Interface Segregation Principle (IRP) 
Clients should not be forced to depend upon 
interface members that they don't use. 
Interfaces that serve only one scope should be 
preferred over fat interfaces.
Reference: SRP 
http://www.objectmentor.com/resources/articles/srp.pdf 
Pag. 151/152
Refactoring and TDD
Synergy between testing and design 
Michael Feathers: 
writing tests is another way to look the code and 
locally understand it and reuse it, 
and that is the same goal of good OO design. 
This is the reason for 
the deep synergy 
between testability and good design.
Other testing strategies for legacy code 
 Start with other tests 
 Use an automatic refactoring tool 
 Strangler pattern 
 DDD anti-corruption layer
Mike Cohn's Test Pyramid. Explained ! 
UI 
tests 
Integration 
tests 
Unit tests
More references
More references
More references
References 
 http://scratch.mit.edu/projects/13134082/ 
 http://vimeo.com/15007792 
 http://martinfowler.com/bliki/TestPyramid.html 
 http://martinfowler.com/bliki/StranglerApplication.html 
 http://www.markhneedham.com/blog/2009/07/07/domain-driven- 
design-anti-corruption-layer/ 
 http://www.objectmentor.com/resources/articles/srp.pdf 
 http://www.objectmentor.com/resources/articles/ocp.pdf 
 http://www.objectmentor.com/resources/articles/lsp.pdf 
 http://www.objectmentor.com/resources/articles/isp.pdf 
 http://www.objectmentor.com/resources/articles/dip.pdf
References / Links / Slides 
On Twitter 
On Twitter : 
@GGBOZZO 
@ILIASBARTOLINI 
@LUKADOTNET
1 of 59

Recommended

Agile Testing by
Agile Testing  Agile Testing
Agile Testing Intelliware Development Inc.
3.6K views57 slides
Teaching Kids Programming by
Teaching Kids ProgrammingTeaching Kids Programming
Teaching Kids ProgrammingLynn Langit
586 views25 slides
Evolution of Software Engineering in NCTR Projects by
Evolution of Software Engineering in NCTR  Projects   Evolution of Software Engineering in NCTR  Projects
Evolution of Software Engineering in NCTR Projects Mohammed Abbas
1.9K views25 slides
Code quality as a built-in process by
Code quality as a built-in processCode quality as a built-in process
Code quality as a built-in processElad Maimon
567 views51 slides
Test driven development by
Test driven developmentTest driven development
Test driven developmentSharafat Ibn Mollah Mosharraf
923 views30 slides
Test Driven Development by
Test Driven DevelopmentTest Driven Development
Test Driven DevelopmentOrbit One - We create coherence
959 views17 slides

More Related Content

What's hot

Test Driven Development by Denis Lutz by
Test Driven Development by Denis LutzTest Driven Development by Denis Lutz
Test Driven Development by Denis Lutzjazzman1980
1.4K views32 slides
Test driven development by
Test driven developmentTest driven development
Test driven developmentNascenia IT
637 views12 slides
Quality Jam: BDD, TDD and ATDD for the Enterprise by
Quality Jam: BDD, TDD and ATDD for the EnterpriseQuality Jam: BDD, TDD and ATDD for the Enterprise
Quality Jam: BDD, TDD and ATDD for the EnterpriseQASymphony
1.9K views42 slides
TDD vs. ATDD - What, Why, Which, When & Where by
TDD vs. ATDD - What, Why, Which, When & WhereTDD vs. ATDD - What, Why, Which, When & Where
TDD vs. ATDD - What, Why, Which, When & WhereDaniel Davis
3.4K views28 slides
Building Mobile (app) Masterpiece with Distributed Agile by
Building Mobile (app) Masterpiece with Distributed AgileBuilding Mobile (app) Masterpiece with Distributed Agile
Building Mobile (app) Masterpiece with Distributed AgileWee Witthawaskul
503 views33 slides
BDD presentation by
BDD presentationBDD presentation
BDD presentationtemebele
8.2K views26 slides

What's hot(20)

Test Driven Development by Denis Lutz by jazzman1980
Test Driven Development by Denis LutzTest Driven Development by Denis Lutz
Test Driven Development by Denis Lutz
jazzman19801.4K views
Test driven development by Nascenia IT
Test driven developmentTest driven development
Test driven development
Nascenia IT637 views
Quality Jam: BDD, TDD and ATDD for the Enterprise by QASymphony
Quality Jam: BDD, TDD and ATDD for the EnterpriseQuality Jam: BDD, TDD and ATDD for the Enterprise
Quality Jam: BDD, TDD and ATDD for the Enterprise
QASymphony 1.9K views
TDD vs. ATDD - What, Why, Which, When & Where by Daniel Davis
TDD vs. ATDD - What, Why, Which, When & WhereTDD vs. ATDD - What, Why, Which, When & Where
TDD vs. ATDD - What, Why, Which, When & Where
Daniel Davis3.4K views
Building Mobile (app) Masterpiece with Distributed Agile by Wee Witthawaskul
Building Mobile (app) Masterpiece with Distributed AgileBuilding Mobile (app) Masterpiece with Distributed Agile
Building Mobile (app) Masterpiece with Distributed Agile
Wee Witthawaskul503 views
BDD presentation by temebele
BDD presentationBDD presentation
BDD presentation
temebele8.2K views
Agile Testing by Naresh Jain
Agile TestingAgile Testing
Agile Testing
Naresh Jain15.8K views
Agile Test Driven Development by Viraf Karai
Agile Test Driven DevelopmentAgile Test Driven Development
Agile Test Driven Development
Viraf Karai15K views
Unit testing & TDD concepts with best practice guidelines. by Mohamed Taman
Unit testing & TDD concepts with best practice guidelines.Unit testing & TDD concepts with best practice guidelines.
Unit testing & TDD concepts with best practice guidelines.
Mohamed Taman5.9K views
Acceptance Test Driven Development by Mike Douglas
Acceptance Test Driven DevelopmentAcceptance Test Driven Development
Acceptance Test Driven Development
Mike Douglas2.6K views
Behavior Driven Development (BDD) by Ajay Danait
Behavior Driven Development (BDD)Behavior Driven Development (BDD)
Behavior Driven Development (BDD)
Ajay Danait4.6K views
xUnit and TDD: Why and How in Enterprise Software, August 2012 by Justin Gordon
xUnit and TDD: Why and How in Enterprise Software, August 2012xUnit and TDD: Why and How in Enterprise Software, August 2012
xUnit and TDD: Why and How in Enterprise Software, August 2012
Justin Gordon2.7K views
Test Driven Development by guestc8093a6
Test Driven DevelopmentTest Driven Development
Test Driven Development
guestc8093a65.2K views
Agile and ATDD the perfect couple by Stephen Tucker
Agile and ATDD the perfect coupleAgile and ATDD the perfect couple
Agile and ATDD the perfect couple
Stephen Tucker1.8K views
Scrum + Behavior Driven Development (BDD) - Colombo by Naveen Kumar Singh
Scrum + Behavior Driven Development (BDD) - ColomboScrum + Behavior Driven Development (BDD) - Colombo
Scrum + Behavior Driven Development (BDD) - Colombo
Behavior Driven Development Pros and Cons by Iosif Itkin
Behavior Driven Development Pros and ConsBehavior Driven Development Pros and Cons
Behavior Driven Development Pros and Cons
Iosif Itkin25.3K views

Viewers also liked

Add 2009 10 by
Add 2009 10Add 2009 10
Add 2009 10RUAULT
2.8K views68 slides
Why Nortel Went Bankrupt by
Why Nortel Went BankruptWhy Nortel Went Bankrupt
Why Nortel Went BankruptChris Sandström
11K views111 slides
Hong kong by
Hong kongHong kong
Hong kongSonya Fay McKenzie
2.3K views2 slides
City of deception by
City of deceptionCity of deception
City of deceptionDr Saim Ali soomro
1.9K views3 slides
I2 Argentina Unitech by
I2 Argentina UnitechI2 Argentina Unitech
I2 Argentina UnitechUNITECH S.A.
5K views37 slides
Environmental Law for Road Builders by
Environmental Law for Road Builders Environmental Law for Road Builders
Environmental Law for Road Builders DSaxe
2.8K views59 slides

Viewers also liked(20)

Add 2009 10 by RUAULT
Add 2009 10Add 2009 10
Add 2009 10
RUAULT2.8K views
Environmental Law for Road Builders by DSaxe
Environmental Law for Road Builders Environmental Law for Road Builders
Environmental Law for Road Builders
DSaxe2.8K views
Quelle gouvernance pour le numérique? by Antoine Vigneron
Quelle gouvernance pour le numérique?Quelle gouvernance pour le numérique?
Quelle gouvernance pour le numérique?
Antoine Vigneron3.9K views
Clinical development, contract & outsourcing in mena & asia pac webinar-l aju... by Larry Ajuwon
Clinical development, contract & outsourcing in mena & asia pac webinar-l aju...Clinical development, contract & outsourcing in mena & asia pac webinar-l aju...
Clinical development, contract & outsourcing in mena & asia pac webinar-l aju...
Larry Ajuwon4K views
Coding is the new literacy to make a difference in the world by mcd_boulanger
Coding is the new literacy to make a difference in the worldCoding is the new literacy to make a difference in the world
Coding is the new literacy to make a difference in the world
mcd_boulanger3.3K views
5 mythes de la marche au ralenti by ellipsos inc.
5 mythes de la marche au ralenti5 mythes de la marche au ralenti
5 mythes de la marche au ralenti
ellipsos inc.2.6K views
U.S. Economic Sanctions Update by Jon Yormick
U.S. Economic Sanctions UpdateU.S. Economic Sanctions Update
U.S. Economic Sanctions Update
Jon Yormick4.7K views
Power Pointless How To Make An Amazing Presentation by Joan Shi
Power Pointless How To Make An Amazing PresentationPower Pointless How To Make An Amazing Presentation
Power Pointless How To Make An Amazing Presentation
Joan Shi2.9K views

Similar to Refactoring legacy code driven by tests - ITA

Refactoring legacy code driven by tests - ENG by
Refactoring legacy code driven by tests - ENGRefactoring legacy code driven by tests - ENG
Refactoring legacy code driven by tests - ENGLuca Minudel
2.5K views52 slides
Templates and Exception Handling in C++ by
Templates and Exception Handling in C++Templates and Exception Handling in C++
Templates and Exception Handling in C++Nimrita Koul
149 views68 slides
TDD And Refactoring by
TDD And RefactoringTDD And Refactoring
TDD And RefactoringNaresh Jain
7.4K views34 slides
C++ question and answers by
C++ question and answersC++ question and answers
C++ question and answersAdenKheire
124 views12 slides
Testing the untestable by
Testing the untestableTesting the untestable
Testing the untestableRoyKlein
990 views32 slides
Object Oriented Concepts and Principles by
Object Oriented Concepts and PrinciplesObject Oriented Concepts and Principles
Object Oriented Concepts and Principlesdeonpmeyer
7.9K views68 slides

Similar to Refactoring legacy code driven by tests - ITA(20)

Refactoring legacy code driven by tests - ENG by Luca Minudel
Refactoring legacy code driven by tests - ENGRefactoring legacy code driven by tests - ENG
Refactoring legacy code driven by tests - ENG
Luca Minudel2.5K views
Templates and Exception Handling in C++ by Nimrita Koul
Templates and Exception Handling in C++Templates and Exception Handling in C++
Templates and Exception Handling in C++
Nimrita Koul149 views
TDD And Refactoring by Naresh Jain
TDD And RefactoringTDD And Refactoring
TDD And Refactoring
Naresh Jain7.4K views
C++ question and answers by AdenKheire
C++ question and answersC++ question and answers
C++ question and answers
AdenKheire124 views
Testing the untestable by RoyKlein
Testing the untestableTesting the untestable
Testing the untestable
RoyKlein990 views
Object Oriented Concepts and Principles by deonpmeyer
Object Oriented Concepts and PrinciplesObject Oriented Concepts and Principles
Object Oriented Concepts and Principles
deonpmeyer7.9K views
C#3.0 & Vb 9.0 New Features by techfreak
C#3.0 & Vb 9.0 New FeaturesC#3.0 & Vb 9.0 New Features
C#3.0 & Vb 9.0 New Features
techfreak715 views
Test Driven iOS Development (TDD) by Babul Mirdha
Test Driven iOS Development (TDD)Test Driven iOS Development (TDD)
Test Driven iOS Development (TDD)
Babul Mirdha5.8K views
Working Effectively With Legacy Perl Code by erikmsp
Working Effectively With Legacy Perl CodeWorking Effectively With Legacy Perl Code
Working Effectively With Legacy Perl Code
erikmsp1K views
Assessing Unit Test Quality by guest268ee8
Assessing Unit Test QualityAssessing Unit Test Quality
Assessing Unit Test Quality
guest268ee82.2K views
Technical trainning.pptx by SanuSan3
Technical trainning.pptxTechnical trainning.pptx
Technical trainning.pptx
SanuSan32 views
Python for Machine Learning by Student
Python for Machine LearningPython for Machine Learning
Python for Machine Learning
Student143 views
C:\Fakepath\Combating Software Entropy 2 by Hammad Rajjoub
C:\Fakepath\Combating Software Entropy 2C:\Fakepath\Combating Software Entropy 2
C:\Fakepath\Combating Software Entropy 2
Hammad Rajjoub381 views
C:\Fakepath\Combating Software Entropy 2 by Hammad Rajjoub
C:\Fakepath\Combating Software Entropy 2C:\Fakepath\Combating Software Entropy 2
C:\Fakepath\Combating Software Entropy 2
Hammad Rajjoub1K views
RPG Program for Unit Testing RPG by Greg.Helton
RPG Program for Unit Testing RPG RPG Program for Unit Testing RPG
RPG Program for Unit Testing RPG
Greg.Helton2.8K views

More from Luca Minudel

It takes two to tango - why tech and business succeed or fail together v4.1 b... by
It takes two to tango - why tech and business succeed or fail together v4.1 b...It takes two to tango - why tech and business succeed or fail together v4.1 b...
It takes two to tango - why tech and business succeed or fail together v4.1 b...Luca Minudel
527 views59 slides
Scrum master self-assessment kit v3.2 by
Scrum master self-assessment kit v3.2Scrum master self-assessment kit v3.2
Scrum master self-assessment kit v3.2Luca Minudel
1K views20 slides
Project management in the age of accelerating change - IT/Tech specific by
Project management in the age of accelerating change - IT/Tech specificProject management in the age of accelerating change - IT/Tech specific
Project management in the age of accelerating change - IT/Tech specificLuca Minudel
400 views78 slides
Project management in the age of accelerating change - general non IT specific by
Project management in the age of accelerating change - general non IT specificProject management in the age of accelerating change - general non IT specific
Project management in the age of accelerating change - general non IT specificLuca Minudel
322 views78 slides
Scrum master self assessment v2.7 by
Scrum master self assessment v2.7Scrum master self assessment v2.7
Scrum master self assessment v2.7Luca Minudel
4.2K views10 slides
Agility - definition and curricula by
Agility - definition and curriculaAgility - definition and curricula
Agility - definition and curriculaLuca Minudel
1.3K views27 slides

More from Luca Minudel(20)

It takes two to tango - why tech and business succeed or fail together v4.1 b... by Luca Minudel
It takes two to tango - why tech and business succeed or fail together v4.1 b...It takes two to tango - why tech and business succeed or fail together v4.1 b...
It takes two to tango - why tech and business succeed or fail together v4.1 b...
Luca Minudel527 views
Scrum master self-assessment kit v3.2 by Luca Minudel
Scrum master self-assessment kit v3.2Scrum master self-assessment kit v3.2
Scrum master self-assessment kit v3.2
Luca Minudel1K views
Project management in the age of accelerating change - IT/Tech specific by Luca Minudel
Project management in the age of accelerating change - IT/Tech specificProject management in the age of accelerating change - IT/Tech specific
Project management in the age of accelerating change - IT/Tech specific
Luca Minudel400 views
Project management in the age of accelerating change - general non IT specific by Luca Minudel
Project management in the age of accelerating change - general non IT specificProject management in the age of accelerating change - general non IT specific
Project management in the age of accelerating change - general non IT specific
Luca Minudel322 views
Scrum master self assessment v2.7 by Luca Minudel
Scrum master self assessment v2.7Scrum master self assessment v2.7
Scrum master self assessment v2.7
Luca Minudel4.2K views
Agility - definition and curricula by Luca Minudel
Agility - definition and curriculaAgility - definition and curricula
Agility - definition and curricula
Luca Minudel1.3K views
Agile Delivery Manager self-assessment radar by Luca Minudel
Agile Delivery Manager self-assessment radarAgile Delivery Manager self-assessment radar
Agile Delivery Manager self-assessment radar
Luca Minudel1.3K views
CTO self-assessment radar by Luca Minudel
CTO self-assessment radarCTO self-assessment radar
CTO self-assessment radar
Luca Minudel2.2K views
Reflections on Kent Beck's 3x Explore, Expand, and Extract by Luca Minudel
Reflections on Kent Beck's 3x Explore, Expand, and ExtractReflections on Kent Beck's 3x Explore, Expand, and Extract
Reflections on Kent Beck's 3x Explore, Expand, and Extract
Luca Minudel583 views
New Lean-Agile Coach Self-Assessment - detailed descriptions v3 by Luca Minudel
New Lean-Agile Coach Self-Assessment - detailed descriptions v3New Lean-Agile Coach Self-Assessment - detailed descriptions v3
New Lean-Agile Coach Self-Assessment - detailed descriptions v3
Luca Minudel5.6K views
From Continuous Integration to Continuous Delivery and DevOps by Luca Minudel
From Continuous Integration to Continuous Delivery and DevOpsFrom Continuous Integration to Continuous Delivery and DevOps
From Continuous Integration to Continuous Delivery and DevOps
Luca Minudel2.9K views
Draft your next training course with ideas from Training from the Back of the... by Luca Minudel
Draft your next training course with ideas from Training from the Back of the...Draft your next training course with ideas from Training from the Back of the...
Draft your next training course with ideas from Training from the Back of the...
Luca Minudel1.2K views
New Lean-Agile Coach self-assessment - levels description v3.2 by Luca Minudel
New Lean-Agile Coach self-assessment - levels description v3.2New Lean-Agile Coach self-assessment - levels description v3.2
New Lean-Agile Coach self-assessment - levels description v3.2
Luca Minudel15.6K views
Pratica avanzata del refactoring (2004) by Luca Minudel
Pratica avanzata del refactoring (2004)Pratica avanzata del refactoring (2004)
Pratica avanzata del refactoring (2004)
Luca Minudel339 views
New Lean-Agile Coach self-assessment radars v3.2 by Luca Minudel
New Lean-Agile Coach self-assessment radars v3.2New Lean-Agile Coach self-assessment radars v3.2
New Lean-Agile Coach self-assessment radars v3.2
Luca Minudel20.9K views
AgileDay 2006 - Essere agili nel diventare agili by Luca Minudel
AgileDay 2006 - Essere agili nel diventare agiliAgileDay 2006 - Essere agili nel diventare agili
AgileDay 2006 - Essere agili nel diventare agili
Luca Minudel396 views
Architettura del software un approccio Agile, Web-cast Microsoft 2006 by Luca Minudel
Architettura del software un approccio Agile, Web-cast Microsoft 2006Architettura del software un approccio Agile, Web-cast Microsoft 2006
Architettura del software un approccio Agile, Web-cast Microsoft 2006
Luca Minudel1.1K views
Agility: The scientific definition of how to be(come) Agile by Luca Minudel
Agility: The scientific definition of how to be(come) AgileAgility: The scientific definition of how to be(come) Agile
Agility: The scientific definition of how to be(come) Agile
Luca Minudel783 views
Lightning talk: Active Agility, the magic ingredient of Lean and Agile by Luca Minudel
Lightning talk: Active Agility, the magic ingredient of Lean and AgileLightning talk: Active Agility, the magic ingredient of Lean and Agile
Lightning talk: Active Agility, the magic ingredient of Lean and Agile
Luca Minudel567 views
Software development in Formula One: challenges, complexity and struggle for ... by Luca Minudel
Software development in Formula One: challenges, complexity and struggle for ...Software development in Formula One: challenges, complexity and struggle for ...
Software development in Formula One: challenges, complexity and struggle for ...
Luca Minudel1.4K views

Recently uploaded

SAP FOR TYRE INDUSTRY.pdf by
SAP FOR TYRE INDUSTRY.pdfSAP FOR TYRE INDUSTRY.pdf
SAP FOR TYRE INDUSTRY.pdfVirendra Rai, PMP
23 views3 slides
DSD-INT 2023 Next-Generation Flood Inundation Mapping for Taiwan - Delft3D FM... by
DSD-INT 2023 Next-Generation Flood Inundation Mapping for Taiwan - Delft3D FM...DSD-INT 2023 Next-Generation Flood Inundation Mapping for Taiwan - Delft3D FM...
DSD-INT 2023 Next-Generation Flood Inundation Mapping for Taiwan - Delft3D FM...Deltares
7 views40 slides
A first look at MariaDB 11.x features and ideas on how to use them by
A first look at MariaDB 11.x features and ideas on how to use themA first look at MariaDB 11.x features and ideas on how to use them
A first look at MariaDB 11.x features and ideas on how to use themFederico Razzoli
45 views36 slides
DSD-INT 2023 Simulation of Coastal Hydrodynamics and Water Quality in Hong Ko... by
DSD-INT 2023 Simulation of Coastal Hydrodynamics and Water Quality in Hong Ko...DSD-INT 2023 Simulation of Coastal Hydrodynamics and Water Quality in Hong Ko...
DSD-INT 2023 Simulation of Coastal Hydrodynamics and Water Quality in Hong Ko...Deltares
11 views23 slides
DevsRank by
DevsRankDevsRank
DevsRankdevsrank786
11 views1 slide
DSD-INT 2023 Delft3D FM Suite 2024.01 2D3D - New features + Improvements - Ge... by
DSD-INT 2023 Delft3D FM Suite 2024.01 2D3D - New features + Improvements - Ge...DSD-INT 2023 Delft3D FM Suite 2024.01 2D3D - New features + Improvements - Ge...
DSD-INT 2023 Delft3D FM Suite 2024.01 2D3D - New features + Improvements - Ge...Deltares
16 views12 slides

Recently uploaded(20)

DSD-INT 2023 Next-Generation Flood Inundation Mapping for Taiwan - Delft3D FM... by Deltares
DSD-INT 2023 Next-Generation Flood Inundation Mapping for Taiwan - Delft3D FM...DSD-INT 2023 Next-Generation Flood Inundation Mapping for Taiwan - Delft3D FM...
DSD-INT 2023 Next-Generation Flood Inundation Mapping for Taiwan - Delft3D FM...
Deltares7 views
A first look at MariaDB 11.x features and ideas on how to use them by Federico Razzoli
A first look at MariaDB 11.x features and ideas on how to use themA first look at MariaDB 11.x features and ideas on how to use them
A first look at MariaDB 11.x features and ideas on how to use them
Federico Razzoli45 views
DSD-INT 2023 Simulation of Coastal Hydrodynamics and Water Quality in Hong Ko... by Deltares
DSD-INT 2023 Simulation of Coastal Hydrodynamics and Water Quality in Hong Ko...DSD-INT 2023 Simulation of Coastal Hydrodynamics and Water Quality in Hong Ko...
DSD-INT 2023 Simulation of Coastal Hydrodynamics and Water Quality in Hong Ko...
Deltares11 views
DSD-INT 2023 Delft3D FM Suite 2024.01 2D3D - New features + Improvements - Ge... by Deltares
DSD-INT 2023 Delft3D FM Suite 2024.01 2D3D - New features + Improvements - Ge...DSD-INT 2023 Delft3D FM Suite 2024.01 2D3D - New features + Improvements - Ge...
DSD-INT 2023 Delft3D FM Suite 2024.01 2D3D - New features + Improvements - Ge...
Deltares16 views
DSD-INT 2023 Modelling litter in the Yarra and Maribyrnong Rivers (Australia)... by Deltares
DSD-INT 2023 Modelling litter in the Yarra and Maribyrnong Rivers (Australia)...DSD-INT 2023 Modelling litter in the Yarra and Maribyrnong Rivers (Australia)...
DSD-INT 2023 Modelling litter in the Yarra and Maribyrnong Rivers (Australia)...
Deltares9 views
Elevate your SAP landscape's efficiency and performance with HCL Workload Aut... by HCLSoftware
Elevate your SAP landscape's efficiency and performance with HCL Workload Aut...Elevate your SAP landscape's efficiency and performance with HCL Workload Aut...
Elevate your SAP landscape's efficiency and performance with HCL Workload Aut...
HCLSoftware6 views
DSD-INT 2023 Delft3D FM Suite 2024.01 1D2D - Beta testing programme - Geertsema by Deltares
DSD-INT 2023 Delft3D FM Suite 2024.01 1D2D - Beta testing programme - GeertsemaDSD-INT 2023 Delft3D FM Suite 2024.01 1D2D - Beta testing programme - Geertsema
DSD-INT 2023 Delft3D FM Suite 2024.01 1D2D - Beta testing programme - Geertsema
Deltares17 views
Navigating container technology for enhanced security by Niklas Saari by Metosin Oy
Navigating container technology for enhanced security by Niklas SaariNavigating container technology for enhanced security by Niklas Saari
Navigating container technology for enhanced security by Niklas Saari
Metosin Oy8 views
Roadmap y Novedades de producto by Neo4j
Roadmap y Novedades de productoRoadmap y Novedades de producto
Roadmap y Novedades de producto
Neo4j50 views
Citi TechTalk Session 2: Kafka Deep Dive by confluent
Citi TechTalk Session 2: Kafka Deep DiveCiti TechTalk Session 2: Kafka Deep Dive
Citi TechTalk Session 2: Kafka Deep Dive
confluent17 views
What Can Employee Monitoring Software Do?​ by wAnywhere
What Can Employee Monitoring Software Do?​What Can Employee Monitoring Software Do?​
What Can Employee Monitoring Software Do?​
wAnywhere21 views
Neo4j y GenAI by Neo4j
Neo4j y GenAI Neo4j y GenAI
Neo4j y GenAI
Neo4j42 views
360 graden fabriek by info33492
360 graden fabriek360 graden fabriek
360 graden fabriek
info3349224 views

Refactoring legacy code driven by tests - ITA

  • 1. Refactoring legacy code driven by tests Luca Minudel + Ilias Bartolini + Luigi Bozzo I’m the Refactoring Chicken I’m the TDD egg
  • 2. Let’s clarify the scope of this Workshop
  • 3. Languages supported in this Workshop C# Java JavaScr ipt Ruby Pytho n Sala PHP Cpp
  • 4. Automatic Testing Continuum Specification (documentation) Verification Design
  • 5. Scope of this workshop Specification (documentation) Verification Design
  • 6. Types of Automatic Tests End-to-end, out-of-process, business facing Unit, in-process, technology facing
  • 7. Scope of this workshop End-to-end, out-of-process, business facing Unit, in-process, technology facing
  • 8. Exercise 1: Tire Pressure Monitoring System Alarm class: controlla la pressione di un pneumatico e lancia un allarme se la pressione esce dall’intervallo di valori attesi.
  • 9. Exercise 1: Tire Pressure Monitoring System Alarm class: controlla la pressione di un pneumatico e lancia un allarme se la pressione esce dall’intervallo di valori attesi. Sensor class: simula un vero sensore di pressione in un pneumatico, generando valori casuali ma realistici di pressione.
  • 10. Exercise 1: Tire Pressure Monitoring System Scrivi gli unit test per la Alarm class. Fai il Refactoring del codice sino a rendere la Alarm class testabile.
  • 11. Exercise 1: Tire Pressure Monitoring System Scrivi gli unit test per la Alarm class. Fai il Refactoring del codice sino a rendere la Alarm class testabile. Minimizza i cambiamenti alla API pubblica più che puoi.
  • 12. Exercise 1: Tire Pressure Monitoring System Scrivi gli unit test per la Alarm class. Fai il Refactoring del codice sino a rendere la Alarm class testabile. Minimizza i cambiamenti alla API pubblica piu che puoi. Extra credits: Alarm class viola alcuni dei principi SOLID. Prendi nota della linea di codice e il principio violato.
  • 13. The SOLID acronym S single responsibility principle O open closed principle L Liskov substitution principle I interface segregation principle D dependency inversion principle
  • 14. Dependency Inversion Principle (DIP) Martin Fowler's definition: a) High level modules should not depend upon low level modules, both should depend upon abstractions. b) Abstractions should not depend upon details, details should depend upon abstractions.
  • 15. Dependency Inversion Principle (DIP) Both low level classes and high level classes should depend on abstractions. High level classes should not depend on low level classes.
  • 16. DIP Violation In Example Code High Level Class Dependency Low Level Class
  • 17. Open Closed Principle (OCP) Bertrand Meyer's definition: Software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification.
  • 18. Open Closed Principle (OCP) Classes and methods should be open for extensions & strategically closed for modification. So that the behavior can be changed and extended adding new code instead of changing the class.
  • 19. OCP Violation In Example Code Want to use a new type of sensor? Must modify code; cannot extend it
  • 21. Reference: WELC Parametrize Constructor Extract Interface
  • 22. Exercise 2: Unicode File To Htm Text Converter UnicodeFileToHtmTextConverter class: trasforma del testo semplice in html per essere visualizzato da un browser.
  • 23. Exercise 2: Unicode File To Htm Text Converter Scrivi gli unit test per la UnicodeFileToHtmTextConverter. Fai il Refactoring del codice sino a rendere la classe testabile.
  • 24. Exercise 2: Unicode File To Htm Text Converter Scrivi gli unit test per la UnicodeFileToHtmTextConverter. Fai il Refactoring del codice sino a rendere la classe testabile. Minimizza i cambiamenti alla API pubblica più che puoi.
  • 25. Exercise 2: Unicode File To Htm Text Converter Scrivi gli unit test per la UnicodeFileToHtmTextConverter. Fai il Refactoring del codice sino a rendere la classe testabile. Minimizza i cambiamenti alla API pubblica più che puoi. Extra credits: UnicodeFileToHtmTextConverter class viola alcuni dei principi SOLID. Prendi nota della linea di codice e il principio violato.
  • 26. Feathers’ rules of thumb. Extended ! A test is not a unit test when:  It talks to the database  It communicates across the network  It touches the file system or reads config info  It uses DateTime.now() or Random  It depends on non-deterministic behavior  It can't run at the same time as any of your other unit tests  You have to do special things to your environment (such as editing config files) to run it.
  • 27. Refactoring and TDD Should we inject this dependency?
  • 28. Behavior of TextReader TextReader documentation from MSDN Non-idempotent behavior
  • 29. Dependency injection and idempotent behavior
  • 30. Reference: WELC Parametrize Constructor Extract Interface Skin and Wrap the API
  • 31. Exercise 3: Ticket Dispenser TicketDispenser class: gestisce il sistema delle code agli sportelli di un negozio. Ci possono essere più distributori dei biglietti col numero del turno senza che questi distribuiscano lo stesso numero a due clienti diversi.
  • 32. Exercise 3: Ticket Dispenser TurnTicket class: rappresenta il biglietto col numero del turno. TurnNumberSequence class: genera la sequenza dei numeri del turno.
  • 33. Exercise 3: Ticket Dispenser Scrivi gli unit test per la TicketDispenser class. Fai il Refactoring del codice sino a rendere la classe TicketDispenser testabile.
  • 34. Exercise 3: Ticket Dispenser Scrivi gli unit test per la TicketDispenser class. Fai il Refactoring del codice sino a rendere la classe TicketDispenser testabile. Minimizza i cambiamenti alla API pubblica più che puoi.
  • 35. Exercise 3: Ticket Dispenser Scrivi gli unit test per la TicketDispenser class. Fai il Refactoring del codice sino a rendere la classe TicketDispenser testabile. Minimizza i cambiamenti alla API pubblica più che puoi. Extra credits: TicketDispenser class viola alcuni dei principi OO e SOLID. Prendi nota della linea di codice e il principio violato.
  • 37. DIP Violation In Example Code High Level Class Dependencies Low Level Classes
  • 38. OCP Violation In Example Code Want to use a new type of sequence or ticket? Must modify code; cannot extend it
  • 41. Reference: WELC Parametrize Constructor Extract Interface Skin and Wrap the API Introduce Instance Delegator …
  • 42. Exercise 4: Telemetry System TelemetryDiagnosticControl class: establishes a connection to the telemetry server through the TelemetryClient, sends a diagnostic request and receives the response with diagnostic info. TelemetryClient class: simulates the communication with the Telemetry Server, sends requests and then receives and returns the responses
  • 43. Exercise 4: Telemetry System Write the unit tests for the TelemetryDiagnosticControl class. Refactor the code as much as you need to make the class testable.
  • 44. Exercise 4: Telemetry System Write the unit tests for the TelemetryDiagnosticControl class. Refactor the code as much as you need to make the class testable. Minimize changes to the public API as much as you can.
  • 45. Exercise 4: Telemetry System Write the unit tests for the TelemetryDiagnosticControl class. Refactor the code as much as you need to make the class testable. Minimize changes to the public API as much as you can. Extra credits: TelemetryClient class fails to follow one or more of the OO and SOLID principles. Write down the line number, the principle & the violation.
  • 46. Single Responsibility Principle (SRP) A class should have only one reason to change.
  • 47. Single Responsibility Principle (SRP) There should never be more than one reason for a class to change. A class should have one and only one responsibility.
  • 48. Interface Segregation Principle (IRP) Clients should not be forced to depend upon interfaces that they do not use.
  • 49. Interface Segregation Principle (IRP) Clients should not be forced to depend upon interface members that they don't use. Interfaces that serve only one scope should be preferred over fat interfaces.
  • 52. Synergy between testing and design Michael Feathers: writing tests is another way to look the code and locally understand it and reuse it, and that is the same goal of good OO design. This is the reason for the deep synergy between testability and good design.
  • 53. Other testing strategies for legacy code  Start with other tests  Use an automatic refactoring tool  Strangler pattern  DDD anti-corruption layer
  • 54. Mike Cohn's Test Pyramid. Explained ! UI tests Integration tests Unit tests
  • 58. References  http://scratch.mit.edu/projects/13134082/  http://vimeo.com/15007792  http://martinfowler.com/bliki/TestPyramid.html  http://martinfowler.com/bliki/StranglerApplication.html  http://www.markhneedham.com/blog/2009/07/07/domain-driven- design-anti-corruption-layer/  http://www.objectmentor.com/resources/articles/srp.pdf  http://www.objectmentor.com/resources/articles/ocp.pdf  http://www.objectmentor.com/resources/articles/lsp.pdf  http://www.objectmentor.com/resources/articles/isp.pdf  http://www.objectmentor.com/resources/articles/dip.pdf
  • 59. References / Links / Slides On Twitter On Twitter : @GGBOZZO @ILIASBARTOLINI @LUKADOTNET

Editor's Notes

  1. The triangle in action: http://scratch.mit.edu/projects/13134082/
  2. They could be code you inherited from a legacy code-base.
  3. They could be code you inherited from a legacy code-base.
  4. They could be code you inherited from a legacy code-base.
  5. They could be code you inherited from a legacy code-base.
  6. They could be code you inherited from a legacy code-base.
  7. Michael Feathers, NDC 2010 The Deep Synergy Between Testability and Good Design http://vimeo.com/15007792 http://michaelfeathers.typepad.com/michael_feathers_blog/2007/09/the-deep-synerg.html
  8. http://martinfowler.com/bliki/TestPyramid.html http://martinfowler.com/bliki/StranglerApplication.html http://www.markhneedham.com/blog/2009/07/07/domain-driven-design-anti-corruption-layer/
  9. http://martinfowler.com/bliki/TestPyramid.html http://martinfowler.com/bliki/StranglerApplication.html http://www.markhneedham.com/blog/2009/07/07/domain-driven-design-anti-corruption-layer/
  10. Magic Tricks of Testing , Sendi Metz, https://speakerdeck.com/skmetz/magic-tricks-of-testing-railsconf