34
TDD, the way to better software by Ursu Dan Session 2/6 15:00 - 16:00 #Codeway Dan Ursu, iOS Developer YOPESO

TDD, the way to better software | Dan Ursu | CodeWay 2015

  • Upload
    yopeso

  • View
    337

  • Download
    2

Embed Size (px)

Citation preview

Page 1: TDD, the way to better software | Dan Ursu | CodeWay 2015

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

The Problem:

Have you ever told said the following things

Page 3: TDD, the way to better software | Dan Ursu | CodeWay 2015

“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

“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

“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

“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

“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

“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

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

So what is TDD?

Page 11: TDD, the way to better software | Dan Ursu | CodeWay 2015

Test driven development is a discipline that eliminates

fear of cleaning code.

Page 12: TDD, the way to better software | Dan Ursu | CodeWay 2015

DemoEliminate fear? What an interesting and fascinating idea.

Page 13: TDD, the way to better software | Dan Ursu | CodeWay 2015

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

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

CreditsTDD is popularized by Uncle Bob (real name Robert C. Martin)

Page 16: TDD, the way to better software | Dan Ursu | CodeWay 2015

TDD in live demo

Page 17: TDD, the way to better software | Dan Ursu | CodeWay 2015

I like those unit tests. Can i get a pair?

Page 18: TDD, the way to better software | Dan Ursu | CodeWay 2015

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

Before making up your mind on to TDD

Page 20: TDD, the way to better software | Dan Ursu | CodeWay 2015

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

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

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

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

Eliminate fear of changing and cleaning

code.

Page 25: TDD, the way to better software | Dan Ursu | CodeWay 2015

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

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

Objections to TDD

Q: Who tests the tests?

Page 28: TDD, the way to better software | Dan Ursu | CodeWay 2015

Objections to TDD

Q: Why not write tests at last?

Page 29: TDD, the way to better software | Dan Ursu | CodeWay 2015

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

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

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

–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

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

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