TDD, the way to better software by Ursu DanSession 2/6 15:00 - 16:00
#Codeway
Dan Ursu, iOS Developer YOPESO
The Problem:
Have you ever told said the following things
“After the next release let’s put aside 3 days for
refactoring” Manager’s thoughts:
“Again with these arguments about clean
code?”
“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?”
“I fixed one bug and 10 others popped up in
another place”Manager’s thoughts: “I am
starting to think you are unprofessional”
“I’m not sure how the code works because the colleague who wrote it is on a vacation”
Manager’s thoughts: “Really unprofessional”
“I don’t clean this piece of ugly code because i’m afraid it will break things”
Manager’s thoughts: “You sound highly
incompetent”
“I’ll finish the task and clean the code
afterwards” Manager’s thoughts:
“Yet you always seem to set aside time for refactoring”
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.
So what is TDD?
Test driven development is a discipline that eliminates
fear of cleaning code.
DemoEliminate fear? What an interesting and fascinating idea.
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.
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
CreditsTDD is popularized by Uncle Bob (real name Robert C. Martin)
TDD in live demo
I like those unit tests. Can i get a pair?
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.
Before making up your mind on to TDD
1. What would your life be if all your unit test would pass every minute or so?
2. Low level documentation. TDD generates a low level documentation so formal that it executes. It never gets old.
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.
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?
Eliminate fear of changing and cleaning
code.
Consider this:Perfectly designed systems with no tests
vs Bad design but fully covered with tests that
you trust
Objections to TDD
Q: My manager doesn’t allow me to use TDD.
Objections to TDD
Q: Who tests the tests?
Objections to TDD
Q: Why not write tests at last?
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.
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?
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.
–Robert C. Martin
“It is irresponsible to ship a single line of code without executing it in a test ”
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
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