Bye Bye Cowboy-Coder 
days!
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?
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
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

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

  • 1.
  • 3.
    What do developersdo for living? Change the code!
  • 4.
    Four Reasons toChange Software • Adding a feature • Fixing a bug • Improving the design • Optimizing resource usage
  • 5.
    Changing Software Addinga Feature Fixing a Bug Refactoring Optimizing Structure Changes Changes Changes New Changes Functionality Functionality Changes Resource Usage Changes
  • 6.
    Preserving behavior ExistingBehavior 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?
  • 8.
  • 10.
    Working with Feedback! Yes, Unit Tests…
  • 11.
    Few qualities ofUnit Tests • They run fast • They help us localize problems
  • 12.
    Dependencies… • Noway 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 Timeto fix/Price 1 Minute 10 Minutes 1 Hour 1 Day 1 Week Time to fix/Price
  • 17.
    The Legacy CodeDilemma When we change code, we should have tests in place. To put tests in place, we often have to change code.
  • 18.
    The Legacy CodeChange 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!
  • 21.
  • 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.
  • 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 LegacyCode 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 iftests 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 GetThis 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 ElseFails, Do Some Scratch Refactoring
  • 32.
  • 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 BestIntro 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!