SlideShare a Scribd company logo
Bye Bye Cowboy-Coder 
days!
Bye Bye Cowboy Coder Days! (Legacy Code & TDD)
What do developers do for living? 
Change the code!
Four Reasons to Change Software 
• Adding a feature 
• Fixing a bug 
• Improving the design 
• Optimizing resource usage
Changing Software 
Adding a 
Feature 
Fixing a Bug Refactoring Optimizing 
Structure Changes Changes Changes 
New 
Changes 
Functionality 
Functionality Changes 
Resource 
Usage 
Changes
Preserving behavior 
Existing Behavior New Behavior
Risky Change 
1. What changes do we have to make? 
2. How will we know that we've done them correctly? 
3. How will we know that we haven't broken anything?
How?
Bye Bye Cowboy Coder Days! (Legacy Code & TDD)
Working with Feedback! 
Yes, Unit Tests…
Few qualities of Unit Tests 
• They run fast 
• They help us localize problems
Dependencies… 
• No way to run separately 
• Takes time to load 
• Hard to localize problem
GUI Tests 
Integration 
Tests 
Unit Tests
GUI Tests 
Integration 
Tests 
Unit Tests
Bye Bye Cowboy Coder Days! (Legacy Code & TDD)
Lag Time 
Time to fix/Price 
1 Minute 10 Minutes 1 Hour 1 Day 1 Week 
Time to fix/Price
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.
The Legacy Code Change Algorithm 
1. Identify change points 
2. Find test points 
3. Break dependencies 
4. Write tests 
5. Make changes and refactor
Sensing and Separation 
• We break dependencies to sense when we can't access 
values our code computes 
• We break dependencies to separate when we can't even get 
a piece of code into a test harness to run
Faking Collaborators 
• Fake Objects 
• Mock Objects 
• Nothing!
Sprout
Sprout Method 
1. Identify where to change 
2. Wdown a method call and then comment it out. 
3. Determine what local variables you need for the call 
4. Determine return value 
5. Develop the sprout method using test-driven development 
6. Remove the comment in the source method
Sprout Class 
1. Identify where you need to make your code change. 
2. Write class in that place, and call a method; then 
comment those lines out 
3. Determine what local variables you need from the source 
method, and make them arguments to the classes' 
constructor 
4. Determine return values to the source method and add a 
call in the source method to receive those values. 
5. Develop the sprout class test first 
6. Remove the comment in the source
Wrap
Wrap Method 
1. Identify a method you need to change 
2. If the change can be formulated as a single sequence of 
statements in one place, develop a new method for it 
using test-driven development 
3. Create another method that calls the new method and the 
old method
Wrap Class 
1. Identify a method where you need to make a change. 
2. Create a class that accepts the class you are going to 
wrap as a constructor argument. 
3. Create a method on that class, using test-driven 
development. 
4. Write another method that calls the new method and the 
old method on the wrapped class 
5. Instantiate the wrapper class in your code in the place 
where you need to enable the new behavior
TDD and Legacy Code 
1. Get the class you want to change under test 
2. Write a failing test case 
3. Get it to compile 
4. Make it pass (Try not to change existing code as you do 
this) 
5. Remove duplication 
6. Repeat
Allways check if tests are testing 
1. Write test and new code in separate changelist 
2. Shelve new code change list 
3. Ensure tests are failing
Characterization Tests 
1. Use a piece of code in a test harness 
2. Write an assertion that you know will fail - let the 
failure tell you what the behavior is 
3. Change the test so that it expects the behavior that 
the code produces 
4. Repeat
I Can't Get This Class into a Test 
Harness 
• Lean on the Compiler 
• Extract Interface 
• Extract Implementer 
• Subclass and Override Method 
• Parameterize Constructor 
• Parameterize Method 
• Extract and Override Getter 
• Extract and Override Factory Method 
• Singleton Design Pattern
When All Else Fails, Do Some Scratch 
Refactoring
Questions?
Agile Turas Lietuvoje 
• Agile Tour Kaunas – rugsėjo 25 d. 
• Agile Tour Vilnius – spalio 9 d. 
• Agile Master Classes 
www.agileturas.lt
The World’s Best Intro to TDD 
• J. B. Rainsberger, Kanada 
• 2 Dienų mokymai, Kaune 
• Rugsėjo 23 - 24 d. 
www.agileturas.lt/master-classes

More Related Content

What's hot (20)

PPTX
Unit tests benefits
Kate Semizhon
 
PPTX
Git branching policy and review comment's prefix
Kumaresh Chandra Baruri
 
PPT
Code review
dqpi
 
PDF
Quality Assurance Guidelines
Tim Stribos
 
PDF
Behavior Driven Development with SpecFlow
Rachid Kherrazi
 
PDF
DIG1108C Lesson 7 Fall 2014
David Wolfpaw
 
KEY
Unit Testing Your Application
Paladin Web Services
 
PDF
Test Driven Development (TDD)
David Ehringer
 
PDF
Agile Mumbai 2020 Conference | How to get the best ROI on Your Test Automati...
AgileNetwork
 
PDF
Test Driven Development
Mireia Sangalo
 
PDF
iOS Test-Driven Development
Pablo Villar
 
PPTX
TDD- Test Driven Development
Liza Antoun
 
PDF
Code Review
Lukas Rypl
 
PDF
Agile Programming Systems # TDD intro
Vitaliy Kulikov
 
PPTX
Bdd and spec flow
Charles Nurse
 
PPTX
JavaScript Unit Testing
L&T Technology Services Limited
 
PPTX
Testing JavaScript Applications
Muhammad Samu
 
PPT
Acceptance Test Driven Development With Spec Flow And Friends
Christopher Bartling
 
PDF
Unit test in a nutshell
Roberto Bettazzoni
 
PDF
ABAP Code Retreat Frankfurt 2016: TDD - Test Driven Development
Hendrik Neumann
 
Unit tests benefits
Kate Semizhon
 
Git branching policy and review comment's prefix
Kumaresh Chandra Baruri
 
Code review
dqpi
 
Quality Assurance Guidelines
Tim Stribos
 
Behavior Driven Development with SpecFlow
Rachid Kherrazi
 
DIG1108C Lesson 7 Fall 2014
David Wolfpaw
 
Unit Testing Your Application
Paladin Web Services
 
Test Driven Development (TDD)
David Ehringer
 
Agile Mumbai 2020 Conference | How to get the best ROI on Your Test Automati...
AgileNetwork
 
Test Driven Development
Mireia Sangalo
 
iOS Test-Driven Development
Pablo Villar
 
TDD- Test Driven Development
Liza Antoun
 
Code Review
Lukas Rypl
 
Agile Programming Systems # TDD intro
Vitaliy Kulikov
 
Bdd and spec flow
Charles Nurse
 
JavaScript Unit Testing
L&T Technology Services Limited
 
Testing JavaScript Applications
Muhammad Samu
 
Acceptance Test Driven Development With Spec Flow And Friends
Christopher Bartling
 
Unit test in a nutshell
Roberto Bettazzoni
 
ABAP Code Retreat Frankfurt 2016: TDD - Test Driven Development
Hendrik Neumann
 

Similar to Bye Bye Cowboy Coder Days! (Legacy Code & TDD) (20)

PDF
Working With Legacy Code
Andrea Polci
 
PDF
Keeping code clean
Brett Child
 
PPTX
Test Drive Dirven Driver HAHAahhaha.pptx
findwaytocom
 
PDF
Test and Behaviour Driven Development (TDD/BDD)
Lars Thorup
 
PDF
Test-Driven Development
Amir Assad
 
PPTX
An Introduction to Developer Testing
Will Green
 
PDF
Unit testing legacy code
Lars Thorup
 
PDF
The Way of The Software Craftsman # Part One: The Beginning
Vitaliy Kulikov
 
PPT
TDD And Refactoring
Naresh Jain
 
PDF
[DevDay2018] Let’s all get along. Clean Code please! - By: Christophe K. Ngo,...
DevDay Da Nang
 
KEY
TDD refresher
Kerry Buckley
 
ZIP
Test Driven Development
guy_davis
 
PPTX
Refactoring workshop
Itzik Saban
 
PDF
Intro to JavaScript Testing
Ran Mizrahi
 
PPTX
Working Effectively with Legacy Code
Orbit One - We create coherence
 
PPTX
TDD Training
Manuela Grindei
 
PPTX
Agile Testing Agile Ottawa April 2015
Dag Rowe
 
PDF
20191116 DevFest 2019 The Legacy Code came to stay (El legacy vino para queda...
Antonio de la Torre Fernández
 
KEY
Driving application development through behavior driven development
Einar Ingebrigtsen
 
ODP
Effective TDD - Less is more
Ben Lau
 
Working With Legacy Code
Andrea Polci
 
Keeping code clean
Brett Child
 
Test Drive Dirven Driver HAHAahhaha.pptx
findwaytocom
 
Test and Behaviour Driven Development (TDD/BDD)
Lars Thorup
 
Test-Driven Development
Amir Assad
 
An Introduction to Developer Testing
Will Green
 
Unit testing legacy code
Lars Thorup
 
The Way of The Software Craftsman # Part One: The Beginning
Vitaliy Kulikov
 
TDD And Refactoring
Naresh Jain
 
[DevDay2018] Let’s all get along. Clean Code please! - By: Christophe K. Ngo,...
DevDay Da Nang
 
TDD refresher
Kerry Buckley
 
Test Driven Development
guy_davis
 
Refactoring workshop
Itzik Saban
 
Intro to JavaScript Testing
Ran Mizrahi
 
Working Effectively with Legacy Code
Orbit One - We create coherence
 
TDD Training
Manuela Grindei
 
Agile Testing Agile Ottawa April 2015
Dag Rowe
 
20191116 DevFest 2019 The Legacy Code came to stay (El legacy vino para queda...
Antonio de la Torre Fernández
 
Driving application development through behavior driven development
Einar Ingebrigtsen
 
Effective TDD - Less is more
Ben Lau
 
Ad

More from Kaunas Java User Group (13)

PPTX
Smart House Based on Raspberry PI + Java EE by Tadas Brasas
Kaunas Java User Group
 
PDF
Presentation
Kaunas Java User Group
 
PPTX
Automated infrastructure
Kaunas Java User Group
 
PDF
Adf presentation
Kaunas Java User Group
 
PPTX
Building with Gradle
Kaunas Java User Group
 
PDF
Eh cache in Kaunas JUG
Kaunas Java User Group
 
PDF
Apache Lucene Informacijos paieška
Kaunas Java User Group
 
PDF
Java 8 Stream API (Valdas Zigas)
Kaunas Java User Group
 
PDF
Intro to Java 8 Closures (Dainius Mezanskas)
Kaunas Java User Group
 
PDF
Kaunas JUG#1: Intro (Valdas Zigas)
Kaunas Java User Group
 
PDF
Kaunas JUG#1: Java History and Trends (Dainius Mezanskas)
Kaunas Java User Group
 
PDF
Kaunas JUG#2: Devoxx 2013 (Saulius Tvarijonas)
Kaunas Java User Group
 
Smart House Based on Raspberry PI + Java EE by Tadas Brasas
Kaunas Java User Group
 
Automated infrastructure
Kaunas Java User Group
 
Adf presentation
Kaunas Java User Group
 
Building with Gradle
Kaunas Java User Group
 
Eh cache in Kaunas JUG
Kaunas Java User Group
 
Apache Lucene Informacijos paieška
Kaunas Java User Group
 
Java 8 Stream API (Valdas Zigas)
Kaunas Java User Group
 
Intro to Java 8 Closures (Dainius Mezanskas)
Kaunas Java User Group
 
Kaunas JUG#1: Intro (Valdas Zigas)
Kaunas Java User Group
 
Kaunas JUG#1: Java History and Trends (Dainius Mezanskas)
Kaunas Java User Group
 
Kaunas JUG#2: Devoxx 2013 (Saulius Tvarijonas)
Kaunas Java User Group
 
Ad

Recently uploaded (20)

PDF
What’s my job again? Slides from Mark Simos talk at 2025 Tampa BSides
Mark Simos
 
PPTX
Designing_the_Future_AI_Driven_Product_Experiences_Across_Devices.pptx
presentifyai
 
PPTX
Mastering ODC + Okta Configuration - Chennai OSUG
HathiMaryA
 
PDF
UPDF - AI PDF Editor & Converter Key Features
DealFuel
 
PDF
SIZING YOUR AIR CONDITIONER---A PRACTICAL GUIDE.pdf
Muhammad Rizwan Akram
 
PDF
Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
PDF
“Computer Vision at Sea: Automated Fish Tracking for Sustainable Fishing,” a ...
Edge AI and Vision Alliance
 
PDF
UiPath DevConnect 2025: Agentic Automation Community User Group Meeting
DianaGray10
 
PDF
Future-Proof or Fall Behind? 10 Tech Trends You Can’t Afford to Ignore in 2025
DIGITALCONFEX
 
PDF
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
PPTX
MuleSoft MCP Support (Model Context Protocol) and Use Case Demo
shyamraj55
 
PDF
LOOPS in C Programming Language - Technology
RishabhDwivedi43
 
PDF
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
PDF
AI Agents in the Cloud: The Rise of Agentic Cloud Architecture
Lilly Gracia
 
PPTX
Future Tech Innovations 2025 – A TechLists Insight
TechLists
 
PPTX
The Project Compass - GDG on Campus MSIT
dscmsitkol
 
PDF
CIFDAQ Market Wrap for the week of 4th July 2025
CIFDAQ
 
PDF
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
PPTX
Digital Circuits, important subject in CS
contactparinay1
 
PDF
“Voice Interfaces on a Budget: Building Real-time Speech Recognition on Low-c...
Edge AI and Vision Alliance
 
What’s my job again? Slides from Mark Simos talk at 2025 Tampa BSides
Mark Simos
 
Designing_the_Future_AI_Driven_Product_Experiences_Across_Devices.pptx
presentifyai
 
Mastering ODC + Okta Configuration - Chennai OSUG
HathiMaryA
 
UPDF - AI PDF Editor & Converter Key Features
DealFuel
 
SIZING YOUR AIR CONDITIONER---A PRACTICAL GUIDE.pdf
Muhammad Rizwan Akram
 
Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
“Computer Vision at Sea: Automated Fish Tracking for Sustainable Fishing,” a ...
Edge AI and Vision Alliance
 
UiPath DevConnect 2025: Agentic Automation Community User Group Meeting
DianaGray10
 
Future-Proof or Fall Behind? 10 Tech Trends You Can’t Afford to Ignore in 2025
DIGITALCONFEX
 
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
MuleSoft MCP Support (Model Context Protocol) and Use Case Demo
shyamraj55
 
LOOPS in C Programming Language - Technology
RishabhDwivedi43
 
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
AI Agents in the Cloud: The Rise of Agentic Cloud Architecture
Lilly Gracia
 
Future Tech Innovations 2025 – A TechLists Insight
TechLists
 
The Project Compass - GDG on Campus MSIT
dscmsitkol
 
CIFDAQ Market Wrap for the week of 4th July 2025
CIFDAQ
 
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
Digital Circuits, important subject in CS
contactparinay1
 
“Voice Interfaces on a Budget: Building Real-time Speech Recognition on Low-c...
Edge AI and Vision Alliance
 

Bye Bye Cowboy Coder Days! (Legacy Code & TDD)

  • 3. What do developers do for living? Change the code!
  • 4. Four Reasons to Change Software • Adding a feature • Fixing a bug • Improving the design • Optimizing resource usage
  • 5. Changing Software Adding a Feature Fixing a Bug Refactoring Optimizing Structure Changes Changes Changes New Changes Functionality Functionality Changes Resource Usage Changes
  • 6. Preserving behavior Existing Behavior New Behavior
  • 7. Risky Change 1. What changes do we have to make? 2. How will we know that we've done them correctly? 3. How will we know that we haven't broken anything?
  • 10. Working with Feedback! Yes, Unit Tests…
  • 11. Few qualities of Unit Tests • They run fast • They help us localize problems
  • 12. Dependencies… • No way to run separately • Takes time to load • Hard to localize problem
  • 13. GUI Tests Integration Tests Unit Tests
  • 14. GUI Tests Integration Tests Unit Tests
  • 16. Lag Time Time to fix/Price 1 Minute 10 Minutes 1 Hour 1 Day 1 Week Time to fix/Price
  • 17. 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.
  • 18. The Legacy Code Change Algorithm 1. Identify change points 2. Find test points 3. Break dependencies 4. Write tests 5. Make changes and refactor
  • 19. Sensing and Separation • We break dependencies to sense when we can't access values our code computes • We break dependencies to separate when we can't even get a piece of code into a test harness to run
  • 20. Faking Collaborators • Fake Objects • Mock Objects • Nothing!
  • 22. Sprout Method 1. Identify where to change 2. Wdown a method call and then comment it out. 3. Determine what local variables you need for the call 4. Determine return value 5. Develop the sprout method using test-driven development 6. Remove the comment in the source method
  • 23. Sprout Class 1. Identify where you need to make your code change. 2. Write class in that place, and call a method; then comment those lines out 3. Determine what local variables you need from the source method, and make them arguments to the classes' constructor 4. Determine return values to the source method and add a call in the source method to receive those values. 5. Develop the sprout class test first 6. Remove the comment in the source
  • 24. Wrap
  • 25. Wrap Method 1. Identify a method you need to change 2. If the change can be formulated as a single sequence of statements in one place, develop a new method for it using test-driven development 3. Create another method that calls the new method and the old method
  • 26. Wrap Class 1. Identify a method where you need to make a change. 2. Create a class that accepts the class you are going to wrap as a constructor argument. 3. Create a method on that class, using test-driven development. 4. Write another method that calls the new method and the old method on the wrapped class 5. Instantiate the wrapper class in your code in the place where you need to enable the new behavior
  • 27. TDD and Legacy Code 1. Get the class you want to change under test 2. Write a failing test case 3. Get it to compile 4. Make it pass (Try not to change existing code as you do this) 5. Remove duplication 6. Repeat
  • 28. Allways check if tests are testing 1. Write test and new code in separate changelist 2. Shelve new code change list 3. Ensure tests are failing
  • 29. Characterization Tests 1. Use a piece of code in a test harness 2. Write an assertion that you know will fail - let the failure tell you what the behavior is 3. Change the test so that it expects the behavior that the code produces 4. Repeat
  • 30. I Can't Get This Class into a Test Harness • Lean on the Compiler • Extract Interface • Extract Implementer • Subclass and Override Method • Parameterize Constructor • Parameterize Method • Extract and Override Getter • Extract and Override Factory Method • Singleton Design Pattern
  • 31. When All Else Fails, Do Some Scratch Refactoring
  • 33. Agile Turas Lietuvoje • Agile Tour Kaunas – rugsėjo 25 d. • Agile Tour Vilnius – spalio 9 d. • Agile Master Classes www.agileturas.lt
  • 34. The World’s Best Intro to TDD • J. B. Rainsberger, Kanada • 2 Dienų mokymai, Kaune • Rugsėjo 23 - 24 d. www.agileturas.lt/master-classes

Editor's Notes

  • #12: A unit test that takes 1/10th of a second to run is a slow unit test. If you have a project with 3,000 classes and there are about 10 tests apiece, that is 30,000 tests. How long will it take to run all of the tests for that project if they take 1/10th of a second apiece? Close to an hour. Elaborate more on problem localization
  • #16: Realybėje tai pasidaro dar panašiau į tai...
  • #17: Lag time is the amount of time that passes between a change that you make and the moment that you get real feedback about the change
  • #20: Generally, when we want to get tests in place, there are two reasons to break dependencies: sensing and separation.
  • #28: One of the most valuable things about TDD is that it lets us concentrate on one thing at a time. We are either writing code or refactoring; we are never doing both at once. That separation is particularly valuable in legacy code because it lets us write new code independently of new code. After we have written some new code, we can refactor to remove any duplication between it and the old code.
  • #30: Usually unexpected behavior IS expected from the whole application! If the system has been deployed, you need to examine the possibility that someone is depending on that behavior, even though you see it as a bug. Know your code better!
  • #32: Best way to get familiar with the code!