- Write a failing test starting from the smallest simplist thing you have certainty about (inside out approach)
- Get the test to report the failed state that you EXPECT.
- Make the test pass using the simplist/stupidist Transformation possible.
- Refactor out ALL duplication, taking only small steps that never go red.
- Refactor to meet all of Sandi and Kents rules.
- Start a new failing test only after all existing code passes and neither code nor tests contain any duplication.
- Your class can be no longer than 100 lines of code.
- Your methods can be no longer than five lines of code.
- You can pass no more than four parameters and you can’t just make it one big hash.
- When a call comes into your Rails controller, you can only instantiate one object to do whatever it is that needs to be done.
- You can break these rules if you can talk your pair into agreeing with you.
- Runs all the tests.
- Expresses every idea that we need to express.
- Says everything once and only once (Eliminate all duplication)
- Has no superfluous parts.
- Guard clause, a technique to get back to a clean transformation state
- use pattern 'return if' to reset code back to nil state.
- Example:
"SA" -> return "SA" if city == "Adelaide" # now returns nill in other cases.
Transformation Priority Premise (TPP)
no code at all->code that employs nil
- create something that just returns nill (the clean state)
- Aninteger or empty string.
- Examples: 1, 2, ""
- A simple constant to a more complex constant
- Examples: "" -> "Adelaide"
- replacing a constant with a variable or an argument
- Example: "Adelaide" -> city
- adding more unconditional statements.
- Example:
postal_address = "Adelaide SA 5000"
->
postal_address = "#{"Adelaide"} #{"SA"} #{"5000"}"
- splitting the execution path
- unconditional just means starting somewhere where there is NO if statement.
- Examples: if, unless, ternary if (?:) return "Good luck on your Trip!" -> return "Good luck on your Trip!" unless staying_home?
- Add an array where previously there was just a variable
- Array to a Hash or Set(unique array)
- Invert if statement where useful eg. 5 > age becomes age < 5
- Remove else statement
- Convert into while statement
- replacing an expression with a function or algorithm
- calling a function or method
- expression: just a line of code >> puts "You are #{(currentyear - birthyear)} years old"
- function: calling code from elsewhere >> puts "You are #{(age(birthyear)} years old"
-
Replacing the value of a variable.
-
Setting a variable
-
Example:
active_users = 10
- constant : a value
- scalar : a local binding, or variable
- invocation : calling a function/method
- conditional : if/switch/case/cond
- loop : applies to for loops as well
- assignment : replacing the value of a variable