tags:

views:

910

answers:

4

Hi,

Normally it's easy to see unused variables in Netbeans, just a grey squiggly line.

But how would I find all of these unused variables in my project? or of a single class?

Reason: I'm debugging a code base which had lots of copy and paste, but it wasn't done carefully s.t. there's many bug of not replacing with the right variable after copy and paste.

Thanks

+7  A: 

You could run something like FindBugs on it.

FindBugs

Looking at the bug list it has

UuF: Unused field (UUF_UNUSED_FIELD) This field is never used. Consider removing it from the class.

You could filter on just this, but it is a good idea to run this on all code all the time, it is amazing what it finds.

Shawn
Upvote for making me discover FindBugs. However, a simple test shows that it correctly detects unused class fields, but not unused local variables.
Luc Touraille
A: 

In Eclipse, that gray squiggly line is a yellow squiggly line called a Warning. Then the warning is propagated up to the package level, and up to the project level (such that your project is almost always underlined in yellow with a warning icon). Anyway it really helps you see which source files have warnings in them.

Then your task is to solve every warning in the entire project, and you will have caught the unhandled variables.

I assume netbeans has the same feature somewhere.

Karl
A: 

The compiler will warn about unused variables giving you your list.

Unused method variables will be removed by the compiler but unused member variables remain, which depending on the state of your codebase may make this a cosmetic problem that can be handled as and when each file is modified rather than a conserted effort to remove all unused variables in one go.

Saying that, I generally like my builds to run with no warnings, so that when I genuinely break something I notice the warning. Maybe this is the clean-up you are looking for ;-)

Nick Holt
+2  A: 

PMD will find unused local variables for you (among many other things). There's a NetBeans plugin that I give installation instructions for here (Warning: Shameless Plug, that links to my blog).

Bill the Lizard