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 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 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.


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 #

Is also available for JavaScript?

Posted by Chris on July 04, 2014 at 01:09 AM PDT #

Hi, is there an issue with NB 7.4?
It doesn't show anything below "Detect Dead Code", even if I add a unused dummy public method to my project...

Posted by Maik on July 21, 2014 at 07:45 AM PDT #

Where does it say that it work with NB 7.4?

Posted by Geertjan on July 21, 2014 at 07:55 AM PDT #

then I change my question
should it work with NB 7.4 too?
thanks in advance

Posted by guest on July 21, 2014 at 09:52 AM PDT #

No, it shouldn't. In the coming weeks, I will update it to be usable in NetBeans IDE 8, the current release.

Posted by Geertjan on July 23, 2014 at 02:07 AM PDT #

Good news :-)
Thanks a lot.

Posted by Maik on July 23, 2014 at 03:17 PM PDT #

have you managed to update this for Netbeans 8?

Thanks a lot,


Posted by guest on October 30, 2014 at 06:40 AM PDT #

Post a Comment:
  • HTML Syntax: NOT allowed

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.


« February 2016