Rule 8. Code That Isn’t Running Doesn’t Work

Any big codebase, especially one that’s been around a while, has dead ends in it: lines of code, or functions, or subsystems, that aren’t getting exercised any more. Presumably they were added for a reason—at one point, those lines of code were getting called. Things change, though, and at some point whatever code was calling no longer needs to. The calls stop coming. The code has been orphaned.

Sometimes the orphaning is obvious, like a function that isn’t called from anywhere else in the program. If your language and toolchain are robust enough, you might even get a warning about this particular kind of dead code.

More typically, the orphaned code isn’t that obvious. It might still be mechanical, like a virtual method defined in a base class that is never called for a particular derived class. Static analysis can’t pick up that sort of thing up. Or there’s code written to handle some special edge case in a function, a scenario that requires special conditional handling. At some point, things change and that scenario can no longer occur. The edge case code is still sitting there, but it’s never called.

The closer you look at any mature codebase, the more orphaned code you find—like enumerated values that are defined but never used, or special-case code targeted at an old version of a library you haven’t used for years.

This sort of evolution of code is both natural and inevitable. The codebase is a river, winding its way back and forth ...

Get The Rules of Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.