A Rhythm for Success: The TDD
Cycle – The Code
Agile
in a Flash by Jeff Langr and Tim Ottinger (card #44)
> Red
> Green
> Refactor
--
Of the agile developer practices,
TDD will change your coding approach most fundamentally. TDD is a rhythm that
helps you continually keep steady, forward progress. In the TDD cycle, you
produce enough test code to fail, produce enough code to make the test pass,
and then refactor before writing the next microtest. Red and green are the
colors shown by GUI test tools for failing and passing tests.
Red. Newbies
commonly make the mistake of not watching tests fail first. The red is
essential feedback that the tests are written correctly and recognized by the
test runner.
Green. Making the
test pass is the part that is obvious; the hard part is that you write only
enough code to make the test pass. No more! By limiting the amount of coding,
you force the tests to be more complete, you avoid the cost of an over-designed
system, and you learn how to incrementally grow behavior.
Refactor. While the test
is passing, before anyone can depend on its current implementation, clean it.
Rename a test. Improve readability. Extract an interface or a class. Unlike the
Green step, there are no limits on the amount of refactoring you can do per
step, as long as the tests always pass. Refactoring is critical to survival; it
is your main opportunity to ensure your system’s
design stays clean.
“All code is guilty until proven
innocent”
No comments:
Post a Comment