,

The ultimate guide for debugging code

I have been coding for over 10 years now and have seen and fixed so many bugs in code. Sometimes, I can understand the root cause and the scenario to replicate the bug by just looking at the issue description. Sometimes, I have to dig into the logs and the code to understand and reproduce the issue.

Let me share some tips that I have learned from my own experience with debugging

Observe the bug

Spend some time to read and understand the bug before jumping straight into the code. Sometimes, you might be given the exact error message. Then you just need to go to the place where the error message got printed in your code. By looking at the logs and the code, you can understand the issue so easily.

Sometimes, understanding the issue is not much simpler. Observe the issue description or the screenshot attached, they might have some clue about the issue.

Try to gather as many information as you can from the issue description

Reproduce that bug

Before start working on your code changes by just looking at or hearing the bug, try to reproduce that bug. This step is the most important to ensure that the bug is properly identified. Without the proper knowledge of the issue, you will not be able to fix it. Gaining proper knowledge about the issue before fixing it is the most crucial step in debugging

If you know the steps on how to reproduce the bug, you are good to go. If you are not aware of the steps, start digging into logs. You will definitely get some clue. By looking at the logs, you can trace the actions happened in your application. If you follow the same actions when reproducing, you can get more close to reproduce the issue.

Logs combined with the code knowledge is a great way to start reproducing that issue

Try to reproduce at least 2 times to ensure that is the proper scenario to replicate the issue

Debug code

Add logs for debugging

Once you are able to reproduce the issue, start analysing it. Add logs and print statements wherever you want. Instead of adding random meaningless strings, always prefer meaningful words or sentences which can help even others when they are looking at your code. Logs should be added in a way to tell the story of what has happened in the application. It shouldn’t be just 1,2,3 or a,b,c to understand the flow. Make the logs more meaningful.

Adding logs and printing statements will help you to figure out the root cause of the issue

Print stack traces

Try printing stack traces to identify the block which caused this issue. Stack traces will get printed in a different format based on your programming language and the framework which you are working. Learn to understand your stack traces.

Printing stack traces will be really helpful in reducing the time for figuring out the root cause of the issue

Compare with a working code

if you have access to the working version of the code, add logs there also and compare both of them. This will show the exact point where it got changed and the issue happened.

Comparing logs and stack traces of working code with the buggy code helps to identify the exact place of the issue

Search engines are your friends

If you are getting the error which is out of your web application, you have so many options to choose. If it is some error related to the programming language or the framework you are using, feel free to use search engines and stack overflow for posting your queries.

It the issue is related to some third party which you are using in your application, search for the answers in their forums. Still not getting answers? Contact them directly explaining your issue.

Think out of the box

Don’t blame yourself for writing buggy code whenever issue happens. Your code might not be the reason for some issues. Some updates in the device or browser, custom configurations in the device, issues in server or network, different OS, different versions, version update of some plugins you used in your code can also cause the issue. Sometimes, it might not be a direct issue but a complex scenario like a race condition

Start analysing by looking at the code. But don’t stick to the thought that the issue can happen only from the buggy code.

Assumptions and guesses are most welcomed

Start making assumptions and guesses about your issues. The most important thing is that your assumptions should co-ordinate with the logs and your code. Ask yourself “Can this be the root cause of the issue?” If you are able to prove your assumption with the logs and your code, you are good to proceed further. Always reproduce your bug after figuring out the root cause.

Assumptions should match the logs and the code and you should be able to reproduce the bug with that assumption

Commenting is not only for comments

Try commenting code where you suspect the buggy code. If your code starts working without that block, you have reached the error block. Feel free to comment the block wherever you suspect there is an error. Don’t forget to uncomment those blocks before you commit the code.

Commenting code helps us to speed up the analysis of the issue

Take a break

Don’t overstress yourself for finding it out. Take a break by moving away from your screen or moving away from that issue and taking over some easier tasks. Believe me, taking breaks will help you come up with the ideas and the perfect solution

Taking breaks in between helps your mind to get refreshed

Ask for help

Don’t hesitate to ask for help. It doesn’t mean that you are not intelligent. It doesn’t mean that you are not ready to work. It doesn’t mean that you are lazy or not having the patience to look into the issue. It just means that you need a helping hand to figure out the issue.

Discussing the issue with others really helps to speak your mind out and invites some thoughts and ideas about your issue

Share your experience

Discuss your issue, the way you have handled that and the fix with your friends or colleagues. This will be helpful for knowledge sharing and you will not get stuck with the issue which your friend has faced, because you know it already.

Sharing the success of debugging is a feeling of accomplishment and gives you self confidence

For beginners,

Beginners are most likely to suffer and I am here to help you. If you are struggling with an issue and you have a small piece of code which you are comfortable to share, then post it in comments. I will try my best to debug it and will guide you on how I did that for you

Stay Happy! Stay Awesome!!

Share

2 comments on “The ultimate guide for debugging code”

  1. Shabarish
    November 14, 2024 at 10:18 pm

    Simple and best

    1. November 14, 2024 at 7:09 am

      Thank you 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *