Skip to main content
๐Ÿ“œ WAYPOINT LESSON

Refactoring Under Guard

โญโญ intermediateโณ 22 min read๐Ÿ“ Lesson 139 of 265

Behavior-preserving change in small verified steps โ€” with the test suite as the safety net, not an afterthought.

Refactoring has one definition

To refactor is to change the structure of code without changing its behavior.

Not cleanup. Not "improvements while I'm in here". If behavior changes, it is a bug fix or a feature โ€” a different activity with different review needs. Mixing the two in one change is how regressions hide.

The harness: tests first, always

You cannot safely refactor unverified code, so the workflow is:

  1. Freeze behavior with tests. Run the code as-is, pin the interesting inputs and outputs in tests (including edge cases โ€” they are where refactors bleed).
  2. One move at a time. Extract a method, run the tests. Rename, run. Each step compiles and stays green.
  3. Commit-sized steps. Small green steps compose; one big-bang rewrite does not.

If you cannot write a failing-then-passing test around the behavior you are about to restructure, you are not refactoring โ€” you are gambling.

The starter catalog

| Move | When | |---|---| | Extract method | block has a nameable single purpose | | Replace magic number with constant | 0.20m โ†’ GoldDiscount | | Guard clause | nested if only guards a degenerate case | | Decompose conditional | a long condition gets a well-named method | | Introduce parameter object | the same 4 values travel together everywhere |

// decompose conditional
public bool CanBulkDiscount(Order o) =>
    o.Qty >= BulkThreshold && o.Tier != Tier.Vip;   // named, testable

When NOT to refactor

  • The code is about to be deleted โ€” refactoring it is negative work.
  • You have no tests and no time to write them โ€” do the smallest safe change; schedule the refactor.
  • The behavior itself is wrong โ€” fix the bug first, then clean the shape. Refactoring a wrong behavior just makes the wrong thing prettier.

Refactoring is how a codebase stays maintainable while it grows โ€” the next modules (patterns, architecture) assume you can restructure safely.

โšก Now practice

Ready to Code
Clean Code Practice: Guards, Totals, ReportsAccumulate validation errors with guard clauses, build a money total from named pieces, and debug a report generator that remembers too much.
3 challenges ยท ยท ~30 min