Saturday, May 19, 2007

Here is a Quick Way to Eliminate Dead Code

If plug-ins were tools the Eclipse Core Tools plug-in would be a Swiss army knife. One of it's most useful tools is finding unused methods and fields in your code. I used it recently to remove obsolete code from my plug-ins.

To start removing unused code, follow these steps:

  1. Download version 1.4.0 (for Eclipse 3.2) of the Eclipse Core Tools from this update site:
    http://www.eclipse.org/eclipse/platform-core/updates/

  2. In a workspace that contains all related code, select the project / package / file you want to work on.

    (When I did this, I actually closed my unit-test projects because I didn't want to keep code around that is used nowhere but in tests. I refactored or removed the broken tests afterwards.)

  3. In the context menu select Find Unreferenced Members. The results will be shown in the Search view.



  4. Remove unreferenced code, but use your judgment!

    Some code may appear unreferenced, that in fact isn't. Common examples:
    • code that is contributed through extension-points
    • code that is invoked through reflection

    Some code may be unreferenced, but there might be good reasons for keeping it:
    • unreferenced code that is API
    • unreferenced fields in Serializable classes
    • private constructors (if you use them to enforce non-instantiability or the singleton property - Effective Java, Items 2 and 3)

  5. Repeat steps 2 - 4 as often as necessary.

What are your favorite tools for cleaning-up code?

Knife Image: (c) 2004, brokenarts/stock.xchng

6 comments:

Lhasadad said...

Doesn't look like the update site is public

Elias Volanakis said...

Hi Lhasadad,

the update site is actually public, despite getting an "Access Forbiden" message when trying to access it in a web browser.

This is because the web browser and the Update Manager access the site a bit differently. The Update Manager automatically appends "site.xml" to the URL given in the article. :)

Elias.

Renaud said...

great tool, thanks!

Anonymous said...

Here is a similar tool: www.ucdetector.org

evernat said...

And here is another similar tool: http://dcd.dev.java.net (eclipse not needed)

Anonymous said...

Great tool, thanks. It would be great if I could identify unused classes or source files, too.

 

Content: © Copyright 2007 - 2009, Elias Volanakis. All rights reserved. Datenschutzhinweis / Privacy Policy