As Pareto’s Law says: Software development is 20% of the time developing and 80% debugging.
Imagine how powerful you could become if you cut down the debugging time and make the debug process more efficient, finding the root cause of the problem in your code or understanding the workflow of your algorithm.
It would be great to sharpen this critical skill and become an exceptional developer to finding “unexpected features” a.k.a. bugs.
Debugging is more than putting some breakpoints and cross our fingers for the program to hit them.
How the “Java Debug” Works?
When a JVM is running a Java Debug session can be launched and communicate with the JVM through the JDWP protocol.
This JDWP (Java Debug Wire Protocol) describes the format of debugging information and requests between a debuggee and a debugger.
Here “a debugger” can be the most basic one, JDB (Java DeBugger) which is a simple command-line that provides an interface to inspection and debugging of a local or remote JVM.
Also, can be an IDE debugger, so your Intellij | Eclipse | Netbeans debugger is typical of your IDE, it provides a GUI to communicate with the JVM through the JVM TI (JVM Tool Interface), this is why every IDE debugger is quite different from other, even though they must implement the same interface.
So, the normal flow to debug it may looks like this:
- Set the breakpoints before the exception or the wrong workflow occurs.
- Set another Breakpoint after to make sure that everything’s fine.
- Lose your mind with what happens in the middle.
Let’s start with my favorite advanced technique, set your Breakpoint in the exception! not before nor after.
If you are getting some unchecked exception or an error in runtime, you should put a Java Exception Breakpoint and take a look into the exception code.
After the exception is launched, we can see inside of the code, and the state of the objects before the exception arises. You will reach the exact point where the exception is thrown.
Instead of doing every execution step and see how variable value changes, we can set a Watchpoint on the Class’s members, this debug point will show us when this member is accessed or modified.
You can set a Watchpoint, with a simple click in the left gutter area at a line where the member is declared.
This is very helpful when you know the conditions needed to hit the execution point, you should mark the checkbox for the condition in the Breakpoint properties and write a Boolean expression when this expression returns true the Breakpoint will hit.
This is an amazing tool, instead of putting a Breakpoint in the first line of the method and in the last or return statement, you should set a Method Breakpoint and watch the state of the program when the method enter or exit.
Even you could want to see a particular value of your program, but this can be a Boolean expression or a primitive value that one object in your code is holding.
To do this, select the piece of code that you want to watch, in this case wasresultSet.getString(“name”), this just because I want to see how the name of the state is changing.
This can help you when you dont have a class’s member to set a watchpoint, this can be seen as a temporary local variable watchpoint.
You can customize your Breakpoints sets, to do your debug process faster and efficient, try to explore your IDE and you will master it. You can have a full control of the JVM execution even when runtime exceptions have been thrown.
Have you any other technique? Share it in the comments!