Column
Living with old code
Old code invites two opposite urges, to delete what looks useless and to rewrite the whole thing. This column reads from finding out why the code is there and who relies on it, to keeping it changeable, to replacing it without a big rewrite.
It often starts with a piece that looks like it does nothing.
Chesterton's Fence
Code or rules whose origin you don't know look like clutter, which makes them tempting to delete.Keep in mind: Learn why it is there before you remove it, but set a deadline on the searchLearning why it is there often means asking the people who built it, and they may be gone.
Programming as Theory Building
A program's design lives in the heads of the people who built it, and the code and documents carry only part of it, so once those people are gone, changes tend to erode the design even with full documentation in hand.Keep in mind: Code stays maintainable only while someone can say why it is the way it isEven they could not list everything it is relied on for. That list is held by its users.
Hyrum's Law
With enough users, every observable behavior of a system, bugs included, is depended on.Keep in mind: Assume everything users can observe has been promisedNobody gets to wait until the code is understood again, because code still in use keeps being changed.
Lehman's Laws of Software Evolution
A program in real use must keep changing or grow less useful, and each change makes it more complex unless work is spent to simplify it.Keep in mind: Budget for simplifying, not just for changingSome of that complexity was taken on knowingly, by shipping before the problem was fully understood, and that part can be recorded as a loan.
Technical Debt
Code shipped before the team fully understands the problem charges extra on later changes to that code until someone rewrites it.Keep in mind: Record shortcuts as loans, and repay them where you change code mostPart of it can be paid back without waiting for a project of its own.
Boy Scout Rule
Code decays because cleanup tends to be deferred to a refactor that rarely happens.Keep in mind: Leave what you touch a little better, in the same diffWhen small repairs feel too slow, the other urge from the start takes over, and a rewrite tempts you to fix more than was broken.
Second-System Effect
After a first success, designers tend to pack the ideas they deferred into the second system.Keep in mind: In a rewrite, carry over the constraints, not the wish listA rewrite also gives up something the old system already has: it works.
Gall's Law
Complex systems that work usually grew from simple systems that worked; designed ones rarely do.Keep in mind: Distrust a design that has to work all at once to work at allIf a replacement has to grow from something that works, the old system can be that something.
Strangler Fig
Replacing a live system piece by piece around its edges is usually safer than replacing it all at once.Keep in mind: Make every step of a migration shippable, and plan the deletion up frontEach piece moved can break someone who relied on the old behaviour, and the people moving it see only what a test checks.
Beyoncé Rule
In a shared codebase, the behavior no test checks is the behavior that changes without warning.Keep in mind: If you depend on it, put a test on it







