8 effective debugging strategies to find and fix bugs like a pro
Improve your problem-solving skills with these practical debugging tips. (4 min)
Bugs are inevitable.
Being able to quickly and effectively find and fix them is crucial for being a trusted software engineer.
If you master debugging, you can save time, reduce frustration, and improve your code quality.
If you do it right, you’ll become a more effective developer.
In today’s article, I’ll share 8 effective debugging strategies you can apply immediately.
1. Reproduce the error
💡 Write step-by-step instructions to make reproducing the bug easy for yourself and others.
Consistently reproducing the bug is fundamental to understanding and fixing it.
Without clear reproduction steps, it will be very hard to find the root cause.
2. Reproduce the error quickly
💡 Set up your code and project to load right where the bug occurs for faster debugging.
By configuring your environment to jump straight to the problematic area, you can reduce the time between finding and fixing the bug.
This allows for rapid iteration and testing of potential fixes, improving the overall debugging experience.
3. Isolate the error
💡 Narrow down the issue by using print statements or commenting out code.
Large codebases can make finding a bug like finding a needle in a haystack.
Isolating the error by deleting unrelated code or adding print statements can significantly help you focus on the exact problem.
4. Do rubber duck
💡 Explain the expectation by going through what you expect to happen to clarify your thought process and understanding of the code.
Rubber duck debugging involves explaining your code, line by line, to an object or a person.
This practice helps you to understand the code, check your expectation of what’s going on and reveal inconsistencies or errors in your thinking.
It’s a simple but very powerful technique to gain new insights.
5. Do a binary search
💡 Divide your code into working and non-working parts to quickly find the bug and minimize the scope
When you face a bug in a large codebase, a binary search approach can significantly help to narrow down the problematic area.
By gradually splitting and testing the code into parts to see where the bug persists, you can reduce the scope of the problem code.
6. Use a debugger
💡 Learn debugging tools to find issues faster.
Debuggers are invaluable tools that provide real-time insights into the program’s execution.
You can set breakpoints, inspect variables, and see the exact flow of your program.
This way you can spot anomalies and unexpected behaviours.
7. Write down what you’ve tried
💡 Help others help you by listing what you’ve tried and your ideas.
By keeping a list of what you’ve tried, you can prevent yourself from doing the same thing and expecting a different result.
It is also helpful when you ping a colleague to help you with the problem because you can easily share your progress so far.
8. Take a break
💡 Step away from your laptop to get a fresh perspective and relax the mind.
Taking a short break allows your mind to rest and can lead to breakthroughs upon returning.
I can’t count the times when a solution to a problem popped up in my head while taking a walk, being in the toilet, or taking a shower.
Many bugs have been solved in a 15min walk.
⭐ TL;DR
Reproduce the error
Reproduce the error quickly
Isolate the error
Do rubber duck
Do a binary search
Use a debugger
Write down what you’ve tried
Take a break
👋 Let’s connect
You can find me on LinkedIn or Twitter.
I share daily practical tips to level up your skills and become a better engineer.
Thank you for being a great supporter, reader, and for your help in growing to 12.7K+ subscribers this week 🙏
This newsletter is funded by paid subscriptions from readers like yourself.
If you aren’t already, consider becoming a paid subscriber to receive the full experience!
Think of it as buying me a coffee twice a month, with the bonus that you also get all my templates and products for FREE.
You can also hit the like ❤️ button at the bottom to help support me or share this with a friend to get referral rewards. It helps me a lot! 🙏
This is an exceptional article! I often struggle to take breaks. I feel if I could spend a little more time I would make some progress.
One thing I'm trying more often is adding AI to the mix. But Binary Search is still king!
Debugging is one of the best skills you can develop.
Thanks for the shoutout, Petar.