Forums Register Login

Suppress 'dead code' warnings

+Pie Number of slices to send: Send
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.

+Pie Number of slices to send: Send
If you remove the final keyword the warnings will go-away.
+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
Just to put an alternative point of view, would you consider converting these DEBUG statements to a logging framework?

ie
log.info("print something");
log.debug("print something with more details information");

That would get rid of the dead code warnings as well
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

Jim Venolia wrote:. . . But DEBUG is what in C would be an #define . . . .

No it isn't. As far as I know, Java® has no equivalent to #define, even less ifndef.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
>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.
+Pie Number of slices to send: Send
Why not use a real logger?
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
It's only a warning? There I was thinking it was a compiler error. You are right. That code compile runs and produces no output.
Is this the real life? Is this just fantasy? Is this a tiny ad?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1045 times.
Similar Threads
how to get rid of warning: [unchecked] unchecked cast
Casting and Generics
Exception again....
Java generics
Section 2 mock test questions
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 22:48:08.