Sunday, November 2, 2008

Remove debug messages

While developing an application it is common place to first use emulators. This environment gives access to stepping and watching variable values. Very useful, high development speed.
Then you need to run the application on actual devices. Some manufacturers implement On Device Debugging so, the same environment is available even when the MIDlet is running on an actual device.
For those that do not have ODD it is necessary to use some sort of logging mechanism, but all need the developer to add debugging code. Code that is not part of the application, that need to be removed before the final build, that is just wasting valuable space.
How can we be sure there is no debugging code?
One common practice is to define a public boolean constant that toggles debugging off:

public static final boolean DEBUG = true;

Every line of debugging code should be enclosed within an if block:

if (DEBUG) {
    // debug code
}

If you set the constant to false the Java compiler will not add the if block to the class file.

No comments: