Saturday, November 25, 2006

Tools to clean/analyse your junk code

While we all write the "best code in the world", only few of us actually take the pain review/analyse it for improvements(for the obvious reason that best cannot be improved!).


Fortunately java world provide a bunch of tools to analyze your source code and provide recommendations to clean them. Eclipse and java development has evolved to a point where most of the development related activities can be acheived from a single IDE with plugins.

Source code analyzers typically work using a set of rules. These rules can be modified, changed, etc based upon requirements. Every tool comes bundled with its own set of rules. Rules belong to categories. You can execute rules/categories selectively or all of them on your source code. Recommendations are then presented in tabular form. You then have a choice of fixing it on your own or allowing the tool to fix it. Caution: Some rules might alter the logic of your source code, so make sure you are aware of the changes before you execute them. Popularity of a tool depends on the effectiveness of the rules and the number of rules in it.

Here are some interesting tools:

Eclipse Refactor:
Right click your source in eclipse and check the options under "source" and "refactor". You can do lot of things like "correct indentation", "format" and other cleanups. This is a good way to make your code look clean and green.

PMD:(free)

PMD scans Java source code and looks for potential problems like:

  • Possible bugs - empty try/catch/finally/switch statements
  • Dead code - unused local variables, parameters and private methods
  • Suboptimal code - wasteful String/StringBuffer usage
  • Overcomplicated expressions - unnecessary if statements, for loops that could be while loops
  • Duplicate code - copied/pasted code means copied/pasted bugs
You can either use the default(around 200) rules or write your own rules using xpath/java.
AppPerfect (commercial)
  • Applies over 600 high-value coding rules, including optimization, portability, i18n and coding standards, while analyzing your source code
  • Automatically fixes many of the violations
  • Support for JDK 1.5-specific syntax and keywords
  • Comprehensive reports including custom report designer and export of reports in PDF, Excel and HTML formats
  • Command line invocation to integrate with build scripts (including ANT-based built scripts)

Eclipse Metrics(free)
Checks for:

Other links:
@Eclipse source code analyzer
@Cyclomatic complexity
@Maven plugin for reporting
[Checkstyle, FindBugs, PMD, Lint4j, JavaNCSS, JCoverage, Cobertura, Emma, Clover, Tasks List]

0 Comments:

Post a Comment

<< Home