clean-code-successive-refinement
Successive Refinement
Clean code isn't written in one pass. You write a rough draft that works, then refine it. The skill is knowing when to stop and clean, and how to clean without ever breaking the system.
The story in brief
A command-line argument parser started out tidy when it supported only boolean flags. Adding string arguments and then integer arguments made it rot quickly. It accumulated parallel maps per type, type-specific parsing branches, scattered error codes, and more conditionals everywhere for each new type. The code still worked, but each new type made it noticeably worse, and more types were coming. So the author stopped adding features and refactored. The end result has one small class per argument type, all sharing a marshaling interface, so adding a new type means adding one class plus one line to register it.
When to stop and clean
Stop adding features when:
- Each new feature requires touching several places in parallel (another map, another branch, another error code).
- You can see that the next one or two planned features will make the structure much worse.
- You're tempted to "just bulldoze through and clean up later." Later rarely comes, and cleaning a mess gets more expensive the longer it waits.
Messy code that works isn't finished. Getting it working is only the first half of the job. Rot is cheap to fix when it's fresh and expensive once it has dug in, so don't let it start.