Refactoring: Changing Structure Without Changing Behaviour · seed 1 · A4, ink-friendly. The answer key prints on its own page for grown-ups.

Tidying code without breaking it

Computing · Software Engineering · ages 19-21
Name ______________________   Date ____________
  1. What do you run between steps?

    • The tests
    • A farewell party
    • The installer
  2. What stays the same during refactoring?

    • Observable behavior
    • File names
    • Line counts
  3. Swapping a whole block at once counts as refactoring.

    Circle one:   True   False

  4. How do you rename and move a class safely?

    • Move first, rename never
    • Rename everything at once silently
    • Rename first, move next, running throughout
  5. How do you split the forty-line billing function?

    • Rewrite it from memory
    • Pull one named helper at a time and test each
    • Delete two jobs to shorten it
  6. A step turns red. What now?

    • Ship it anyway
    • Undo that one tiny step
    • Rewrite the tests to fit
  7. You knew behavior held since tests stayed green at each step.

    Circle one:   True   False

  8. A teammate bundles a bug fix into a tidy-up and calls it refactoring. What is the flaw?

    • Tidy code needs no tests
    • Fixes should never ship
    • Behavior changed, so tests cannot vouch for sameness
LightMySky · lightmysky.comW1-mt_HHZufOyTg4-s1

Answer key

For grown-ups. Fold this page away before handing over the rest.

Tidying code without breaking it W1-mt_HHZufOyTg4-s1

  1. The tests · Green tests prove behavior held.
  2. Observable behavior · Structure moves; behavior holds still.
  3. False · That is a rewrite, not small steps.
  4. Rename first, move next, running throughout · Separate steps keep each one undoable.
  5. Pull one named helper at a time and test each · One pull plus one test run keeps it safe.
  6. Undo that one tiny step · Tiny steps make undo cheap.
  7. True · Green between steps is the evidence.
  8. Behavior changed, so tests cannot vouch for sameness · Mixed behavior breaks the sameness proof.
Worksheet · LightMySky