tdd, the way to better software | dan ursu | codeway 2015
TRANSCRIPT
![Page 1: TDD, the way to better software | Dan Ursu | CodeWay 2015](https://reader034.vdocument.in/reader034/viewer/2022051503/58897ccc1a28ab461b8b4e4d/html5/thumbnails/1.jpg)
TDD, the way to better software by Ursu DanSession 2/6 15:00 - 16:00
#Codeway
Dan Ursu, iOS Developer YOPESO
![Page 2: TDD, the way to better software | Dan Ursu | CodeWay 2015](https://reader034.vdocument.in/reader034/viewer/2022051503/58897ccc1a28ab461b8b4e4d/html5/thumbnails/2.jpg)
The Problem:
Have you ever told said the following things
![Page 3: TDD, the way to better software | Dan Ursu | CodeWay 2015](https://reader034.vdocument.in/reader034/viewer/2022051503/58897ccc1a28ab461b8b4e4d/html5/thumbnails/3.jpg)
“After the next release let’s put aside 3 days for
refactoring” Manager’s thoughts:
“Again with these arguments about clean
code?”
![Page 4: TDD, the way to better software | Dan Ursu | CodeWay 2015](https://reader034.vdocument.in/reader034/viewer/2022051503/58897ccc1a28ab461b8b4e4d/html5/thumbnails/4.jpg)
“This task requires me to change the architecture so it takes a long time”
Manager’s thoughts: “ But it’s a simple button, why does a
simple button cause an architectural change?”
![Page 5: TDD, the way to better software | Dan Ursu | CodeWay 2015](https://reader034.vdocument.in/reader034/viewer/2022051503/58897ccc1a28ab461b8b4e4d/html5/thumbnails/5.jpg)
“I fixed one bug and 10 others popped up in
another place”Manager’s thoughts: “I am
starting to think you are unprofessional”
![Page 6: TDD, the way to better software | Dan Ursu | CodeWay 2015](https://reader034.vdocument.in/reader034/viewer/2022051503/58897ccc1a28ab461b8b4e4d/html5/thumbnails/6.jpg)
“I’m not sure how the code works because the colleague who wrote it is on a vacation”
Manager’s thoughts: “Really unprofessional”
![Page 7: TDD, the way to better software | Dan Ursu | CodeWay 2015](https://reader034.vdocument.in/reader034/viewer/2022051503/58897ccc1a28ab461b8b4e4d/html5/thumbnails/7.jpg)
“I don’t clean this piece of ugly code because i’m afraid it will break things”
Manager’s thoughts: “You sound highly
incompetent”
![Page 8: TDD, the way to better software | Dan Ursu | CodeWay 2015](https://reader034.vdocument.in/reader034/viewer/2022051503/58897ccc1a28ab461b8b4e4d/html5/thumbnails/8.jpg)
“I’ll finish the task and clean the code
afterwards” Manager’s thoughts:
“Yet you always seem to set aside time for refactoring”
![Page 9: TDD, the way to better software | Dan Ursu | CodeWay 2015](https://reader034.vdocument.in/reader034/viewer/2022051503/58897ccc1a28ab461b8b4e4d/html5/thumbnails/9.jpg)
You should never ask time for refactoring. You should refactor every time you see bad code.
You never ask time to write good software, the same way Cooks don’t ask time to cook good
food.
![Page 10: TDD, the way to better software | Dan Ursu | CodeWay 2015](https://reader034.vdocument.in/reader034/viewer/2022051503/58897ccc1a28ab461b8b4e4d/html5/thumbnails/10.jpg)
So what is TDD?
![Page 11: TDD, the way to better software | Dan Ursu | CodeWay 2015](https://reader034.vdocument.in/reader034/viewer/2022051503/58897ccc1a28ab461b8b4e4d/html5/thumbnails/11.jpg)
Test driven development is a discipline that eliminates
fear of cleaning code.
![Page 12: TDD, the way to better software | Dan Ursu | CodeWay 2015](https://reader034.vdocument.in/reader034/viewer/2022051503/58897ccc1a28ab461b8b4e4d/html5/thumbnails/12.jpg)
DemoEliminate fear? What an interesting and fascinating idea.
![Page 13: TDD, the way to better software | Dan Ursu | CodeWay 2015](https://reader034.vdocument.in/reader034/viewer/2022051503/58897ccc1a28ab461b8b4e4d/html5/thumbnails/13.jpg)
The 3 laws of TDD1. You are not allowed to write any production code
unless it is to make a failing unit test pass.
2. You are not allowed to write any more production code than is sufficient to pass the one failing unit
test. 3. You are not allowed to write any more of a unit
test than is sufficient to fail; 2 You are not allowed to write any more of a unit test than is sufficient to fail; and compilation failures are failures.2 3 You are not allowed to write any more production code than is sufficient to pass the one failing unit test.
![Page 14: TDD, the way to better software | Dan Ursu | CodeWay 2015](https://reader034.vdocument.in/reader034/viewer/2022051503/58897ccc1a28ab461b8b4e4d/html5/thumbnails/14.jpg)
1. Write a failing unit test 2. Make the test pass by implementing it
3. Refactor Repeat until software is done
TDD laws lock you into a cycle
![Page 15: TDD, the way to better software | Dan Ursu | CodeWay 2015](https://reader034.vdocument.in/reader034/viewer/2022051503/58897ccc1a28ab461b8b4e4d/html5/thumbnails/15.jpg)
CreditsTDD is popularized by Uncle Bob (real name Robert C. Martin)
![Page 16: TDD, the way to better software | Dan Ursu | CodeWay 2015](https://reader034.vdocument.in/reader034/viewer/2022051503/58897ccc1a28ab461b8b4e4d/html5/thumbnails/16.jpg)
TDD in live demo
![Page 17: TDD, the way to better software | Dan Ursu | CodeWay 2015](https://reader034.vdocument.in/reader034/viewer/2022051503/58897ccc1a28ab461b8b4e4d/html5/thumbnails/17.jpg)
I like those unit tests. Can i get a pair?
![Page 18: TDD, the way to better software | Dan Ursu | CodeWay 2015](https://reader034.vdocument.in/reader034/viewer/2022051503/58897ccc1a28ab461b8b4e4d/html5/thumbnails/18.jpg)
So how do you get to TRUST your tests?
Follow the 3 laws of TDD. Follow them always. If every line of code was written to make a failing unit test pass, you will trust
your tests.
![Page 19: TDD, the way to better software | Dan Ursu | CodeWay 2015](https://reader034.vdocument.in/reader034/viewer/2022051503/58897ccc1a28ab461b8b4e4d/html5/thumbnails/19.jpg)
Before making up your mind on to TDD
![Page 20: TDD, the way to better software | Dan Ursu | CodeWay 2015](https://reader034.vdocument.in/reader034/viewer/2022051503/58897ccc1a28ab461b8b4e4d/html5/thumbnails/20.jpg)
1. What would your life be if all your unit test would pass every minute or so?
![Page 21: TDD, the way to better software | Dan Ursu | CodeWay 2015](https://reader034.vdocument.in/reader034/viewer/2022051503/58897ccc1a28ab461b8b4e4d/html5/thumbnails/21.jpg)
2. Low level documentation. TDD generates a low level documentation so formal that it executes. It never gets old.
![Page 22: TDD, the way to better software | Dan Ursu | CodeWay 2015](https://reader034.vdocument.in/reader034/viewer/2022051503/58897ccc1a28ab461b8b4e4d/html5/thumbnails/22.jpg)
3. Since tests are written first they have a massive impact on your code. Writing tests first makes your code testable.
And another word for testable is decoupled.
You get a better design simply by writing your tests first.
![Page 23: TDD, the way to better software | Dan Ursu | CodeWay 2015](https://reader034.vdocument.in/reader034/viewer/2022051503/58897ccc1a28ab461b8b4e4d/html5/thumbnails/23.jpg)
So you get: 1. Reduced debug time 2. Complete and reliable documentation 3. Improved design
How much would you pay now for a suite of unit tests?
![Page 24: TDD, the way to better software | Dan Ursu | CodeWay 2015](https://reader034.vdocument.in/reader034/viewer/2022051503/58897ccc1a28ab461b8b4e4d/html5/thumbnails/24.jpg)
Eliminate fear of changing and cleaning
code.
![Page 25: TDD, the way to better software | Dan Ursu | CodeWay 2015](https://reader034.vdocument.in/reader034/viewer/2022051503/58897ccc1a28ab461b8b4e4d/html5/thumbnails/25.jpg)
Consider this:Perfectly designed systems with no tests
vs Bad design but fully covered with tests that
you trust
![Page 26: TDD, the way to better software | Dan Ursu | CodeWay 2015](https://reader034.vdocument.in/reader034/viewer/2022051503/58897ccc1a28ab461b8b4e4d/html5/thumbnails/26.jpg)
Objections to TDD
Q: My manager doesn’t allow me to use TDD.
![Page 27: TDD, the way to better software | Dan Ursu | CodeWay 2015](https://reader034.vdocument.in/reader034/viewer/2022051503/58897ccc1a28ab461b8b4e4d/html5/thumbnails/27.jpg)
Objections to TDD
Q: Who tests the tests?
![Page 28: TDD, the way to better software | Dan Ursu | CodeWay 2015](https://reader034.vdocument.in/reader034/viewer/2022051503/58897ccc1a28ab461b8b4e4d/html5/thumbnails/28.jpg)
Objections to TDD
Q: Why not write tests at last?
![Page 29: TDD, the way to better software | Dan Ursu | CodeWay 2015](https://reader034.vdocument.in/reader034/viewer/2022051503/58897ccc1a28ab461b8b4e4d/html5/thumbnails/29.jpg)
Objections to TDD
Q: I already have a project which was not developed using TDD, so i won’t write tests because it’s too late.
![Page 30: TDD, the way to better software | Dan Ursu | CodeWay 2015](https://reader034.vdocument.in/reader034/viewer/2022051503/58897ccc1a28ab461b8b4e4d/html5/thumbnails/30.jpg)
Objections to TDD
Q: I’m working on an application with a cool UI and nothing else. You don't want me to write tests for the UI do you?
![Page 31: TDD, the way to better software | Dan Ursu | CodeWay 2015](https://reader034.vdocument.in/reader034/viewer/2022051503/58897ccc1a28ab461b8b4e4d/html5/thumbnails/31.jpg)
Objections to TDD
Q: Testing individual methods or classes is hard. Why not test the code through the User Interface.
A: Because by the time you run your UI test, you would have written so much bad code, that a failing test can only confirm that human beings make mistakes. Testing individual methods
provides feedback much faster, when its not too late.
![Page 32: TDD, the way to better software | Dan Ursu | CodeWay 2015](https://reader034.vdocument.in/reader034/viewer/2022051503/58897ccc1a28ab461b8b4e4d/html5/thumbnails/32.jpg)
–Robert C. Martin
“It is irresponsible to ship a single line of code without executing it in a test ”
![Page 33: TDD, the way to better software | Dan Ursu | CodeWay 2015](https://reader034.vdocument.in/reader034/viewer/2022051503/58897ccc1a28ab461b8b4e4d/html5/thumbnails/33.jpg)
Conclusions
1. Code rots because we are afraid to clean it. 2. To keep a system clean we need to eliminate the fear 3. The only way to do that is by having a good suite of tests 4. In order to get the tests practice the 3 laws of TDD 5. TDD ties deeply into professionalism
![Page 34: TDD, the way to better software | Dan Ursu | CodeWay 2015](https://reader034.vdocument.in/reader034/viewer/2022051503/58897ccc1a28ab461b8b4e4d/html5/thumbnails/34.jpg)
More Information
Original TDD presentationhttps://cleancoders.com/episode/clean-code-episode-6-p1/show
17:00 - 18:00 Software architecture. Letting go of MVC by Ursu Dan