Dead Code Detection with NetBeans IDE

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 dev@platform.netbeans.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.

Comments:

Like Wow...not, I use PMD and Findbugs, they find loads more...

Posted by guest on August 12, 2013 at 11:50 AM PDT #

They find dead code? FindBugs, which is a standard part of NetBeans since 7.2, certainly does not.

Posted by Geertjan on August 12, 2013 at 12:22 PM PDT #

Post a Comment:
  • HTML Syntax: NOT allowed
About

Geertjan Wielenga (@geertjanw) is a Principal Product Manager in the Oracle Developer Tools group living & working in Amsterdam. He is a Java technology enthusiast, evangelist, trainer, speaker, and writer. He blogs here daily.

The focus of this blog is mostly on NetBeans (a development tool primarily for Java programmers), with an occasional reference to NetBeans, and sometimes diverging to topics relating to NetBeans. And then there are days when NetBeans is mentioned, just for a change.

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
12
13
14
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today