SlideShare une entreprise Scribd logo
1  sur  20
Refactoring for Testability
Techniken für Post-Factum
Abdeckug mit Tests
Rusi Filipov, 2014
Softwerkskammer Karlsruhe
Agenda
●
Legacy Code und das Refactoring Dilemma
●
Coding Dojo mit Refactoring-Aufgaben
●
Typische Situationen analysieren
●
Typische Refactorings anwenden
●
Ziel: Gute Unit Tests in Legacy Code
Rusi Filipov Softwerkskammer Karlsruhe 2014
The First Step in Refactoring
Whenever I do refactoring, the first step is
always the same. I need to build a solid set of
tests for that section of code. The tests are
essential because even though I follow
refactorings structured to avoid most of the
opportunities for introducing bugs, I'm still
human and still make mistakes. Thus I need
solid tests.
Martin Fowler, 1999
Rusi Filipov Softwerkskammer Karlsruhe 2014
The First Step in Refactoring
As we do the refactoring, we will lean on the
tests. I'm going to be relying on the tests to tell
me whether I introduce a bug. It is essential for
refactoring that you have good tests. It's worth
spending the time to build the tests, because the
tests give you the security you need to change
the program later. This is such an important
part of refactoring....
Martin Fowler, 1999
Rusi Filipov Softwerkskammer Karlsruhe 2014
Die Legacy Code Challenge
But the special problem of legacy code is that it
was never designed to be testable. -Péter Török
Rusi Filipov Softwerkskammer Karlsruhe 2014
Legacy Code – Definition
In the industry, legacy code is often used as a
slang term for difficult to change code that we
don't understand. But over years of working with
teams I've arrived at a different definition.
To me, legacy code is simply code without tests.
I've gotten some grief for this definition. What do
tests have to do with whether code is bad? To me,
the answer is straightforward...
Michael Feathers, 2004
Rusi Filipov Softwerkskammer Karlsruhe 2014
Legacy Code – Definition
Code without tests is bad code. It doesn't matter
how well written it is; it doesn't matter how pretty
or object-oriented or well-encapsulated it is.
With tests, we can change the behavior of our code
quickly and verifiably. Without them, we really
don't know if our code is getting better or worse.
Michael Feathers, 2004
Rusi Filipov Softwerkskammer Karlsruhe 2014
Test Coverings and Dependencies
Example: InvoiceUpdateResponder
When classes depend directly on things that are
hard to use in a test, they are hard to modify and
hard to work with.
Dependency is one of the most critical problems
in software development. Much legacy code work
involves breaking dependencies, so that change
can be easier.
Michael Feathers, 2004
Rusi Filipov Softwerkskammer Karlsruhe 2014
The Legacy Code Dilemma
When we change code, we should have tests in
place.
To put tests in place, we often have to change
code.
Michael Feathers, 2004
Rusi Filipov Softwerkskammer Karlsruhe 2014
The Legacy Code Change Algorithm
1. Identify change points.
2. Find test points.
3. Break dependencies.
4. Write tests.
5. Make changes and refactor.
Michael Feathers, 2004
Rusi Filipov Softwerkskammer Karlsruhe 2014
Unit Tests – Definition
Unit tests run fast. If they don't run fast, they aren't unit
tests. Other kinds of tests often masquerade as unit
tests. A test is not a unit test if:
●
It talks to a database
●
It communicates across a network
●
It touches the file system
●
You have to do special things to your environment
(such as editing configuration files) to run it.
Michael Feathers, 2004
Rusi Filipov Softwerkskammer Karlsruhe 2014
Unit Tests – Definition
Unit Tests are F.I.R.S.T.
●
Fast
●
Isolated
●
Repeatable
●
Self-verifying
●
Timely
Tim Ottinger, Jeff Langr
Rusi Filipov Softwerkskammer Karlsruhe 2014
Coupling: Static Dependencies
Object Peer Stereotype: Collaborator
●
Object with logic and behavior that we use
●
In test: replace collaborators with mocks
●
Inject mocked collaborators in SUT
●
Note: not all kinds of objects are Collaborators
Rusi Filipov Softwerkskammer Karlsruhe 2014
Refactor: Static Dependencies
Pass Collaborators „from Above“
● Avoid singletons and creating new collaborators
●
Accept collaborators via the constructor
●
Who should create the collaborators?
●
Parent object, main module, dependency injector
●
What about indirect collaborators?
Rusi Filipov Softwerkskammer Karlsruhe 2014
Coupling: Dynamic Dependencies
Situation: not possible to create collaborator at
construction time during object-wiring
●
Not enough initial information to create collaborator
●
Information available only after the SUT gets active
●
Must create collaborator dynamically after wiring
●
And still replace it with mock object in the test
Rusi Filipov Softwerkskammer Karlsruhe 2014
Distraction: Doing Too Much
Situation: a class is overloaded with many
responsibilities that prevent good testing
●
Class is not focused to do one thing
●
Instead: eierlegende Wollmilchsau
=> Class is harder to understand
=> Class has higher bug probability
=> Too many combinations of „features“ to test
Rusi Filipov Softwerkskammer Karlsruhe 2014
Refactoring Challenge: FtpClient
Evolution of a „Feature-Rich“ FTP Client
●
Core operations: list and download remote files
●
Extra gem: verify checksum of downloads
●
Extra gem: cache results from listings
●
Extra gem: reconnect if connection fails
●
New requirement: add ability to poll multiple
mirrored servers, so that the fastest one gets
used
Rusi Filipov Softwerkskammer Karlsruhe 2014
FtpClient: Original
Rusi Filipov Softwerkskammer Karlsruhe 2014
FtpClient: Refactored
Rusi Filipov Softwerkskammer Karlsruhe 2014
References and Code
●
Martin Fowler: Refactoring Improving the Design
of Existing Code, 1999
●
Michael Feathers: Working Effectively with Legacy
Code, 2004
●
Steve Freeman and Nat Pryce: Growing Object-
Oriented Software Guided by Tests, 2009
●
Source Code on GitHub
https://github.com/rusio/refactoring-for-tests
Rusi Filipov Softwerkskammer Karlsruhe 2014

Contenu connexe

Dernier

Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Paola De la Torre
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...HostedbyConfluent
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 

Dernier (20)

Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 

En vedette

How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Applitools
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at WorkGetSmarter
 

En vedette (20)

How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 
ChatGPT webinar slides
ChatGPT webinar slidesChatGPT webinar slides
ChatGPT webinar slides
 

Refactoring for Testability

  • 1. Refactoring for Testability Techniken für Post-Factum Abdeckug mit Tests Rusi Filipov, 2014 Softwerkskammer Karlsruhe
  • 2. Agenda ● Legacy Code und das Refactoring Dilemma ● Coding Dojo mit Refactoring-Aufgaben ● Typische Situationen analysieren ● Typische Refactorings anwenden ● Ziel: Gute Unit Tests in Legacy Code Rusi Filipov Softwerkskammer Karlsruhe 2014
  • 3. The First Step in Refactoring Whenever I do refactoring, the first step is always the same. I need to build a solid set of tests for that section of code. The tests are essential because even though I follow refactorings structured to avoid most of the opportunities for introducing bugs, I'm still human and still make mistakes. Thus I need solid tests. Martin Fowler, 1999 Rusi Filipov Softwerkskammer Karlsruhe 2014
  • 4. The First Step in Refactoring As we do the refactoring, we will lean on the tests. I'm going to be relying on the tests to tell me whether I introduce a bug. It is essential for refactoring that you have good tests. It's worth spending the time to build the tests, because the tests give you the security you need to change the program later. This is such an important part of refactoring.... Martin Fowler, 1999 Rusi Filipov Softwerkskammer Karlsruhe 2014
  • 5. Die Legacy Code Challenge But the special problem of legacy code is that it was never designed to be testable. -Péter Török Rusi Filipov Softwerkskammer Karlsruhe 2014
  • 6. Legacy Code – Definition In the industry, legacy code is often used as a slang term for difficult to change code that we don't understand. But over years of working with teams I've arrived at a different definition. To me, legacy code is simply code without tests. I've gotten some grief for this definition. What do tests have to do with whether code is bad? To me, the answer is straightforward... Michael Feathers, 2004 Rusi Filipov Softwerkskammer Karlsruhe 2014
  • 7. Legacy Code – Definition Code without tests is bad code. It doesn't matter how well written it is; it doesn't matter how pretty or object-oriented or well-encapsulated it is. With tests, we can change the behavior of our code quickly and verifiably. Without them, we really don't know if our code is getting better or worse. Michael Feathers, 2004 Rusi Filipov Softwerkskammer Karlsruhe 2014
  • 8. Test Coverings and Dependencies Example: InvoiceUpdateResponder When classes depend directly on things that are hard to use in a test, they are hard to modify and hard to work with. Dependency is one of the most critical problems in software development. Much legacy code work involves breaking dependencies, so that change can be easier. Michael Feathers, 2004 Rusi Filipov Softwerkskammer Karlsruhe 2014
  • 9. The Legacy Code Dilemma When we change code, we should have tests in place. To put tests in place, we often have to change code. Michael Feathers, 2004 Rusi Filipov Softwerkskammer Karlsruhe 2014
  • 10. The Legacy Code Change Algorithm 1. Identify change points. 2. Find test points. 3. Break dependencies. 4. Write tests. 5. Make changes and refactor. Michael Feathers, 2004 Rusi Filipov Softwerkskammer Karlsruhe 2014
  • 11. Unit Tests – Definition Unit tests run fast. If they don't run fast, they aren't unit tests. Other kinds of tests often masquerade as unit tests. A test is not a unit test if: ● It talks to a database ● It communicates across a network ● It touches the file system ● You have to do special things to your environment (such as editing configuration files) to run it. Michael Feathers, 2004 Rusi Filipov Softwerkskammer Karlsruhe 2014
  • 12. Unit Tests – Definition Unit Tests are F.I.R.S.T. ● Fast ● Isolated ● Repeatable ● Self-verifying ● Timely Tim Ottinger, Jeff Langr Rusi Filipov Softwerkskammer Karlsruhe 2014
  • 13. Coupling: Static Dependencies Object Peer Stereotype: Collaborator ● Object with logic and behavior that we use ● In test: replace collaborators with mocks ● Inject mocked collaborators in SUT ● Note: not all kinds of objects are Collaborators Rusi Filipov Softwerkskammer Karlsruhe 2014
  • 14. Refactor: Static Dependencies Pass Collaborators „from Above“ ● Avoid singletons and creating new collaborators ● Accept collaborators via the constructor ● Who should create the collaborators? ● Parent object, main module, dependency injector ● What about indirect collaborators? Rusi Filipov Softwerkskammer Karlsruhe 2014
  • 15. Coupling: Dynamic Dependencies Situation: not possible to create collaborator at construction time during object-wiring ● Not enough initial information to create collaborator ● Information available only after the SUT gets active ● Must create collaborator dynamically after wiring ● And still replace it with mock object in the test Rusi Filipov Softwerkskammer Karlsruhe 2014
  • 16. Distraction: Doing Too Much Situation: a class is overloaded with many responsibilities that prevent good testing ● Class is not focused to do one thing ● Instead: eierlegende Wollmilchsau => Class is harder to understand => Class has higher bug probability => Too many combinations of „features“ to test Rusi Filipov Softwerkskammer Karlsruhe 2014
  • 17. Refactoring Challenge: FtpClient Evolution of a „Feature-Rich“ FTP Client ● Core operations: list and download remote files ● Extra gem: verify checksum of downloads ● Extra gem: cache results from listings ● Extra gem: reconnect if connection fails ● New requirement: add ability to poll multiple mirrored servers, so that the fastest one gets used Rusi Filipov Softwerkskammer Karlsruhe 2014
  • 18. FtpClient: Original Rusi Filipov Softwerkskammer Karlsruhe 2014
  • 19. FtpClient: Refactored Rusi Filipov Softwerkskammer Karlsruhe 2014
  • 20. References and Code ● Martin Fowler: Refactoring Improving the Design of Existing Code, 1999 ● Michael Feathers: Working Effectively with Legacy Code, 2004 ● Steve Freeman and Nat Pryce: Growing Object- Oriented Software Guided by Tests, 2009 ● Source Code on GitHub https://github.com/rusio/refactoring-for-tests Rusi Filipov Softwerkskammer Karlsruhe 2014

Notes de l'éditeur

  1. Collaborator Lifetime: Was heisst es für den Lifecycle des Kollaborators wenn man ihn über den Konstruktor eingereicht bekommt? - Der Kollaborator muss vor dem Objekt geboren werden und muss mind. solange leben wie das Objekt selbst lebt.
  2. Don't Implement This With a Single Class!! Single Class = Higher Chance of Buggy Implementation Single Class = Unit-Tests are Doomed Must Extract Secondary Responsibilities into Separate Classes And Test only the Primary Responsibility of Each Class
  3. Don't Implement This With a Single Class!! Single Class = Higher Chance of Buggy Implementation Single Class = Unit-Tests are Doomed Must Extract Secondary Responsibilities into Separate Classes And Test only the Primary Responsibility of Each Class
  4. Don't Implement This With a Single Class!! Single Class = Higher Chance of Buggy Implementation Single Class = Unit-Tests are Doomed Must Extract Secondary Responsibilities into Separate Classes And Test only the Primary Responsibility of Each Class
  5. Don't Implement This With a Single Class!! Single Class = Higher Chance of Buggy Implementation Single Class = Unit-Tests are Doomed Must Extract Secondary Responsibilities into Separate Classes And Test only the Primary Responsibility of Each Class