X

Geertjan's Blog

  • August 3, 2013

Dead Code Detection with NetBeans IDE

Geertjan Wielenga
Product Manager

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.

Join the discussion

Comments ( 18 )
  • guest Monday, August 12, 2013

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


  • Geertjan Monday, August 12, 2013

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


  • Chris Friday, July 4, 2014

    Is also available for JavaScript?


  • Maik Monday, July 21, 2014

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

    Regards

    Maik


  • Geertjan Monday, July 21, 2014

    Where does it say that it work with NB 7.4?


  • guest Monday, July 21, 2014

    nowhere

    then I change my question

    should it work with NB 7.4 too?

    thanks in advance

    Regards

    Maik


  • Geertjan Wednesday, July 23, 2014

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


  • Maik Wednesday, July 23, 2014

    Good news :-)

    Thanks a lot.

    Regards

    Maik


  • guest Thursday, October 30, 2014

    Hi,

    have you managed to update this for Netbeans 8?

    Thanks a lot,

    David


  • guest Monday, February 15, 2016

    Hi,

    nice idea, please update your plugin!

    all the best,

    Lewis


  • Alex Sunday, March 6, 2016

    Could you update your plugin for Netbeans 8.0.2 and Java 1.8.0?


  • Geertjan Monday, March 7, 2016

    Not sure if I will, just in terms of time, but if I do I will definitely not update to an old release. I will update to NetBeans IDE 8.1. Would you like to help?


  • Alex Monday, March 7, 2016

    Thanks for answer, Geertjan.

    You are right. NetBeans 8.1 is already available for download.

    What kind of help do you need? Unfortunately, I do not have any experience in NetBeans upgrade and plugin elaboration.


  • Alex Monday, March 7, 2016

    Thanks for answer, Geertjan.

    You are right. NetBeans 8.1 is already available for download.

    What kind of help do you need? Unfortunately, I do not have any experience in NetBeans upgrade and plugin elaboration.


  • Alex Tuesday, March 8, 2016

    Thanks for answer, Geertjan.

    You are right. NetBeans 8.1 is already available for download.

    What kind of help do you need? Unfortunately, I do not have any experience in NetBeans upgrade and plugin elaboration.


  • Chris Wednesday, December 7, 2016

    As I ask again, is it only working for Java? Such feature is missing for C/C++, PHP and JS.


  • Chris Thursday, December 8, 2016

    As I ask again, is it only working for Java? Such feature is missing for C/C++, PHP and JS.


  • Geertjan Thursday, December 8, 2016

    Yes, it is really only for Java.


Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.Captcha