Number 21
April 17, 2024

The toughest bugs

I don’t want to brag or anything, but I’m pretty good at investigating bugs and finding their causes. I think a lot about this subject and consider it so important that I wrote the first Coding Sheet all about it.

A drawing of many camouflaged invertebrates.Some bugs are hard to find.

As you know, debugging takes a lot of work. Sometimes, we get lucky, figure out the cause almost immediately, and fix it promptly, but that’s the exception. It often takes a few hours, sometimes days and, occasionally, weeks.

There is one thing I observed very frequently: as the cause of a bug becomes harder and harder to find, the more likely it is that it will turn out to be something silly. I’ve spent whole days beating on a bug, turning it left and right, trying this and that, only to discover that I had missed a semicolon somewhere.

And I haven’t even talked about when I make a change, and the bug doesn’t go away, then I change something else, and the error is still there. Finally, after three or four rounds of changes, I realize that I had been making my changes in one directory and running my tests in another.

I always say that the silliest bugs are always the hardest to find because programmers look for smart errors, not stupid ones. We look for significant errors (off-by-one loops, mistaken calculations, or functions that produce unexpected values) but overlook simple typos like the semicolon I’d forgotten.

Sometimes, we are convinced that the bug must reside in a particular place in the program when, in reality, it is elsewhere. I had a few bugs where the code looked flawless, and the unit tests worked perfectly. In the end, it turned out that someone had misread the API documentation, and the test objects didn’t behave like the production objects.

I wrote all this so you won’t be ashamed when the bug that paralyzes you for a few days turns out to be utterly silly. This happens to all of us, no matter how experienced we are. Moreover, as we gain more experience and become more effective, the bugs that stump us will become increasingly stupid.

The illustration for this Coding Sheet comes from “Brehm’s animal life”.