Monday, July 14, 2008

Detecting Code Smells With Eclipse and CheckStyle

In a new article "Automation for the people: Continual refactoring" as a part of the "Automation for the people" series, Paul Duvall discusses the use of static code analysis tools to identify code smells and suggested refactorings. The article shows how to
  • Reduce conditional complexity code smells by measuring cyclomatic complexity using CheckStyle and providing refactorings such as Replace Conditional with Polymorphism
  • Remove duplicated code code smells by assessing code duplication using CheckStyle and providing refactorings such as Pull Up Method
  • Thin large class code smells by counting source lines of code using PMD (or JavaNCSS) and providing refactorings such as Extract Method
  • Wipe out too many imports code smells by determining a class's efferent coupling using CheckStyle (or JDepend) and providing refactorings such as Move Method
There's MoreThe following is a short list of static code analysis tools available for JavaThis post describes how to identify common code smells using CheckStyle and Eclipse. Checkstyle has a useful eclipse plugin. Installing the plugin is simple. In Eclipse Ganymede,
  1. Go to Help->Software Updates->Software Updates->Available Software
  2. Click on Add Site, and add http://eclipse-cs.sourceforge.net/update to the sites list
  3. Select the new site and click Install
Once CheckStyle plugin is install, running the tool is quite simple. Usage is well documented in the plugin site. The following are some Code smells which can be detected using Checkstyle, along with the suggested refactorings (from the "Smells to Refactorings Quick Reference Guide"). The description of the refactorings can be found at refactoring.com and refactoring to patterns catalog. The center column shows the CheckStyle configuration option in the plugin GUI.


Conditional complexityMetrics->Cyclomatic Complexity
  • Introduce Null Object
  • Move Embellishment to Decorator
  • Replace Condidtional Logic with Strategy
  • Replace State-Altering Conditionals with State
Duplicated codeDuplicates->Strict Duplicate Code
  • Chain Constructors
  • Extract Composite
  • Extract Method
  • Extract Class
  • Form Template Method
  • Introduce Null Object
  • Introduce Polymorphic Creation with Factory Method
  • Pull Up Method
  • Pull Up Field
  • Replace One/Many Distinctions with Composite
  • Substitue Algorithm
  • Unify Interfaces with Adapter
Long methodSize Violations->Maximum Method Length
  • Extract Method
  • Compose Method
  • Introduce Parameter Object
  • Move Accumulation to Collecting Parameter
  • Move Accumulation to Visitor
  • Decompose Conditional
  • Preserve Whole Object
  • Replace Conditional Dispatcher with Command
  • Replace Conditional Logic with Strategy
  • Replace Method with Method Object
  • Replace Temp with Query

2 comments:

  1. Another great tool for this is CodePro AnalytiX from Intantiations.

    http://www.instantiations.com/codepro/analytix/feature/code-analysis.html

    ReplyDelete