• Post Reply Bookmark Topic Watch Topic
  • New Topic

Suppress 'dead code' warnings  RSS feed

 
Jim Venolia
Ranch Hand
Posts: 312
2
Chrome Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Ron McLeod
Bartender
Posts: 1602
232
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you remove the final keyword the warnings will go-away.
 
Jason Bullers
Ranch Hand
Posts: 115
11
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Jim Venolia
Ranch Hand
Posts: 312
2
Chrome Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Stefan Evans
Bartender
Posts: 1837
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jim Venolia
Ranch Hand
Posts: 312
2
Chrome Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jim Venolia
Ranch Hand
Posts: 312
2
Chrome Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Stefan Evans
Bartender
Posts: 1837
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
>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.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66304
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why not use a real logger?
 
Dave Tolls
Ranch Foreman
Posts: 3056
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!