When coding using TDD, do you often run into a point when you have to “implement the whole thing”? The sequence of tests that we write significantly impacts where are code ends up! We’ll explore Uncle Bob’s Transformation Priority Premise, looking at the transformations that code goes through and how our tests can drive the code in different directions.
Transformation Priority Premise: TDD Test Order Matters
1. Transformation Priority Premise:
TDD Test Order Matters
Mike Clement
@mdclement
mike@softwareontheside.com
http://blog.softwareontheside.com
http://agilecodegames.com
2. XP Simple Design
• Passes all tests
• Clear, expressive, consistent
• No duplication
• Minimal
3. TDD Review
TDD
• Red
• Green
• Refactor
Ways to get Green
• Fake it
• Obvious
implementation
• Triangulation
4.
5.
6.
7. Example Transformations
• ({}–>nil) no code at all->code that employs nil
• (nil->constant)
• (variable->assignment) replacing the value of
a variable.
• (statement->statements) adding more
unconditional statements.
8. Uncle Bob’s Three Laws of TDD
• You are not allowed to write any production
code unless it is to make a failing unit test
pass.
• You are not allowed to write any more of a
unit test than is sufficient to fail; and
compilation failures are failures.
• You are not allowed to write any more
production code than is sufficient to pass the
one failing unit test.
10. Proposed Transformations
• ({}–>nil) no code at all->code that employs nil
• (nil->constant)
• (constant->constant+) a simple constant to a more complex constant
• (constant->scalar) replacing a constant with a variable or an argument
• (statement->statements) adding more unconditional statements.
• (unconditional->if) splitting the execution path
• (scalar->array)
• (array->container)
• (statement->recursion)
• (if->while)
• (expression->function) replacing an expression with a function or
algorithm
• (variable->assignment) replacing the value of a variable.
12. Problem definition
• function named Wrap that takes two
arguments, a string, and a column number
• The function returns the string, but with line
breaks ('n') inserted at just the right places
to make sure that no line is longer than the
column number.
• Try to break lines at word boundaries.
15. "I can't seem to get this test
to pass without writing a lot
of untested code."
16. They are the wrong tests,
because you could not figure
out how to pass them.
17. "[W]hen faced with a
problem you do not
understand, do any part of it
you do understand, then
look at it again."
18.
19. The Premise Applied
• Prefer higher priority transformations.
• Choose tests that can be passed with higher
priority transformations.
• When a low priority transformation seems
required, backtrack to see if there is a simpler
test to pass.
21. The Premise Applied
• Prefer higher priority transformations.
• Choose tests that can be passed with higher
priority transformations.
• When a low priority transformation seems
required, backtrack to see if there is a simpler
test to pass.
22. Proposed Prioritized Transformations
• ({}–>nil) no code at all->code that employs nil
• (nil->constant)
• (constant->constant+) a simple constant to a more complex constant
• (constant->scalar) replacing a constant with a variable or an argument
• (statement->statements) adding more unconditional statements.
• (unconditional->if) splitting the execution path
• (scalar->array)
• (array->container)
• (statement->recursion)
• (if->while)
• (expression->function) replacing an expression with a function or
algorithm
• (variable->assignment) replacing the value of a variable.