Rule 2. Bugs Are Contagious

There’s a truism of programming that the earlier you find a bug, the easier it will be to fix. That’s generally true…but I think it’s even more true to say that the later you find a bug, the more of a pain in the ass it will be to fix.

Once a bug exists, people will unintentionally write code that relies on that bug. Sometimes that shaky bit of bug-reliant code is nearby, in the same system as the bug. Sometimes it’s not nearby—maybe it’s downstream, in a bit of code that calls your system and depends on the incorrect results your bug causes. Or it’s upstream—a chunk of code that only works because the bug caused you to call it in a particular way.

This is a natural thing—it’s impossible to avoid. We notice things that go wrong, not things that go right. When things go wrong, we investigate to figure out why. But when things don’t go wrong, we don’t investigate. If your code works, or at least seems to work, then there’s a natural tendency to assume that it works in the way you think it works, when very often it works for reasons you’ve never imagined. And since you don’t investigate, you never discover the tangled set of circumstances that led to your code accidentally working.

That’s true for the code you write, and it’s true for the code other people write that calls your code. When you commit a bug to your team’s codebase, the codebase will slowly but inevitably accumulate other bits of code that rely on your bug. These hidden entanglements only ...

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.