Legacy Rescue
You'll take a tangle of working code nobody dares touch, put a safety net of tests under it, and reshape it in small steps without changing what it does.
About
Every program older than a year has a part everyone routes around: the 400-line function, the module with no tests and three authors. Rewriting it from scratch is the trap, because the tangle holds a hundred fixes nobody wrote down. The craft is to pin down what the code does now with tests, then change its shape in steps so small that the suite stays green after every one. Working Effectively with Legacy Code by Michael Feathers is the book. Pick at least 2,000 lines that are still in use, whether that's your own old project, a friend's, a work module you have permission to change, or the worst file of an open source project you use. Behaviour has to come out identical, so this is the opposite of Ten Times Faster: no new features and no speedups, only shape.
At a glance
- Per requirement
- 50 XP
- Completion bonus
- 600 XP
- Requirements
- 6
- Takes
- A season
- Skill level
- Advanced (years of practice)
- Cost
- Free
- Supplies
- Nothing special
- Where
- At home
- Other people
- On your own
Requirements
-
1
Pick a module of at least 2,000 lines with no tests that's still in use, and find its longest function and its most tangled dependency. Count the lines with cloc or wc -l, find the longest function, and find the global, singleton or database handle that everything reaches for. Then write down the three things you're most afraid of breaking, because that list decides where the tests go first. Take a screenshot of the line count, the longest function's length and the three fears. Photo
-
2
Write characterization tests that record what the code does now, not what it should do, until they cover the paths you'll touch. Run the code on real inputs and freeze the outputs into golden files. When a result looks wrong, write the test anyway and note the bug for later, because this step builds a net, not a judgement. Get measured coverage above 70 percent on the part you'll change. Take a screenshot of the suite passing and the coverage report. Photo
-
3
Break one dependency so the code can run in a test without the database, the network or the clock. Introduce a seam: pass the dependency in as an argument, or wrap it in a small interface you can fake. This is the step that makes the rest possible and the one where most people stall for a week. Take a screenshot of the seam before and after, with the test that now runs in isolation. Photo
-
4
Refactor in at least twenty commits, each one small, with the suite green at every commit. Extract a function, rename, inline, move a thing to where it's used. One kind of change per commit, and if the suite goes red, revert the commit rather than fixing forward. Take a screenshot of git log with the twenty commits and the green check or test run on each. Photo
-
5
Get the longest function under 40 lines and the module's line count down, with every test still green. Measured, not felt. The dead code you find on the way can go, and it's usually the easiest win. If a function won't come apart, it's hiding two jobs, so name them and split along that line. Take a screenshot of the new longest function's length and the before and after line counts. Photo
-
6
Show that behaviour didn't change, then fix the bugs you froze and write a page for the next person. Diff the outputs on the recorded inputs byte for byte before you touch any noted bug. Then fix the bugs from the second requirement one commit each, with the test updated to the right answer. The page says where the seams are, what you'd still not touch, and why. Take a screenshot of the empty diff and the page. Photo