I’m just in the middle of a two-day bug hunt, and I’m poised to “make the kill” tonight when I get a chance to fully run the app, so I thought I would share my thoughts on two different styles of catching bugs: Stalking, and Laying Traps.
A lot of developers I’ve worked with prefer to catch a bug red-handed by stepping through the code with a debugging tool. I compare this approach to consciously stalking your “prey”, specifically chasing it down and remaining fully focussed throughout the process.
When you find the bug, you have to be careful to capture the information you require before it (possibly) gets lost, and you must be careful not to step past it or you might end up back at square one. This can be a frustrating experience, especially if the bug is intermittent or generally hard to reproduce.
My preferred method is to lay some traps. I add logging (or, more usually, highly temporary System.out.println calls) such that there is no way the bug can execute without revealing the exact pathway it took through the code. These are like traps that you can return to at your leisure and check if you’ve caught anything.
I prefer this for a couple of reasons: 1) I don’t have to be “on the ball” when the code is run, I can just pick up the output whenever I’m ready and – if I’ve done it right – it will definitely reveal how (or at least where) things are going wrong, and 2) This is the only way to reliably debug a multi-threaded application.
So, I’ve got my traps set for tonight. This is probably my favourite part of catching bugs – with everything set up and ready to snare the little bastard that’s been annoying me for a while. The actual fix is never as much fun.