7 Debugging Tips to help your engineering future
- Posted by Marwan Mohamed
- On September 25, 2023
What is debugging and what debugging tips do we have for you?
Debugging is an essential skill for any software engineer. When a program does not work as expected or a bug is reported, that’s when you need to start debugging. Debugging is the process of finding and resolving bugs in a program. In this post, we will discuss strategies and tips to provide insights on how to have an effective debugging process.
Debugging vs Testing:
Testing and debugging are somehow correlated. Testing purpose is to discover any wrong behavior, then debugging helps the developer to identify the problem and fix it.
How to Start debugging?
The first piece of advice is to understand the problem. Most of the time, you are trying to find a bug in code you did not write. So, you need to start by gaining context for the problem. You can begin by reading the error message carefully and reviewing the code. Often, the bug you are facing may not have an error or thrown exception to help you understand the problem. That’s when you need to apply the following steps:
Print/ Log statements:
Using print statements can be very helpful while debugging for the following reasons:
a) Print statements can be useful as they can help you trace the values of specific variables throughout code blocks.
b) They help in understanding the flow of the desired behavior. For example, you can use print statements like log.info(“Inside function2”) or log.info(“Finished function 2”) to determine if a specific function is reached
Example:
Breakpoints:
IDEs have a powerful tool that we should use while debugging as it allows us to add “Breakpoints” which resumes the executions in the code block you choose and gives you the following benefits:
a) Allows you Inspect all variables in the current code block.
b) Allows modifying variables values in while reproducing the bug to view how the output can change.
c) Allows stepping through the code line by line, which helps you trace the flow of code execution better than reading the code.
Example of variables view in the IDE:
Reproduce the Bug:
Reproducing the bug can help you make sure if it’s valid. If you tried to reproduce the bug in local/dev environment and it was not reproducible this should mean that the bug is environment related not code related.
Use Binary Search:
Imagine that we can divide our code into two parts, comment out a part and run the other and keep repeating until the error occurs, this method helps you narrow your search.
Searching:
Googling an error message should be the first step you take if you do not know what it means. Searching or reading Stack Overflow questions gives you the benefit of learning from others who have encountered similar issues and may have spent hours, days, or even months resolving them. This approach can save you a significant amount of time.
a) It is the simplest thing you can do if you encounter an error message you do not understand – simply Google it. Most likely, you will find an answer on Stack Overflow, which has a large community of developers.
Take a break:
Debugging can be mentally challenging. When you are stuck on a problem for hours, this can lead to frustration. It is advised to take a break and engage in any activity, such as going for a walk or having a meal. Sometimes, this helps to provide a fresh perspective and identify a solution. Finally, don’t hesitate to ask for help from colleagues. Collaborating with others can provide new insights and help save time.
Conclusion:
Debugging is an important skill for any software engineer. We discussed some tips and methodologies which can enhance your debugging process. Remember to have patience and rest your perspective if you are out of solutions. Please feel free to browse our other blogs for more content.