On 'Transformation Priority Premise'
A few days ago I read an article by Uncle Bob entitled Transformation Priority Premise. This post summarizes it's core idea of Transformations and the Transformation Priority Premise.
What is a Transformation?
A transformation, as the counterpart of refactoring, is a small code change/operation with a behavioral change as a result.
There are different kinds of Transformations, for example replacing a constant with a scalar like a variable or an argument,
denoted as (constant -> scalar)
.
We as developers apply transformations to turn red (failing) tests into green (passing) tests.
What is Transformation Priority?
Uncle Bob defines the following transformations, where the order of a transformation represents its priority:
({} -> nil)
no code at all , returnnil
ornull
(nil -> constant)
replacenil/null
with a constant(constant -> constant+)
replace a single constant with several other constants(constant -> scalar)
replace a constant with a scalar (variable or argument)(statement -> statements)
replace a single statement with several other statements(unconditional -> if)
introcude a new execution path(scalar -> array)
replace a scalar with an array(array -> container)
replace an array with a collection container(statement -> tail-recursion)
replace a statement with a tail-recursive call(if -> while)
replace an if-statement with a while-loop(statement -> non-tail-recursion)
replace a statement a non-tail-recursive call(expression -> function)
replace an expression with a function(variable -> assignment)
reassign the value of a variable(case)
add a new execution path to an existing split of the execution path (e.g. introduce anelse
branch or a newcase
statement in an existingswitch
statement)
The Impasse Problem
The impasse problem is when you have to rewrite your whole algorithm in your implementation code in order to make the next test from red to green.
What is the Transformation Priority Premise?
Uncle Bob states the Transformation Priority Premise the following way:
A different sequence of tests allows the algorithm to come together in the stepwise fashion that TDDers prefer. How can you choose the right sequence? [...] if you choose the tests and implementations that employ transformations that are higher on the list, you will avoid the impasse.
From my point of view, the most difficult part of this is to figure out which "next test" is the one that allows you to apply a higher transformation instead of a lower transformation in the list. I have to admit that so far I have never really made any thoughts about the fact that writing tests in a different order could result in an impasse dilemma or not. Uncle Bob presents a Case Study: Word Wrap with two different test order sequences, one of which leads to an impasse, the other not. I think it could worth it paying attention to tests and their corresponding transformations by practicing this with some Katas. I will give you an update about it at a later time.