Debugging is a tool mostly use when an application is not behaving as expected, and every developer should learn how to use it. However, it requires a lot of time to master it.
Fortunately, Java provides some tools included in JDK (jstack…), and there are also third party tools, such as JMeter, that can help us. There is also a very good integration with IDEs such as IntelliJ or Eclipse, so it is very important to know how to use the debugging features of our favorite IDE.
In order to speed up the learning process, we can follow these guidelines:
- Do not be afraid of reading code, specially if the code is from others. We should go as deep as we can to understand what is really happening under the hood.
- Do not blame JDK or libraries. Never assume that Java is broken, because probabilities are always against you. 99% of the times the bug is on something you coded.
- Make assumptions, but do not trust yourself, because we might create blind spots.
- Walk away from the problem and come back when you are fresh (work out, go to bed, meet your friends…).
- Do not program by coincidence, or in other words, do not try stuff without knowing what you are doing.
- Google is your best coding friend.
- Go to JDK javadoc when you are not sure about how a Java feature works.
- Use proper log levels (info, warn, error, debug).
- Use breakpoints to have a full view of the program on a particular point of our application. Conditions on breakpoints are also very useful when we are debugging a loop with thousands of values.
- Use tools such as JMeter for load testing or jstack to capture thread dumps.
- Have a remote debugger for your applications deployed on dev and pre-production environments.
- If we are not able to find the source of the issue by any of the previous guidelines; firstly, start eliminating code; secondly, check your environment; and thirdly, check libraries.
Debugging is an art hard to master, and will definitely require a few years of experience, so start building your debugging skills as soon as possible!