Build & submit taskBetaintermediate

Refactor Legacy Code Without Breaking It: Characterization Tests and a Golden Master

Take a gnarly, working, untested toll-fee module and drive the professional legacy-code loop in your own Claude Code: pin the current behavior with characterization tests first, then refactor the structure with behavior locked, and prove nothing observable changed. The local self-check carries golden outputs frozen from the original algorithm, so behavior drift is caught to the byte, and it only writes your proof-of-work evidence when the tests, the golden match, and the measured structure improvement are all real.

1.3 hrs

Est. time

3

Outcomes

5

Rubric criteria

65%

Pass score

What you'll learn

Skills you'll have real reps in after shipping this.

The scenario

You have inherited tollbooth/fees.py: one ~120 line function that computes a monthly toll bill from a list of trips. It handles vehicle classes, weekday peak windows, an overnight rate, a frequent-driver discount, a minimum-toll floor, and a monthly cap, all tangled into five levels of nesting with duplicated rate lookups, magic numbers (0.85, 27, "SUV"), a dead branch, and single-letter names. It works. Finance reconciles against its output every month. There are no tests, and the last three engineers who opened the file closed it again.

This task is that situation, made safe to practice. You drive Claude Code through the legacy-code workflow the pros use: first write characterization tests (tests that record what the code does today, before you change its structure), run them green against the untouched code, then refactor with that net in place. You do not get to redefine correct: check.py embeds golden outputs computed once from the original algorithm and replays all 8 shipped fixtures through your code, so a one-cent drift fails the check even if your own tests still pass.

Your role

You are the engineer who finally makes this module maintainable without changing a single billed cent. Your deliverable is the refactored module, the characterization tests you pinned it with, machine-generated evidence that all 8 golden outputs still match and that the structure measurably improved, and a short write-up of the workflow, packaged as a single submission.

Start the task to unlock the full brief

You'll get the step-by-step requirements, setup commands, the 5-criterion grading rubric, tips, and the ability to submit your solution for instant AI grading.

Free to start · submit when you're ready

Learning resources

Practice the legacy-code refactor loop in Claude Code, hands-on

Every real codebase has one: the module that works, that money or uptime depends on, and that nobody dares to touch because there are no tests. This task hands you exactly that file (a 120 line toll-billing tangle) and makes you drive the professional loop in your own Claude Code: pin the current behavior with characterization tests, refactor the structure with the tests as a safety net, and prove to the byte that nothing observable changed. The proof is mechanical: a local oracle replays 8 fixtures against golden outputs frozen from the original algorithm, measures your structure with ast, and generates evidence only when both hold. You come away able to make 'refactor safely with an AI agent' a checkable claim on real legacy code, plus a repeatable prompt pattern for mapping undocumented rules before changing them.

Frequently asked questions