Debugging can be simply defined as a process of identifying, isolation, and resolving issues within software code. With debugging, we can ensure that the functionality, reliability, and performance are intact. The below image represents a tweet by Catalin Pit showcasing the breakdown of the software engineer while coding.
It shows that debugging consists of 40% of the total time spent. Making debugging the most important and time-consuming step in the overall coding process.
While coding is undoubtedly a creative endeavor, debugging is the practical application of problem-solving skills. As we see debugging is a challenging task, with methods and techniques we can simplify and quickly debug software code. This will increase your efficiency and productivity as a software developer.
In this article, we delve into the essential debugging techniques that every developer should master to solve errors more efficiently. These techniques can be used regardless of any programming language or framework.
So let’s get started.
1. Print Statement
One of the most simple and used techniques for debugging is using the print statement. By placing print statements through their code, you can look into the execution flow of the program. You can also verify whether the particular block of code is executing or not by checking the print statement. Most of the time, I place print statements in my code to verify the execution of the code when I am unsure whether it is executing or not.
Here are some ways to implement print statements:
- Error Handling: You can print detailed error messages when an exception occurs in your code. This can give more context about the error.
- Resource Usage: You can print information about the resources such as memory, and CPU to identify the bottlenecks or inefficiencies in code.
- Function Entry and Exit: Automatically print messages when functions are entered and exited to trace the flow of execution through your code.
Here is a code example showcasing it:
function functionName(parameter1, parameter2) {
// Debugging print statement
console.log("Entering functionName");
// Debugging print statement
console.log("Value of parameter1:", parameter1);
console.log("Value of parameter2:", parameter2);
// Your code logic goes here
// Debugging print statement
console.log("Exiting functionName");
}
2. Search Online Resources
Searching online about the error will provide more information about the error. Google will help you find the error-related web page from the internet. Most of the time you will find the StackOverflow website where someone already tried to find the solution to the error. In the discussion, you will find a variety of solutions. Most of the time one of those solutions will help you in solving the error.
There can be docs of the provided language or framework that can also help you in solving the error. Searching for errors online will give more content and provide solutions to your error.
Here are some of the other places where you can find the solutions:
- GitHub Issues: If you're using open-source software or libraries, check the project's GitHub repository for any reported issues or discussions related to your problem.
- Forum or Communities: Websites like Reddit, and forums such as Reddit’s r/learnprogramming can help you in finding solutions.
- Blogs and Tutorials: There are a variety of blogs and tutorials on the internet that discuss problems or issues that often occur while using a particular library, programming language, or framework. You can find blogs from dev.to or hashnode quite helpful.
3. Code Review
If you are ever stuck with a problem for a long time, you can take somebody’s other eye to check the code. Most of the time as a developer, we overlook something very simple while debugging our code. Other colleagues or peers can uncover bugs or potential issues. You can read Effective Techniques for Code Review in Software Engineering article by me to get more ideas about better code review.
You can combine this technique with Rubber Duck debugging to find errors more easily. Rubber Duck Debugging is an in which you explain your code and problem-solving process to someone else. You can gain new insights and solutions that you haven’t considered before.
Additionally, You can use code review tools to find errors related to security or performance. You can find tools specific to programming language.
4. Logging
Logging is a systematic method of recording events, actions, or messages that occur during the execution of the program. It helps in monitoring the application and tracing down any bug that occurs in the application. We place logging statements throughout the code in place such as functions, class, before the return statement, exception, etc. This logging statement gives out relevant information that includes timestamps, and log levels such as DEBUG, INFO, WARNING, ERROR, and contextual information.
Logging will help in tracing bugs and errors in a large application and also will provide more context to handle the error.
Implement logging to extract the following information:
- Performance Logging: Integrate logging to track the performance of data processing tasks in a big data analytics pipeline
- Security Logging: Monitor security-related events through logging.
- Error Logging: As mentioned above we can add information about errors such as message, timestamp, and place of error to the log.
5. Using ChatGPT
After the launch of ChatGPT, I more often rely on chatGPT for getting solutions to my problem. The model is quite versed in understanding code and providing solutions to it. It gives a variety of possible solutions to your problem. It will also elaborate the solutions so that you can better understand the solution.
It also provides coding output as a solution. You can use it directly in your project.
6. Contact Support
If you are using a tool that provides support then you should use that to get the solution. In many applications, I use Supabase to build my backend. If I encounter any problem that is kind of new or no solution is working for me then I raise a support ticket. Supabase’s free tier is also quite good with support. I usually get replies within 24 hours and each time their solution works for me.
You can also raise issues on their GitHub if it is open source. You can get information directly from maintainers regarding the problem.
You can use this method when the tool is new and not a lot of resources are online.
Conclusion
Mastering debugging techniques is an essential skill for every developer. It not only helps in resolving issues efficiently but also enhances the overall coding experience. By implementing the discussed techniques such as using print statements, searching online resources, conducting code reviews, leveraging logging, utilizing AI assistants like ChatGPT, and contacting support when needed, developers can navigate through complex problems with ease.
I hope this has helped you understand different techniques that are commonly used to debug the code. Thanks for reading the article.
Top comments (2)
You missed one of the most important debugging technique using a debugger and actually debugging/stepping through your code. With a debugger you can set break points, step through your code and see the state of variables and how they change as you step through. Some debuggers will allow you to execute arbitrary code in a console, which can allow you to verify assumptions, an try out possible solutions without restarting your app. If you are working in an IDE using a debugger a lot of them will allow you to debug into decompiled source code and some will go further and will download and link the source so that you can debug through it. A wise Engineer once told a younger me to use the source, meaning debug through it because you can see what your code is sending in, and how your code interacts with it. You can also get a better appreciation for the libraries you use, and on a rare occasion find an actual bug in the library and report that, or if so inclined fix it.
Surprisingly you talk about debugging without mentioning debuggers. Using GDB or LLDB is a skill that will save you tons of hours of debugging. If you then decide to try time travel debugging (rr and UDB being the two main players) you will go from days and weeks of debugging to mere hours, if not minutes.
sourceware.org/gdb/
replay.io/
Https://undo.io/