I recently wasted a lot of time while attempting to use the amazing Dropzonejs image uploader. I left out a function call in an overloaded function and this broke every subsequent call. More annoyingly, I could not pinpoint the cause of the error as Dropzone was failing silently.
Close to giving up, I decided to give it one more attempt and went to my sandbox – I create a safe area in all my projects for doing ‘risky’ stuff. Starting afresh there, I was amazed to see that everything worked fine. A close comparison of the code samples revealed the bug’s origin – the left out function call. I double-checked to confirm this and that was all about it. Sadly, the documentation does not mention that a failure to call done() in the accept function will break the code silently.
Programmers usually spend a lot of time debugging and it can be a painful experience; some feel like tearing out their hair in exasperation, smashing their poor computers or even believing that their computers really ‘hate’ them! :)
Actually computers do not hate people and the operating system is not conjuring bugs – the most likely reason is buggy code. Here are a couple of tips on debugging; hopefully these will help to reduce time spent, frustration and annoyance levels.
The Inefficient Way of Debugging: Trial-by-Error
Random guesses? Print statements littered around? Fast fixes that ‘appear’ to work? Do these ring a bell? Welcome to the bad bad way of debugging. This approach is fraught with a gazillion issues. The worst being the potential introduction of new bugs into the codebase.
Lucky bug discoveries offer little learning opportunities and it might be difficult to reuse knowledge gained from such fixes in the future. Also, such run-of-the-mill fixes tend to be inelegant and stand out like a sore thumb when compared to earlier designs and architecture
The Efficient Way of Debugging
1. The very first thing is to create a list of the likely causes of the bug; this usually involves thinking deeply about loopholes and design flaws. Studying program error dumps and buggy behaviour might also help.
2. Go through the list while eliminating disproved hypotheses. Once a likely cause is found, a verification needs to be carried out; this can be as simple as proving that the bug appears only when that cause is activated.
3. Don’t just rush to write a fix – I bet you would not want; your ‘super-fix’ to mutate into a monster ‘bug’. Think deeply about the bug and what potential ripple effects a fix could have. Also think about how the proposed fix would work, how it blends into the system’s architecture and possible design impacts.
4. Yes, you got it!! Go ahead and squash that bug! Yuck – I dislike bugs.
5. Not finished yet, you need to go through your code armed with your newly acquired bug-terminating powers. Find and ruthlessly sanitize code likely to suffer from such bugs. Rinse and repeat – always leave code in a cleaner state than before.
Extreme Approaches: Tips for Difficult-to-resolve bugs
You might apply all the above and still be stuck in a rut; this might occur if you have extra-terrestrial bugs (quite popular in multi-threaded environments).
1. Total examination
The brute force approach is bound to work insha Allaah although it involves painstaking work. You go through the code thoroughly (a debugger should come in handy); this might involve examining all execution paths if possible and trying to pinpoint the issue. Hard work…
2. Start afresh
When everything fails, why not throw out the old code and write it again?
Some other tips
1. Timebox debug investigations
Do not spend 3
days hours trying to fix a bug if you can re-implement the same feature in 2 hours. If you are sure you can rewrite a bug-free version of some buggy code, go ahead with a rewrite and spare yourself the trouble. Time counts.
2. Learn to use debugging tools
Debuggers, memory examination tools and profilers. They will help point out to what might be causing the issue. I once had a really really nasty bug while using jQueryUI, the bug only showed up on a particular browser and only when the page was accessed over the local network; otherwise all was fine.
I eventually had to use a profiler after several failed debugging attempts, I then discovered that a function was being called several hundred times. Bug case solved!
3. Discuss with a buddy
Find a programmer / buddy to discuss with, discussing your ideas with someone will help you to find design gaps and flawed assumptions; you might be surprised that you get the error spot on without any contribution from your buddy.
Now go ahead and eliminate some bugs :).
11 thoughts on “The Art of Debugging”
Nice one bro. Painfully true how we feel so awful when our codes have bugs and yet we cant discover them.
I hope everyone learns to make use of this writeup, including myself
Thank you very much John; I am glad you like it.
I appreciate the way you offer analytic rigor to the debugging process. Often, stepping back, taking a deep breath, and approaching the problem without that sense of urgency and fear leads to much more productive and thoughtful debugging sessions. I also love rubber ducking.
Without TDD, as you describe in step 3, we are quite prone to seeing that bug again. Thanks for helping us all remember some of the fundamentals of software development – thinking! :)
Thanks a lot Chris.
Yes you are right however I do not use TDD; I only tried it out once and didn’t really fancy it.
The issue itself was the documentation of the library – it didn’t mention that a call in a particular function was compulsory. I doubt if TDD would have helped much in this scenario too.
I appreciate your feedback and excellent contribution – thanks a lot.
I kinda agree with you. TDD is a great tool in a programmer toolbox however, many times in practice, using TDD or even automated testing isn’t the best thing to do.
For example, for relatively small projects it is better to spend your time focusing on architecture, code quality and tools.
And this is mostly the case for many open source projects, since they have limited manpower it is often more appropriate to cut down on some TDD and automated testing.
Fortunately, writing property-based tests (e.g: Quickcheck), in many cases, takes less time to write and deliver more value (if used correctly).
I feel you bro. In any coding environment debugging can quickly become a nightmare. Thanks for taking the time to share after all the time it took for you to find your bug(s).
Thanks a lot Gbenga! :D
Don’t Spend 3 hours trying to fix a bug while you can re-implement in 2 hours. Nice one that really caught my fancy. Thanks boss
Glad you liked it! Thanks a lot Yusuf!!