The subtle Art of Debugging
By: Akshay Saxena
Tags: Debugging, Education, Learning, Programming

“Sometimes debugging is more art than debugging”.

Being said that, I think it will be much easier for all of us to understand what to expect out of this article.

From my experience of joining a new organizations, working on existing applications, creating new features on top of that application and every now and then fixing numerous amount of bugs in the existing code, I have realised that it is very important to understand how an application works, inside-out, to do your job in a much better way and also without stress.

Let’s face the truth, most teams expect you to understand everything with very few questions(minimal spoon feeding) when you join a new organisation and you have a good number of experience on your resume.

Such situations call for a friend whom you can trust any time, any day with a lot of inputs and helpful outputs, your debugger.

Debugging is not only a way to figure out the cause of an error in the application but also a way to understand how a piece of code or a module works and in turn understanding the application.

But just like knowing what to google is not enough, you also need to know how to google, you need to know what you are getting into before debugging!

Let’s discuss a few points which I adhere to while debug and benefit from:

Know the framework

It is always useful to know the framework upon which the application is built. It gives you a much better understanding of the basic flow of the application, a clearer sense of how resources are fetched and used up in the application, how the different modules, be it internal or third-party, interacts with one another and how different layers are placed and behave within the application.

Frameworks are based upon strict guidelines and rules and hence it makes it easier for you to know from where to begin your debugging task.

Learn to debug

Before you can start carving a stone, sketching a portrait or as simple as creating a Lego house, you need to know your tools, you need to know which brush will give you a perfect stroke for a sketching a face or hair, or what knife and chisel you need to carve a particular stone and shape it accordingly.

Similarly, before debugging also you need to understand your debugger and the functionalities that come along with it. I personally prefer Idea Intellij IDE to develop and it has a very user-friendly and insightful debugger shipped with it, which can connect to various application types, VMs or remotes.

‘Idea’ gives the ability to move forward or backwards in code, evaluate methods, variables in real-time and also how it will behave after fixing it, jump into or out of code snippet and also supports debugging of java 8 Streams.

It is always better to use a debugger over putting console logs in your application. ;)

Take a step back

In a scenario where you are debugging a bug or trying to understand the flow of the application, you move forward in the application. We often don’t bother to understand how did we reach the point where we started the debugging.

It gives you a better understanding of code and also how the module that you’re debugging is functioning along with all the other parts of the application. It is also a neat way to figure out the entry points of the module.

I prefer using Idea Intellij for debugging/developing and it’s ‘drop frame’ functionality is a great tool for taking a step back in your code.

Don’t forget Stack trace/console logs.

The very start of figuring out the cause of any error is the stack trace generated by the failing application. It is very important to read and understand the generated stack trace and take out key points from it such as the exception type, the file and line number at which error occurred, and other information which gives you an insight of what went wrong.

Debugging is not only a process carried when you encounter an error, but also anticipating the erroneous situations and taking precautions in advance is part of the whole process. Hence, It is also very important to place logs within the application where the occurrence of an exception is possible, this makes it easy to know the reason for the error and easier to find and fix it.

Look for patterns

It is always not necessary that your stack trace will be self-explanatory(like null pointers) and the console log could also be very vague at times. Recently working with a senior of mine I realised the importance of looking for patterns. If you follow the above two mentioned points, you will be familiar with your code and how the application works, then looking for similar patterns in the erroneous results makes it very easy for you to find bugs and fix them.

Conclusion

Lastly, I would say that debugging is an ongoing process, don’t look at it as a last resort when you encounter an exception or error and to find and fix it you call for your debugger.

Debugging is a way to understand any application, how it works and flows, how the different layers are integrated together and function as an application, and how can you make it better. So take out your tools and start debugging!