devxlogo

Controlling Debugging Information

Controlling Debugging Information

Debugging is a core part of application development in Java. Programmers tend to use

 System.out.println & System.err.println

in the code everywhere, which becomes uncontrolled as the program evolves. This becomes a problem when moving the application to the production environment and controlling the debugging information generated by the class.

A straightforward method is to comment out the debugging messages, but this is unwieldy and undoing this becomes difficult.

A better way of doing this is to write methods in the code which looks somethinglike:

 Switch for debugpublic static final boolan DEBUG = true;void debug(String s){ if (DEBUG){  System.out.println(s); }}

In the above example, the DEBUG flag is global to the class and provides an easy way to switch debugging. An advantage of using final is that the code is ‘compiled out’ of the class file, if it is false.

Another way of doing this is to provide mutator methods for settingthe debug flag. For example,

  private boolean isDebug = false;

or true by default

 public void setDebug(boolean flag){ isDebug = flag; }

The second approach allows more flexibility in controlling generation of debug messages from the calling method. A calling method can set the debug flag of the called class to true/false depending on the situation, whether the debug messages need to printed out. Also, in simple programs debugging could be controlled by passing command line parameters that could be made to set the flag for generating/blocking the debug messages.

See also  Comparing different methods of testing your Infrastructure-as-Code
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist