12. Dirty Code
Big functions
A function should do one thing.
“One thing. Just one thing. You stick to
that and the rest don't mean sh*t.”
Curly, City Slickers
35. 1. Write a test that fails
1. Write one unit test
addNumber_test.js
2. Run the test and verify the failure
36. 2. Make the test pass
1. Write just enough code to make failing test
pass. addNumber_test.js
addNumber.js
2. Run the test and verify the success
37. 3. Clean up code
If able, clean up Code and Unit Tests
“Leave the campground cleaner than you found it.”
Boy Scouts of America
38. 1. Write a test that fails (Round 2)
1. Write one unit test
addNumber_test.js
2. Run the test and verify the failure
39. 2. Make the test pass (Round 2)
1. Write just enough code to make failing
test pass. addNumber_test.js
addNumber.js
2. Run the test and verify the success
40. 3. Clean up code (Round 2)
It’s tough to clean code this simple, so
we’re done.
addNumber_test.js
addNumber.js
41. Dirty Code
Zero Unit Tests
Low Test Coverage
Stupid Bugs
Fear Of Breaking Anything
42. Clean Code
Lotsa Unit Tests
High Test Coverage
Stupid Bugs (but a whole lot less of them)
No Fear Of Breaking Anything
43. Clean Code
Having to test a little
code at a time forced
me to write smaller functions
with lower coupling
that did one thing.
45. High Test Coverage
The code follows the tests.
It makes sense that test coverage would be high.
46. High Test Coverage
I no longer have to rely on the functional
testers to tell me my code works. I know
it works.
For my F5 tells me so.
47. No fear of breaking anything
This one’s my favorite, because it
means I can refactor mercilessly.
Every time I make a code change, I run
the tests. They tell me immediately
and loudly if I broke anything.
48. No fear of breaking anything
Like Patrick Bateman
in American Psycho, I
laugh maniacally while
I carve up bodies of
code.
50. Other Reasons that TDD Rules
Closes the Feedback Loop
“The act of writing a unit test is more an act of design
than of verification. It is also more an act of
documentation than of verification. The act of writing a
unit test closes a remarkable number of feedback loops,
the least of which is the one pertaining to verification
of function.”
Robert “Uncle Bob” Martin
Agile Software Development
( You know code works after
30 seconds, not 10 minutes. )
51. It can be quite a struggle at first.
Downright painful.