views:

77

answers:

2

Recently i've found myself writing a lot of methods with what i can only think to call debugging scaffolding. Here's an example:

public static void printArray (String[] array, boolean bug) 
{ 
    for (int i = 0; i<array.lenght; i++) 
    { 
        if (bug) System.out.print (i) ; //this line is what i'm calling the debugging scaffolding i guess. 
        System.out.println(array[i]) ; 
    }
}

in this method if i set bug to true, wherever its being called from maybe by some kind of user imput, then i get the special debugging text to let me know what index the string being printed as at just in case i needed to know for the sake of my debugging (pretend a state of affairs exists where its helpful).

All of my questions more or less boil down to the question: is this a good idea? but with a tad bit more objectivity:

  • Is this an effective way to test my methods and debug them? i mean effective in terms of efficiency and not messing up my code.

  • Is it acceptable to leave the if (bug) stuff ; code in place after i've got my method up and working? (if a definition of "acceptability" is needed to make this question objective then use "is not a matter of programing controversy such as ommiting brackets in an if(boolean) with only one line after it, though if you've got something better go ahead and use your definition i won't mind)

  • Is there a more effective way to accomplish the gole of making debugging easier than what i'm doing?

  • Anything you know i mean to ask but that i have forgotten too (as much information as makes sense is appreciated).

+5  A: 

I don't think the way you do is really a nice way of logging.

Why?

It's not configurable during runtime. It's custom made and specific for every log statement.

What else?

You should check out some common logging utility. The most common one I know is log4j.

So here you go: http://logging.apache.org/log4j/

Florian Gutmann
what does "logging" mean?
David
@David: In this context, "logging" means recording state to some form of external output. Log4j (and the plethora of logging solutions for Java) are capable of writing to files, stdout, databases, etc.
ig0774
+1... log4j, used with good Exception handling, does the job very well.
M. Joanis
Both this and Dan's answer seem to sugest that i want to change the state of my porgram during run time. This isn't the case. I just want to view its state in detail. (dans answer is a little closer to what i'm going for though in terms of teh Error and Debug and such)
David
@David, a logging framework is the easiest way to formalize the "if (debug) print xyz" since the flags are maintained inside the logging framework, and the destination of the print statements is also maintained inside the logging framework. If you use slf4j you can even skip the if as it is also handled inside the logging framework.
Thorbjørn Ravn Andersen
+5  A: 

Check out Simple Logging Facade for Java (slf4j). It subsumes the functionality of Log4j.

slf4j lets you log each statement at a fixed importance, ranging from low-importance DEBUG messages, through medium importance INFO messages, up to critical stuff with ERROR. There are intermediate importance levels too.

You configure slf4j's importance threshold at runtime. So when you want only the highest priority messages, you could set the threshold to ERROR, but when you're testing, set it to DEBUG. This means you can see different degrees of debugging verbosity just by tweaking some settings. You don't have to recompile to change which statements get logged. That's a big win.

What you're doing now is basically slf4j's log.debug("message").

Also consider Java assertions. Assertions help you guarantee properties on your program's internal state at runtime.

Dan LaRocque
While log4j is indeed "older", don't most people who use slf4j end up sending it's output to log4j anyway?
matt b
Actually, logback is the successor of log4j: http://logback.qos.ch/documentation.html It's a native implementation of slf4j.
BalusC
I can't speak for most people. But no, I use slf4j's native backend called logback. I don't think I've had actual log4j dependencies on my projects for ages. You can do that, though.
Dan LaRocque
@BalusC right, edited
Dan LaRocque
@BalusC, logback is a fork of log4j by the original founder. The log4j project does not consider logback the successor of log4j. We use logback instead of log4j as log4j currently is in maintainance mode.
Thorbjørn Ravn Andersen