File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Debugging code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Debugging code" Watch "Debugging code" New topic

Debugging code

Matteo Giuberti

Joined: Mar 25, 2011
Posts: 5
I don't know if it's for beginners or java in general, anyway I posted it here. admin should feel free to move this to another category!

I'm trying to realize the more clean way to add debug into my code.
Generally I use some System.out.println() but I know this is not the general rule for a clean code.
I want to do something like creating a public static final boolean flag, let's say "debug" (what a fantasy!), and everytime i need to write some debugging strings I do:

in this way the compiler automatically ignore the unreachable code if debug is set to false!
moreover i can use different variables to specify the level of debugging!

I tried to implement this in many ways and i want to know if someone can tell me the ideal way to do that.

i create a class with static method like this:

and from my code I call:

I just call from my code:

with defined as instance variable:
public static final boolean flag=true;

Debug is an interface:

and my class call:

Debug is again an interface but this time my main class implements it (notice that before this doesn't happen!):

and my class is :


Can you give me some hints and suggestions about these methods??
I think all works pretty well but I'm searching for PERFORMANCE, so I need to use the best between them!
I need to know pros and cons of all these approach!
thank you in advance!

Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63857

java.util.logging or Log4J

[Asking smart questions] [About Bear] [Books by Bear]
Matthew Brown

Joined: Apr 06, 2010
Posts: 4543

Bear's right. A logging framework will give you what you want, and more. You can write messages with different priorities (e.g. errors, warnings, debug statements), and then you can configure logging at the level you want. It also gives you much more flexibility for where the stuff gets written.
Matteo Giuberti

Joined: Mar 25, 2011
Posts: 5
ok I'll give a look at those.

but first of all I would clarify what i'm searching.
i need something able to hide my code if I don't want it! or maybe hide some "kind" of code.
suppose i have info data and debug data.
in developing phase i want to see all the output possible.
when i don't need debug but just know some info I wanna see only the info data.
and when I'll over my application I don't want anything to be displayed.

note that in the last two case, i want the program completely ignore that part of code like I've never written it at all!!
so: no if cycle, no methods call and things like that.

are the classes you give me perfect for my purpose?
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63857

Matteo Giuberti

Joined: Mar 25, 2011
Posts: 5
ok. yesterday i saw something about java.util.logging but I'm not sure you have understood my question!
let's say this first:

in C/C++ it exists the conditional compilation used through the construct #define and #ifdef.

in Java, the same thing could be realized by using public static final boolean variables and if condition!

this construct AT COMPILATION TIME is recognized by compiler as an unreachable piece of code and it's stripped out from the subsequent execution!
in that way, there's no overhead for the check of the if condition neither!
This is pretty useful in developing because a developer can just change the value of variable flag to let the program show some output or not to show it without getting the overhead associated with it!!! THAT IS THE POINT!

From what i read, it doesn't seem to me that java.util.logging classes do that. I think they're just a cleaner way to do debug instead using System.out.println !

Just to clarify, let's do an example:


are CASE1 and CASE2 the same?? because I'm pretty sure that, because of conditional compilation, the if statement is not even checked in CASE1 whereas logger.log in CASE2 I think produce some overhead though setlevel is set to OFF!

am I right or am i just missing something??
Sunny Bhandari
Ranch Hand

Joined: Dec 06, 2010
Posts: 448

have you looked at assertions...

Java Experience
I agree. Here's the link:
subject: Debugging code
jQuery in Action, 3rd edition