How to Debugging
Debugging is a must-have skill for software engineers. Debugging process generally involves three stages: reproduce the bug, find root cause, fix & testing
Welcome to The Scalable, blog and newsletter to help you faster in becoming tech leader. Check our tech lead roadmaps.
Before we start, kindly to subscribe, follow me on Threads & Linkedin and share this content to your friends. Enjoy.
Quick Links:
Intro
In programming, "bugs" refer to errors or defects in a software program that cause it to behave in unexpected or unintended ways.
Writing perfect code without bugs is hard to achieve, and it’s inevitable to have bugs. That’s why debugging is a must-have skill for software engineers. Debugging is the process of identifying, analyzing, and removing bugs or defects from a software program.
While it's a must-have skill, many software engineers don't know how to debug. When they encounter a bug, they tend to resort to blind guessing, resulting in poor decision-making. Sometimes, they fail to fix the root cause, and even worse, it may lead to the generation of new bugs.
So, in today's issue, we are going to talk about the debugging process, which generally involves three stages:
Reproduce Bugs
Find the Root Cause
Fix & Test
Reproduce the Bugs
The first thing we need to do when we encounter bugs is to find out how to reproduce it. Reproducing the bugs means we need to understand step-by-step how to trigger the bugs to happen.
The purpose of reproduce bugs are to identify:
Trigger Point (on button clicked, on page load, endpoint hit, etc.)
Actual Behavior
Expected Behavior
If we are able to reproduce the bugs, it becomes easier to solve them and ensure whether they are really resolved or just partially fixed.
There are two types of bugs in terms of occurence:
Consistent bugs: These bugs occur every time we follow the reproduction steps, consistently producing the bugs.
Intermittent bugs: When we follow the reproduction steps, sometimes the bugs occur, and sometimes they do not.
In my experience, if we encounter an intermittent bug, the first thing we need to do is find the intermittent variable that might affect the bug's occurrence.
These variables could be related to hardware, software versions, network connectivity, the number of servers, the volume of traffic, and other specific settings.
If we still can't understand the intermittent variable, then just jump into the next step, which is finding the root cause and going back to it when we have another clue. But remember, skipping to find the intermittent variable will make finding the root cause harder.
Find the Root Cause
Note: If the bugs are super urgent, the first aid is to restart the service (or the part of the software) that is related to the bugs. We can skip finding the cause for later.
To find out the cause of the bug, what we need to do is isolate the problem, which means we need to narrow down which part of the software or even which line of code is causing the bug.
There are 2 starting points to find the root cause:
Read the Error Message
A good error message usually provides a clear explanation of why something is working unexpectedly. It also offers information about the specific part of the software that caused the bug. We can begin to find the root cause using this information or even try to fix the bug by searching online.
Start from the Trigger Point
In the first step, which is reproducing the bugs, one of the outcomes is that we know what triggers the bug to occur. For example, if the bug is triggered when a button is clicked, we can start by examining the function behind the button click.
The next step is to trace back and pinpoint the bug. I often use 3 approaches to isolate and find the specific part of the software that is causing the bugs:
Deduce from code
To start, we can thoroughly read the code and make educated guesses about which part of the code might be causing the issue. Utilize this approach when we have a strong understanding of the product and the technical aspects of the software.
Breakpoint
If deducing from the code not worked, we can use breakpoints to trace what is causing the error. The breakpoint feature allows developers to pause the execution of a program at a specific point in the code. When a breakpoint is set, the program will stop running when it reaches that line of code, allowing the developer to inspect the program's state, variable values, and the flow of execution at that particular moment. Some programming languages, especially the compiled type, usually provide the breakpoint feature.
Print statement in every line
Sometimes we can't use breakpoints, and in that case, I often use this approach. We can simply add print statements in every line of code that might be the cause of the error. Here's an example:
func f1() { print("f1 called") f2() print("f2 called") f3() print("f3 called") f4() print("f4 called") }
If the bug happens in
f3()
, then thef3 called
message will never be printed. So, we can isolate the problem and check thef3()
function to identify the cause.
Fix & Test
If we receive a good error message, then google it. We can simply "Copy & Paste the error message to Google" and review the top 5 search results. In my experience, both StackOverflow and GitHub issues are very helpful in such cases. The bug you encountered might have occurred to other people and has already been resolved
However, if we are unable to solve it by Googling, it's okay as long as we have identified the part of the software that causes the bug (this is actually the most challenging part). Once we find it, we can make the necessary changes to fix the bug.
The most crucial part is testing the solution. During the reproduction phase, we understand both the actual behavior and the expected behavior. When we make changes to fix the bug (whether following information from Google or fixing it manually), we need to observe the changes in behavior and ensure it works as expected. It's important to be cautious because the changes we make might introduce new bugs. So, test it thoroughly and carefully.
TL;DR
Picture tells 1000 words:
That’s it for today, thank you for reading! If you find it valuable, don’t forget to: