Debugging the Code – Hands-on practical tips for debugging efficiently

Software Debugging is the basic skill for any programmer in the world. Debugging is the process of executing the code interactively one line at a time to understand what is happening in the code flow. Debugging is also the process of detecting the source of the problem and fixing any potential bugs in the software code.

We debug the code mostly in below scenarios:

  • To understand the large codebase
  • To fix any defects in the application
  • When developing the new functionality in the existing application
  • When developing a new application or feature from the scratch

I have been debugging the code for a whole lot of reasons. Below are some of my learning and experiences to debug the software codes effectively :

  • Learn about the correct behavior of an application – Before you start working on a defect, first check whether the defect was working earlier or not with previous build/releases. In other words, be clear about the correct behavior of the application. It is always a good idea to check whether the functionality was working in previous releases. It will save hours of your time trying to debug the problem in your current code.
  • Reproduce the problem – The very first step to debug is to reproduce the problem. It is the way to be sure that the issue/defect exists in the application.
  • Check the error logs – Once the problem is reproduced, you can start with the error logs. Read the error messages, stack trace, exception messages, and debug prints. You can try to understand what those error/debug messages are, which tells where the potential problem could be in the application.
  • Sprinkle lots of print statements – To understand the CPP/Java/Python codebase, you can sprinkle a lot of print statements to see what’s happening on the ground.
  • Use the debugging tools – Various great software IDEs come with in-built debuggers like the eclipse, gdb, visual studio which provides a convenient and interactive way to debug the code. Attaching a debugger to the application and setting the breakpoint at multiple lines of source code is another way to debug the codebase.
    For instance, in an eclipse debugger, you can create a debug configuration by going to eclipse configuration – create the debug configuration(eclipse/java/remote) – I will share more on the remote debugging later.
  • Backtrack the code – After creating the debug configuration, you can run the application with the debugger, it will hit the breakpoint in the code. Then, you can start to backtrack the code from where the first breakpoint hits.
  • Look around – To debug further, click on the all the threads to go backward and backtrack the code where the code followed the stack trace.
  • Check all the references of the method – When the breakpoint first hits, then you can check all the references of that method and put the breakpoint everywhere, so that when you start debugging again, it may hit the breakpoint before that method as well.
  • Learn about the debugger capabilities: You can use various debugger capabilities like debugging shell, outlines, drop-to-frame. Use extensively debug shell to see the value of variable, objects. Check the outline for variable values. Use the “drop-to-frame” feature to go back to the code flow and start debugging an application from start, without starting the debugger all over again.
  • Narrow down the scope – If you are not sure where the problem is, you can comment out lots of code to narrow down the scope of the problem. Gradually, you have to move from a larger area to a smaller area to zero-in on the problem.
  • Fix one problem at a time – You don’t need to solve all problems at once, go one-by-one. Pick one problem and try to solve that first, then you can move on to the next problem.
  • Read the code a lot – I heard one statement from my previous colleague which stuck in my mind. He said, “Read the code as you read English language”. That means you should be comfortable in reading the codes. Read it a lot of times, slowly it will start making sense. At the outset, everyone puzzles out seeing the large codebase.
  • Run your code often – Even you make small changes, make sure to run your code every time to ensure that everything is working fine. You should not wait to write a thousand lines of code before running them all together.
  • Look for help – If doing all of the above and still you are not able to find the root cause of the issue, you can look for the help by googling the error message, or discussing with your colleagues. It is not a bad idea to ask for help, but make sure you do your due diligence first and have exhausted all the debugging tips yourself.

A few things you need to keep in mind while making the code changes to fix the defect:

  • Make sure the check for null as much as you can when writing code. It will save you from the null pointer exception umpteen times.
  • Make sure to close all the resources that are opened earlier:

Since Java doesn’t have destructors that predictably deallocate the resources. Resource management in Java is done manually. One of the ways is to use try-finally construct. Keep the code in finally block as short as possible and that should not throw any exception.

File file = new File(“filename”);

try {
} finally {

  • If you are working with the UI team, make sure your back-end development changes are tested well with existing/new UI changes. You can’t check-in your back-end code without doing the integration testing with the UI code, especially if the whole back-end functionality has to be shown via the UI.

Bonus tips – Use of lots of logging messages while writing the code, it will help you when debugging the application.

That’s all for the debugging tips for now. Happy debugging !!



Leave a Reply

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

Post comment