bye bye cowboy coder days! (legacy code & tdd)
DESCRIPTION
Bye Bye Cowboy-Coder days! by Vytautas Dagilis. „Working Effectively with Legacy Code“- one of the best books which talks about Unit testing and TDD. Presentation covers best practices listed in the book as well as shows which of them were successfully implemented in practice. You will find out how to commit changes and be sure that they work even without running and testing application itself.TRANSCRIPT
![Page 1: Bye Bye Cowboy Coder Days! (Legacy Code & TDD)](https://reader033.vdocuments.net/reader033/viewer/2022052714/555c2518d8b42a09438b4bdd/html5/thumbnails/1.jpg)
Bye Bye Cowboy-Coder days!
![Page 2: Bye Bye Cowboy Coder Days! (Legacy Code & TDD)](https://reader033.vdocuments.net/reader033/viewer/2022052714/555c2518d8b42a09438b4bdd/html5/thumbnails/2.jpg)
![Page 3: Bye Bye Cowboy Coder Days! (Legacy Code & TDD)](https://reader033.vdocuments.net/reader033/viewer/2022052714/555c2518d8b42a09438b4bdd/html5/thumbnails/3.jpg)
What do developers do for living?Change the code!
![Page 4: Bye Bye Cowboy Coder Days! (Legacy Code & TDD)](https://reader033.vdocuments.net/reader033/viewer/2022052714/555c2518d8b42a09438b4bdd/html5/thumbnails/4.jpg)
Four Reasons to Change Software• Adding a feature• Fixing a bug• Improving the design• Optimizing resource usage
![Page 5: Bye Bye Cowboy Coder Days! (Legacy Code & TDD)](https://reader033.vdocuments.net/reader033/viewer/2022052714/555c2518d8b42a09438b4bdd/html5/thumbnails/5.jpg)
Changing Software
Adding a Feature
Fixing a Bug Refactoring Optimizing
Structure Changes Changes ChangesNew Functionality
Changes
Functionality ChangesResource Usage
Changes
![Page 6: Bye Bye Cowboy Coder Days! (Legacy Code & TDD)](https://reader033.vdocuments.net/reader033/viewer/2022052714/555c2518d8b42a09438b4bdd/html5/thumbnails/6.jpg)
Preserving behavior
Existing Behavior New Behavior
![Page 7: Bye Bye Cowboy Coder Days! (Legacy Code & TDD)](https://reader033.vdocuments.net/reader033/viewer/2022052714/555c2518d8b42a09438b4bdd/html5/thumbnails/7.jpg)
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?
![Page 8: Bye Bye Cowboy Coder Days! (Legacy Code & TDD)](https://reader033.vdocuments.net/reader033/viewer/2022052714/555c2518d8b42a09438b4bdd/html5/thumbnails/8.jpg)
How?
![Page 9: Bye Bye Cowboy Coder Days! (Legacy Code & TDD)](https://reader033.vdocuments.net/reader033/viewer/2022052714/555c2518d8b42a09438b4bdd/html5/thumbnails/9.jpg)
![Page 10: Bye Bye Cowboy Coder Days! (Legacy Code & TDD)](https://reader033.vdocuments.net/reader033/viewer/2022052714/555c2518d8b42a09438b4bdd/html5/thumbnails/10.jpg)
Working with Feedback!
Yes, Unit Tests…
![Page 11: Bye Bye Cowboy Coder Days! (Legacy Code & TDD)](https://reader033.vdocuments.net/reader033/viewer/2022052714/555c2518d8b42a09438b4bdd/html5/thumbnails/11.jpg)
Few qualities of Unit Tests
• They run fast• They help us localize problems
![Page 12: Bye Bye Cowboy Coder Days! (Legacy Code & TDD)](https://reader033.vdocuments.net/reader033/viewer/2022052714/555c2518d8b42a09438b4bdd/html5/thumbnails/12.jpg)
Dependencies…
• No way to run separately• Takes time to load• Hard to localize problem
![Page 13: Bye Bye Cowboy Coder Days! (Legacy Code & TDD)](https://reader033.vdocuments.net/reader033/viewer/2022052714/555c2518d8b42a09438b4bdd/html5/thumbnails/13.jpg)
GUI Tests
Integration Tests
Unit Tests
![Page 14: Bye Bye Cowboy Coder Days! (Legacy Code & TDD)](https://reader033.vdocuments.net/reader033/viewer/2022052714/555c2518d8b42a09438b4bdd/html5/thumbnails/14.jpg)
GUI Tests
Integration TestsUnit Tests
![Page 15: Bye Bye Cowboy Coder Days! (Legacy Code & TDD)](https://reader033.vdocuments.net/reader033/viewer/2022052714/555c2518d8b42a09438b4bdd/html5/thumbnails/15.jpg)
![Page 16: Bye Bye Cowboy Coder Days! (Legacy Code & TDD)](https://reader033.vdocuments.net/reader033/viewer/2022052714/555c2518d8b42a09438b4bdd/html5/thumbnails/16.jpg)
Lag Time
1 Minute 10 Minutes 1 Hour 1 Day 1 Week
Time to fix/Price
Time to fix/Price
![Page 17: Bye Bye Cowboy Coder Days! (Legacy Code & TDD)](https://reader033.vdocuments.net/reader033/viewer/2022052714/555c2518d8b42a09438b4bdd/html5/thumbnails/17.jpg)
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.
![Page 18: Bye Bye Cowboy Coder Days! (Legacy Code & TDD)](https://reader033.vdocuments.net/reader033/viewer/2022052714/555c2518d8b42a09438b4bdd/html5/thumbnails/18.jpg)
The Legacy Code Change Algorithm1. Identify change points2. Find test points3. Break dependencies4. Write tests5. Make changes and refactor
![Page 19: Bye Bye Cowboy Coder Days! (Legacy Code & TDD)](https://reader033.vdocuments.net/reader033/viewer/2022052714/555c2518d8b42a09438b4bdd/html5/thumbnails/19.jpg)
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
![Page 20: Bye Bye Cowboy Coder Days! (Legacy Code & TDD)](https://reader033.vdocuments.net/reader033/viewer/2022052714/555c2518d8b42a09438b4bdd/html5/thumbnails/20.jpg)
Faking Collaborators
• Fake Objects• Mock Objects• Nothing!
![Page 21: Bye Bye Cowboy Coder Days! (Legacy Code & TDD)](https://reader033.vdocuments.net/reader033/viewer/2022052714/555c2518d8b42a09438b4bdd/html5/thumbnails/21.jpg)
Sprout
![Page 22: Bye Bye Cowboy Coder Days! (Legacy Code & TDD)](https://reader033.vdocuments.net/reader033/viewer/2022052714/555c2518d8b42a09438b4bdd/html5/thumbnails/22.jpg)
Sprout Method
1. Identify where to change2. Wdown a method call and then comment it out.3. Determine what local variables you need for
the call4. Determine return value5. Develop the sprout method using test-driven
development6. Remove the comment in the source method
![Page 23: Bye Bye Cowboy Coder Days! (Legacy Code & TDD)](https://reader033.vdocuments.net/reader033/viewer/2022052714/555c2518d8b42a09438b4bdd/html5/thumbnails/23.jpg)
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 out3. 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 first6. Remove the comment in the source
![Page 24: Bye Bye Cowboy Coder Days! (Legacy Code & TDD)](https://reader033.vdocuments.net/reader033/viewer/2022052714/555c2518d8b42a09438b4bdd/html5/thumbnails/24.jpg)
Wrap
![Page 25: Bye Bye Cowboy Coder Days! (Legacy Code & TDD)](https://reader033.vdocuments.net/reader033/viewer/2022052714/555c2518d8b42a09438b4bdd/html5/thumbnails/25.jpg)
Wrap Method
1. Identify a method you need to change2. 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
![Page 26: Bye Bye Cowboy Coder Days! (Legacy Code & TDD)](https://reader033.vdocuments.net/reader033/viewer/2022052714/555c2518d8b42a09438b4bdd/html5/thumbnails/26.jpg)
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
![Page 27: Bye Bye Cowboy Coder Days! (Legacy Code & TDD)](https://reader033.vdocuments.net/reader033/viewer/2022052714/555c2518d8b42a09438b4bdd/html5/thumbnails/27.jpg)
TDD and Legacy Code
1. Get the class you want to change under test2. Write a failing test case3. Get it to compile4. Make it pass (Try not to change existing code
as you do this)5. Remove duplication6. Repeat
![Page 28: Bye Bye Cowboy Coder Days! (Legacy Code & TDD)](https://reader033.vdocuments.net/reader033/viewer/2022052714/555c2518d8b42a09438b4bdd/html5/thumbnails/28.jpg)
Allways check if tests are testing1. Write test and new code in separate changelist2. Shelve new code change list3. Ensure tests are failing
![Page 29: Bye Bye Cowboy Coder Days! (Legacy Code & TDD)](https://reader033.vdocuments.net/reader033/viewer/2022052714/555c2518d8b42a09438b4bdd/html5/thumbnails/29.jpg)
Characterization Tests
1. Use a piece of code in a test harness2. Write an assertion that you know will fail -
let the failure tell you what the behavior is3. Change the test so that it expects the
behavior that the code produces4. Repeat
![Page 30: Bye Bye Cowboy Coder Days! (Legacy Code & TDD)](https://reader033.vdocuments.net/reader033/viewer/2022052714/555c2518d8b42a09438b4bdd/html5/thumbnails/30.jpg)
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
![Page 31: Bye Bye Cowboy Coder Days! (Legacy Code & TDD)](https://reader033.vdocuments.net/reader033/viewer/2022052714/555c2518d8b42a09438b4bdd/html5/thumbnails/31.jpg)
When All Else Fails, Do Some Scratch Refactoring
![Page 32: Bye Bye Cowboy Coder Days! (Legacy Code & TDD)](https://reader033.vdocuments.net/reader033/viewer/2022052714/555c2518d8b42a09438b4bdd/html5/thumbnails/32.jpg)
Questions?
![Page 33: Bye Bye Cowboy Coder Days! (Legacy Code & TDD)](https://reader033.vdocuments.net/reader033/viewer/2022052714/555c2518d8b42a09438b4bdd/html5/thumbnails/33.jpg)
Agile Turas Lietuvoje
• Agile Tour Kaunas – rugsėjo 25 d.• Agile Tour Vilnius – spalio 9 d.• Agile Master Classes
www.agileturas.lt
![Page 34: Bye Bye Cowboy Coder Days! (Legacy Code & TDD)](https://reader033.vdocuments.net/reader033/viewer/2022052714/555c2518d8b42a09438b4bdd/html5/thumbnails/34.jpg)
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