By Geertjan-Oracle on Aug 03, 2013
In NetBeans IDE, you can go to Source | Inspect to look for unused imports and assignments in your code. But that, of course, is only the tip of the iceberg. Michael Bishop, on the firstname.lastname@example.org mailing list, asks:
I'm looking to clean up some of my code. Is there a tool out there that can find dead code across all my source modules? I know I can open every last source module and do a "Find Usages" for a particular section code in the "Open Projects" scope, but I'm looking for a way to search for ALL unused code across all the sources I control.
He then points to the DCD (dead code detector) project at https://java.net/projects/dcd. Far from being dead, the project is, in fact, stable. I love how things that haven't been developed for a few years are automatically considered to be dead, for that reason alone.
On the contrary, I've found that the DCD project is very usable indeed and simply by putting all the code into a NetBeans module, together with a TopComponent that adds the DeadCodeDetectorUI JPanel, I have a handy, though slightly unidiomatic, NetBeans feature that does what I would expect a dead code detector to do:
I've also tried the above on a modular application, i.e., a NetBeans Platform application. I opened the Paint Application, then added a new class in the ColorChooser module and defined a variable named "bla" that I subsequently didn't use, while I removed the reference in the Paint module to the "initComponents" method. Then I ran the dead code detector and came up with the following results, note the "readProperties" and "writeProperties" are also detected, since they're not used in my code, but used automatically by the NetBeans Platform for serialization:
Next steps would be to either integrate it into the Source | Inspect feature in NetBeans IDE or to create a right-click action on projects, packages, and files from which the dead code detection can be invoked. The result would be printed into the Output window, with hyperlinks so that the user can jump directly into their dead code and do something with it. Simply deleting all the dead code automatically would probably be a bad idea. On a case by case basis, the user would want to decide what to do with the dead code.
Once I've contacted the DCD project owner Emeric Vernat, I'll work with him to make the plugin available. But anyone else could do exactly what I did in about 10 minutes, i.e., download the DCD project sources, copy them into a module, include the 3rd party JARs that are in the DCD project, create a new TopComponent, and add the GUI panel from the DCD project to the TopComponent.