Read in other languages: English Español 中文
One of the most common debugging techniques, 'printf' debugging is particularly popular because most people learn it intuitively when writing their first programs.
'printf' debugging is very accessible because you don't need any special tools for it. When dealing with your first bugs, even before you know what a debugger is, it naturally comes to mind to make your program print variables step by step, so that you can follow the execution in the console.
Despite being one of the most basic debugging techniques, experienced developers also use it a lot. It can help you investigate any type of problem, such as suboptimal routines, inconsistent state, multithreading issues, and more.
As I already mentioned, this technique does not require you to use any special tools, like an IDE. However, if you are using one, it can make you even more efficient at recording program state.
Note: This article shows features of IntelliJ IDEA. Similar features may or may not be available in other IDEs. If you are using a different tool, consider checking its documentation to see if these features are present there as well.
IntelliJ IDEA provides live templates for the most common debug logging patterns. To use a live template for debug logging, type the corresponding command and press Tab. IntelliJ IDEA will generate the print statement and insert it at the cursor.
Let's look at some examples.
public static BufferedImage recolor(BufferedImage in, BufferedImage mask, int newColor) { // digite 'soutp' aqui, depois pressione Tab return null; }
Generated code:
public static BufferedImage recolor(BufferedImage in, BufferedImage mask, int newColor) { System.out.println("in = " + in + ", mask = " + mask + ", newColor = " + newColor); return null; }
public static double coolMethod(double parameter) { double a = Math.random(); double b = Math.random(); // digite 'soutv' aqui, pressione Tab e então selecione o valor return a * b * parameter; }
Generated code:
public static double coolMethod(double parameter) { double a = Math.random(); double b = Math.random(); System.out.println("b = " + b); return a * b * parameter; }
public static BufferedImage recolor(BufferedImage in, BufferedImage mask, int newColor) { // digite 'soutm' aqui return null; }
Generated code:
public static BufferedImage recolor(BufferedImage in, BufferedImage mask, int newColor) { System.out.println("ImageUtils.recolor"); return null; }
One of the disadvantages of debugging with print statements is that they introduce the overhead of manual management. You can't turn them on and off quickly, and you definitely don't want to accidentally compromise and run them in production.
For this reason, if you need to log something for debugging purposes, I would recommend using logging breakpoints as they are much easier to manage.
To set a logging breakpoint, hold down Shift, then click the tab. Unlike a regular breakpoint, it does not suspend program execution, but prints to the console instead.
By default, it is a message indicating that the program has reached this line. You can also use the options next to the Evaluate and log checkbox in the breakpoint settings if you prefer to log the current stack trace or the result of a custom expression.
Note: Be careful with registration expressions. Evaluating those that cause side effects can be a source of new bugs or unexpected behaviors. Additionally, when used in hot code, they can significantly slow down your program.
When logging breakpoints become numerous, you can track and manage them in the Breakpoints dialog (Run | View Breakpoints):
You can even create custom groups for them:
This will help you manage your breakpoints in a centralized way. For example, you can create a group related to a specific bug and save it for later. When the issue is resolved, disable the group. This way, if the problem reappears, you won't have to recreate everything from scratch. You simply reactivate the group again.
For events that occur a lot during program execution, recording each individual event may be overpriced. Not only does this flood the console with messages, but too much I/O interaction can significantly slow down the debug session.
For such events, it may be useful to use the Pass count feature. You can access it in the Breakpoints.
dialog.Once you have set Pass count to a specific value, the corresponding breakpoint will only trigger every nth hit, ensuring that the record does not become a nuisance.
Whether you're inserting print statements or setting logging breakpoints for debugging, modern tools have features to improve your debugging experience. With this post, I wanted to make sure you are aware of these little tricks that make the whole process more enjoyable.
If you are interested in more articles related to debugging and profiling, check out some of my other articles:
Happy debugging!
The above is the detailed content of Improved printf debugging. For more information, please follow other related articles on the PHP Chinese website!