Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Debugging code

 
Matteo Giuberti
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

------------------------------------------
FIRST CASE:
i create a class with static method like this:



and from my code I call:



------------------------------------------
SECOND CASE:
I just call from my code:


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

------------------------------------------
THIRD CASE:
Debug is an interface:



and my class call:


------------------------------------------
FOURTH CASE:
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!

Matteo
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64824
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
java.util.logging or Log4J
 
Matthew Brown
Bartender
Posts: 4567
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 64824
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes.
 
Matteo Giuberti
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
CASE 1:


CASE2:


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
Posts: 448
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
have you looked at assertions...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic