Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: Java
Expertise: All
Aug 29, 2000



Building the Right Environment to Support AI, Machine Learning and Deep Learning

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 something like:

 Switch for debug
public static final boolan DEBUG = true;
void debug(String s){
 if (DEBUG){
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 setting the 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.

Indus Khaitan
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date