Transformation Priority Premise: TDD Test Order Matters

Preview:

DESCRIPTION

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.

Citation preview

Transformation Priority Premise: TDD Test Order Matters

Mike Clement@mdclement

mike@softwareontheside.comhttp://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

Software Craftsmanship!

• Participate in a local Software Craftsmanship group wherever home is for you.

My Contact Info

• @mdclement• mike@softwareontheside.com• http://blog.softwareontheside.com• http://agilecodegames.com• Utah Software Craftsmanship Group– http://utahsc.org– @utahsc