Home > Java > javaTutorial > body text

Improved &#printf&# debugging

PHPz
Release: 2024-08-21 06:01:02
Original
693 people have browsed it

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.

Live templates

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.

Register method parameters

public static BufferedImage recolor(BufferedImage in, BufferedImage mask, int newColor) {

    // digite 'soutp' aqui, depois pressione Tab

    return null;
}
Copy after login

Generated code:

public static BufferedImage recolor(BufferedImage in, BufferedImage mask, int newColor) {

    System.out.println("in = " + in + ", mask = " + mask + ", newColor = " + newColor);

    return null;
}
Copy after login

Register values

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;
}
Copy after login

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;
}
Copy after login

Register method entries

public static BufferedImage recolor(BufferedImage in, BufferedImage mask, int newColor) {

    // digite 'soutm' aqui

    return null;
}
Copy after login

Generated code:

public static BufferedImage recolor(BufferedImage in, BufferedImage mask, int newColor) {

    System.out.println("ImageUtils.recolor");

    return null;
}
Copy after login

Registration breakpoints

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.

Depuração

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.

Depuração

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):

Depuração

You can even create custom groups for them:

Depuração

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.

Frequent event log

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.

Depuração

For such events, it may be useful to use the Pass count feature. You can access it in the Breakpoints.

dialog.

Depuração

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.

Summary

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:

  • Debugger.godMode() – Hack a JVM Application with the Debugger
  • Troubleshooting Debugger Slowness
  • Debugging Inactive Applications
  • What's Wrong with createDirectories()? - CPU Profile Guide

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!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template