Skip to content

Instantly share code, notes, and snippets.

@evolve2k
Last active December 16, 2015 21:29
Show Gist options
  • Save evolve2k/5500435 to your computer and use it in GitHub Desktop.
Save evolve2k/5500435 to your computer and use it in GitHub Desktop.
OO TDD Rules

Today's approach

RED PHASE (Transformation)

FAIL

  1. Write a failing test starting from the smallest simplist thing you have certainty about (inside out approach)
  2. Get the test to report the failed state that you EXPECT.

EXPECT

  1. Make the test pass using the simplist/stupidist Transformation possible.

PASS!

GREEN PHASE (Refactoring)

  1. Refactor out ALL duplication, taking only small steps that never go red.
  2. Refactor to meet all of Sandi and Kents rules.
  3. Start a new failing test only after all existing code passes and neither code nor tests contain any duplication.

Sandi Metz rules of practical OO design

  1. Your class can be no longer than 100 lines of code.
  2. Your methods can be no longer than five lines of code.
  3. You can pass no more than four parameters and you can’t just make it one big hash.
  4. When a call comes into your Rails controller, you can only instantiate one object to do whatever it is that needs to be done.
  5. You can break these rules if you can talk your pair into agreeing with you.

Kent Becks 4 rules of simple design

  1. Runs all the tests.
  2. Expresses every idea that we need to express.
  3. Says everything once and only once (Eliminate all duplication)
  4. Has no superfluous parts.

Corey Haines Guard Clause

  • 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)

1. ({}–>nil)

no code at all->code that employs nil

  • create something that just returns nill (the clean state)

2. (nil->constant)

  • Aninteger or empty string.
  • Examples: 1, 2, ""

3. (constant->constant+)

  • A simple constant to a more complex constant
  • Examples: "" -> "Adelaide"

4.(constant->scalar)

  • replacing a constant with a variable or an argument
  • Example: "Adelaide" -> city

5.(statement->statements)

  • adding more unconditional statements.
  • Example:

postal_address = "Adelaide SA 5000"
-> postal_address = "#{"Adelaide"} #{"SA"} #{"5000"}"

6. (unconditional->if)

  • 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?

7. (scalar->array)

  • Add an array where previously there was just a variable

8. (array->container)

  • Array to a Hash or Set(unique array)

9.(statement->recursion)

10. (if->while) # includes other looping structures

  • Invert if statement where useful eg. 5 > age becomes age < 5
  • Remove else statement
  • Convert into while statement

11.(expression->function)

  • 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"

12. (variable->assignment) replacing the value of a variable.

  • Replacing the value of a variable.

  • Setting a variable

  • Example:

    active_users = 10

Simplified List

  • 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment