clean-code-legacy-refactoring

Installation
SKILL.md

Refactoring Legacy Code: First Make It Work, Then Make It Right

The case study takes a respectable, widely used open-source date class and cleans it up methodically. The lesson isn't "this code was bad." It's that even good inherited code benefits from a disciplined two-phase pass, and that the first phase usually turns up real bugs.

Phase 1: First, make it work

The existing tests passed, but they covered only about half the code. Before changing anything:

  1. Measure coverage with a coverage tool. Look for unexecuted regions and for functions that are never called at all (dead functions).
  2. Write your own independent test suite that exercises everything that could break. In the case study, this raised statement coverage to roughly 90%.
  3. Expect to find bugs. Some new tests failed, revealing actual defects: off-by-one errors in date boundaries, wrong behavior near edge days, and a function that never returned the right value. Write tests that pin the correct behavior, then fix the code.
  4. Look at coverage patterns. Code that's never executed by any test, or executed only on the failing path, points to the cause.
  5. Test exhaustively around bugs. Bugs cluster. When you find one, test that function thoroughly.
  6. Mark tests for unclear requirements as questions, for example skipped with a note, instead of guessing.

Only when the suite is thorough and green do you start restructuring.

Installs
1
First Seen
1 day ago