As software engineers, debugging is an integral part of our jobs. You may have times as a Software Engineer when you are more concerned about fixing bugs than developing novel features.
A debugging process can be divided into two categories:
Throughout this blog post, I'll share some of the debugging practices I adopted and have now become part of my debugging process.
It is essential to remember that if a bug exists, even if you are not experiencing it, it implies that something is definitely off with the code. This could be logic, a dependency issue, or a missed edge case.
Gathering all available historical information about the issue is the first step in debugging. This typically allows you to identify whether the problem is persistent or whether it has never been fixed. There may be a log, error note, screenshot, and perhaps some old message exchanges.
SCM also allows you to view all of the file's previous modifications. Different Git extensions in the IDE can help with this process. Going back in time and discovering even the smallest indication might occasionally inspire an idea when you are absolutely baffled as to how the bug is happening.
Sometimes, half of the issues may be solved by just reading the code and tracing back all the functions.
In my experience, after locating the API Endpoint where the problem occurred, I look for the method connected to it and then attempt to visualize the flow by creating a mental model of the code.
Once you comprehend the code, you may add breakpoints (eg: binding.pry in Rails) to the code to verify your comprehension.
When working on a complicated problem, speaking aloud to yourself might occasionally disclose blind spots.
I try putting it down in the manner of what I would advise someone to do if they were in a similar circumstance if thinking aloud doesn't help.
Simply write down everything you know about the bug so that when you see it all written out in front of you, your mind will start to fill in the blanks.
One of the most popular debugging techniques is this one. It entails inserting logs into the code at various locations—basically, before and after every function call.
Adding logs can help you understand the state of a variable whose value is changing across many functions.
As a result of the discovery of the defect, we can conclude that the code is flawed. In huge code bases, it is essential to reproduce problems that only appear under a certain set of circumstances.
In order to understand the set of conditions, all the data points can be gathered as you analyze the database values. Try finding a pattern across all the users for whom the error has been occurring. You can then experiment with a different set of conditions to correct the issue.
To further aid in the debugging of edge cases, the code may be significantly modified to include early returns, conditional returns, etc.
Forgetting what you know about the product is a crucial step in trying to recreate something. Typically, when we have been working on something for a very long time, we are familiar with all the tricks and know what will work and what won't. Due to this, the bug's reproduction is biased. Consider your product from the user's perspective and create the most absurd scenario possible.
When you encounter a bug, don't always think of the issue to be some big code change or rewrite. Start small.
Check for null values, any before action validations, if else conditions.
When I originally started, I believed that significant modifications would be necessary, but over time, I've learned that it's better to focus on the little aspects first.
You can start by reviewing the unit tests for that specific piece of code if you are entirely perplexed by the various edge situations the function handles.
There is a possibility that you will not always have to debug something that you have worked on. When this happens, it can be helpful to speak to someone with a broader understanding.
You can bounce your ideas for solving when working on a critical flow, if modifying something, with the working team to ensure you haven't missed any edge cases.
Using tools like HoneyBadger, and Sentry can help you keep a log of the errors. You can add them to all your APIs & critical workflows.
Other than alerting, you can have a logging system, where you can monitor the routes that are being called in actual production.
The most crucial step is the last: once you've identified the cause of the problem, remember to record it for the future you.
Lastly, if none of the above steps work, take a break, come back and start again.
PS: Keep in mind that debugging is not magic; rather, it is a sequential process that you get better at as you do it more frequently.
Originally published here.