Transformation Priority Premise: TDD Test Order Matters
Mike Clement@mdclement
[email protected]://blog.softwareontheside.com
http://agilecodegames.com
XP Simple Design
• Passes all tests• Clear, expressive, consistent• No duplication•Minimal
TDD Review
TDD
• Red• Green• Refactor
Ways to get Green
• Fake it• Obvious
implementation• Triangulation
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.
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.
Rule of Perpetual
Inconsequence
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.
WORD WRAPA Fork in the Road
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.
LET’S CODE!
?
"I can't seem to get this test to pass without writing a lot
of untested code."
They are the wrong tests, because you could not figure
out how to pass them.
"[W]hen faced with a problem you do not
understand, do any part of it you do understand, then
look at it again."
BACK UP!
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.
WHAT ABOUT SOLUTION QUALITY?Sorting Algorithms
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.
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.
ABSOLUTE PRIORITY PREMISE“Cousin” Micah
Action Items!
• Read the original post http://cleancoder.posterous.com/the-transformation-priority-premise
• Sorting: http://cleancoder.posterous.com/transformation-priority-and-sorting
• Fibonacci: http://cleancoder.posterous.com/fib-the-transformation-priority-premise
• Flash Card: http://cleancoder.posterous.com/flash-tpp• Uncle Bob on TPP: https://vimeo.com/60561303• “Cousin” Micah on APP: https://vimeo.com/57851350 and
https://vimeo.com/59265614• Experiment!
Software Craftsmanship!
• Participate in a local Software Craftsmanship group wherever home is for you.
My Contact Info
• @mdclement• [email protected]• http://blog.softwareontheside.com• http://agilecodegames.com• Utah Software Craftsmanship Group– http://utahsc.org– @utahsc