My code is littered with debug statements of the form:
etc. Unfortunately each one of these generates a DEAD CODE warning. I don't want to use @SuppressWarnings as I'm actually interested in dead code, just not the DEBUG lines.
My only idea is to make DEBUG an int to make the compiler think it may change during runtime, but that strikes me as ugly.
Assuming you want to keep all the flags working like that, what if you read in the value of DEBUG as a system property with a default value of 0? Does that make the warnings go away?
Yeah, I'd thought of that. I've already got command line args, easy enough to add one to change DEBUG. But DEBUG is what in C would be an #define and, IMHO, it's meaning is clearer the way it's declared.
Sounds like I either learn to live with the warnings, or convert my #define to an int.
My DEBUGs don't really need a logging framework. The code is working, I just leave the DEBUGs in in case I break something in the future.
I've used logging frameworks in the past, even written a couple of my own before they were commonly available. Their good when needed, just not needed here.
>My only idea is to make DEBUG an int to make the compiler think it may change during runtime, but that strikes me as ugly.
The whole concept is a little bit ugly to be honest.
The debug stuff can only be switched on/off at compile time. It IS dead code as far as the compiler is concerned.
The reason I suggested a logging framework is that delegating the logic to a method avoids the dead code issue.
Plus you would get the bonus of being able to switch the debug logic on dynamically.
At the end of the day though, if you want it like this, I would embrace the "make the int non final" approach so the compiler doesn't complain and call it a day.
Sure, its a little bit ugly, but not as ugly as what you have now.
Unfortunately, in my experience, you can never get rid of all the ugly.
Jim Venolia wrote:Really? We both know what I meant. In C DEBUG would be an ifdef, in Java that isn't possible so I emulate it.
We both know where I'm coming from and what I'm trying to do.
Except that the ifdef actually physically adds/removes (depending on your viewpoint) the code, so it doesn't get to the compiler.
Your DEBUG statement, in Java, is still there in the final code (barring compiler optimisation), hence the dead code warning.
Why are you concerned about the warnings, out of curiosity? They don't prevent your code compiling.