Automation for the people: Improving code with Eclipse plugins

Automate code quality analysis within Eclipse with five helpful plugins

What if you were able to discover potential problems in your code prior to building it? Interestingly enough, there are Eclipse plugins for tools such as JDepend and CheckStyle that can help you discover problems before they are manifested in software. In this installment of Automation for the people, automation expert Paul Duvall provides examples of installing, configuring, and using these static analysis plugins in Eclipse so that you can prevent problems early in the development life cycle.

Share:

Paul Duvall (paul.duvall@stelligent.com), CTO, Stelligent Incorporated

Paul DuvallPaul Duvall is the CTO of Stelligent Incorporated, which helps companies address software quality with effective developer testing strategies and Continuous Integration techniques that enable teams to monitor and improve code quality early and often. He is a contributing author to the UML™ 2 Toolkit and currently co-authoring Continuous Integration: Improving Software Quality and Reducing Risk (Addison-Wesley).



11 January 2007

Also available in Chinese Russian Japanese

About this series

As developers, we work to automate processes for end-users; yet, many of us overlook opportunities to automate our own development processes. To that end, Automation for the people is a series of articles dedicated to exploring the practical uses of automating software development processes and teaching you when and how to apply automation successfully.

One of my primary goals when developing software is either preventing a defect from being introduced into a code base or limiting its lifetime; in other words, I try to find defects as early as possible. Obviously, the more I learn about how to write better code and learn how to effectively test software, the better I am at catching defects. But, I also like to have a safety net that can find potential defects.

In the August installment of this series, I concluded that integrating inspection tools into the build process (for example, using Ant or Maven) can establish a consistent approach in finding potential defects. Although this approach enables consistency and transcends IDEs, it is also slightly reactionary. You must either build software locally or wait for a Continuous Integration build to run. By using Eclipse plugins, however, you can uncover some of these violations before you build or integrate via Continuous Integration. This enables what I like to dub progressive programming, which enables a level of quality checking during coding -- it's hard to get much earlier!

This article covers what I consider to be the "big five" code analysis areas:

  • Coding standards
  • Code duplication
  • Code coverage
  • Dependency analysis
  • Complexity monitoring

These analysis areas can be uncovered using a number of the following slick Eclipse plugins:

  • CheckStyle: For coding standards
  • PMD's CPD: Enables discovering code duplication
  • Coverlipse: Measures code coverage
  • JDepend: Provides dependency analysis
  • Eclipse Metrics plugin: Effectively spots complexity

Eclipse isn't your build system

Using Eclipse plugins shouldn't preclude you from using these inspection tools as part of the build process. In fact, you want to ensure that the rules followed using the Eclipse plugins are the same rules applied to your build process.

Installing Eclipse plugins

Installing Eclipse plugins couldn't be simpler -- it takes only a few steps. Before you start though, it's helpful to have the URL of the plugin's download site handy. I've provided a list of the plugins I use in this article in Table 1:

Table 1. List of code improvement plugins and installation URLs
ToolPurposeURL for Eclipse plugin
CheckStyleCoding standard analysishttp://eclipse-cs.sourceforge.net/update/
CoverlipseTest code coveragehttp://coverlipse.sf.net/update
CPDCopy/Paste detectionhttp://pmd.sourceforge.net/eclipse/
JDependPackage dependency analysishttp://andrei.gmxhome.de/eclipse/
MetricsComplexity monitoringhttp://metrics.sourceforge.net/update

Now that you know where to find some useful plugins, installing them is a simple process. Start Eclipse, and then follow these steps:

  1. Select Help | Software Updates | Find and Install, as shown in Figure 1:

    Figure 1. Finding and Installing an Eclipse plugin
    Finding and Installing an Eclipse plugin
  2. Select the Search for new features to install radio button and then click Next.
  3. Click New Remote Site, enter the name and URL for your desired plugin (see Figure 2), click OK, and then click Finish to display the Eclipse Update Manager.

    Figure 2. Configuring a new Remote Site
    Configuring a new Remote Site
  4. In the Eclipse Update Manager, you'll see an option to view various aspects about the plugin. I usually select the top-level choice, as shown in Figure 3. Make your selection and click Finish. Eclipse installs the plugin now. You need to restart your Eclipse instance.

    Figure 3. Installing the Eclipse plugin
    Installing the Eclipse plugin

Follow these steps to install any Eclipse plugin; simply change the plugin's name and associated download location.


Correcting standards with CheckStyle

The maintainability of a code base directly affects the overall cost of software. Furthermore, maintainability contributes to the frustration (or lack thereof) of developers -- the easier it is to make code changes, the easier it is to add new product features. A tool like CheckStyle assists in finding violations to coding standards that may affect maintainability such as large classes, long methods, and unused variables, to name a few.

What about PMD?

Another open source tool called PMD provides features similar to those in CheckStyle. I prefer CheckStyle, but PMD has a strong and dedicated following, so I recommend that you check it out; a lot of people who swear by it!

The benefit of using the CheckStyle plugin for Eclipse is the ability to learn about various violations in context of source code while coding, making it more likely that a developer will actually fix the violation before a check-in. You can almost think of using the CheckStyle plugin as a continual code review!

Install the CheckStyle plugin and configure it (see Figure 4) as follows:

  1. Select Project and then select the Properties item from the Eclipse menu.
  2. Select the CheckStyle active for this project check box and click OK.

    Figure 4. Configuring CheckStyle plugin in Eclipse
    Configuring CheckStyle pluging in Eclipse

Eclipse rebuilds your workspace and lists any discovered coding violations within the Eclipse console, as shown in Figure 5:

Figure 5. List of CheckStyle violations within Eclipse
List if CheckStyle violations within Eclipse

Embedding coding standard checks within Eclipse using the CheckStyle plugin is a great way to proactively improve code while coding, which can have the affect of discovering a potential defect in your source code early in the development cycle. This has the added benefit of saving your time, your frustration, and therefore maybe even your project's costs. Now that's being proactive!


Confirming coverage with Coverlipse

Coverlipse is an Eclipse plugin that you can use to assess the percentage of source code that has a corresponding test. With Coverlipse, you are able to assess code coverage while writing code. Are you noticing a pattern yet?

Install the Coverlipse plugin and associate it with JUnit by selecting the Run Eclipse menu item, which presents a series of run configurations like JUnit, SWT application, and Java™ applications, for example. Right-click and select New from the JUnit w/Coverlipse node. From here, you need to identify the location of your JUnit tests as demonstrated in Figure 6:

Figure 6. Configuring Coverlipse to obtain code coverage
Configuring Coverlipse to get code coverage

Once you click Run, Eclipse runs Coverlipse and embeds markers in your source code (as shown in Figure 7), which indicate portions of code that have associated JUnit tests:

Figure 7. Report generated by Coverlipse with embedded class markers
Report generated by Coverlipse with embedded class markers

As you an see, you can determine code coverage much more quickly using the Coverlipse Eclipse plugin. This live data capability can help hone your testing efforts before checking in code into a CM system, for example. How's that for progressive programming?


Catching code duplication with CPD

The PMD plugin for Eclipse provides a feature called CPD (or Copy Paste Detector), which finds duplicated code. To use this handy tool within Eclipse, you need to install the Eclipse plugin for PMD, which contains the CPD utility.

To find duplicate code, right-click an Eclipse project and select PMD | Find Suspect Cut and Paste, as shown in Figure 8:

Figure 8. Running Copy and Paste checks using CPD plugin
Running Copy and Paste checks using PMD/CPD plugin

Once you've run CPD, a report folder is created in your Eclipse root directory containing a file called cpd.txt, which lists all duplicate code. An example of the cpd.txt file is shown in Figure 9:

Figure 9. CPD text file generated from Eclipse plugin
CPD text file generated from Eclipse plugin

Finding duplicate code manually is challenging, but using a plugin like CPD, however, quickly reveals duplicated code while you are coding.


Dependency jousting with JDepend

JDepend is a freely available open source tool that provides object-oriented metrics for package dependencies, which give an indication of the resiliency of a code base. In other words, JDepend effectively measures the robustness (or conversely, the brittleness) of an architecture.

In addition to its Eclipse plugin, JDepend provides an Ant task, Maven plugin, and a Java application for obtaining these metrics. Each provides a different delivery mechanism for the same information; however, the significant difference and corresponding benefit of the Eclipse plugin is that it delivers this information in a closer proximity to the source code (that is, as you are coding).

Figure 10 demonstrates using the Eclipse plugin for JDepend by right-clicking on a source folder and selecting Run JDepend Analysis. Be sure to select a folder that has source code; otherwise, you won't see this menu option.

Figure 10. Analyzing code using JDepend Analysis
Analyzing code using JDepend Analysis

The report generated when running the JDepend analysis is shown in Figure 11. The left pane shows the packages and the right pane shows the dependency metrics for each package.

Figure 11. Package dependencies for project within Eclipse
Package dependencies for project within Eclipse

As you can see, the JDepend plugin provides a wealth of information that can facilitate watching whether or not an architecture is becoming more or less maintainable over time -- and the best part about it is that you can see this data while you're coding.


Measuring complexity with Metrics

The final measure in the "big five" code analysis areas is complexity. Eclipse provides a plugin called Metrics that provides many useful code metrics including cyclomatic complexity, which is a measure of the number of unique paths in a method.

Install the Metrics plugin and restart Eclipse; then, follow these steps:

  1. Right-click your project and select the Properties menu. In the resulting window, select the Enable Metrics plugin check box and click OK, as shown in Figure 12:

    Figure 12. Configuring Metrics to work for your project
    Configuring Metrics to work for your project
  2. Select the Window menu from Eclipse to open the Metrics view and then select Show View | Other....
  3. Select the Metrics | Metrics View to open the window shown in Figure 13. You need to use the Java perspective and rebuild your project to display the metrics.

    Figure 13. Opening the Metrics View in Eclipse
    Opening the Metrics View in Eclipse
  4. Click OK to display a window like the one in Figure 14.

    In this case, I am viewing the cyclomatic complexity of an individual method. What's really nice is that you can double-click the method from the Metrics list and the plugin opens the source code for this method in the Eclipse editor. Now that makes it super easy to provide a correction, if needed!

    Figure 14. Viewing Cyclomatic Complexity for a method
    Viewing Cyclomatic Complexity for a method

As I mentioned earlier, the Eclipse Metrics plugin provides many other powerful metrics to help improve your code while you are developing software -- now that's a progressive plugin!


Use what works for you

As you can tell from this article, I think it's important to have the "big five" measures for code quality: coding standards, code duplication, code coverage, dependency analysis, and complexity monitoring. But what works for you is what matters. Keep in mind that there are many other tools available as Eclipse plugins, such as PMD and FindBugs to name a few, that can help you improve the quality of your code early in the development cycle. Regardless of your desired tool or preferred measure, it's important to take action to improve code actively and make your manual code review process more efficient. I predict that after using these plugins for a time, you'll wonder how you lived without them.

Resources

Learn

Get products and technologies

Discuss

  • Improve Your Code Quality discussion forum: Regular developerWorks contributor Andrew Glover brings his considerable expertise as a consultant focused on improving code quality to this moderated discussion forum.

Comments

developerWorks: Sign in

Required fields are indicated with an asterisk (*).


Need an IBM ID?
Forgot your IBM ID?


Forgot your password?
Change your password

By clicking Submit, you agree to the developerWorks terms of use.

 


The first time you sign into developerWorks, a profile is created for you. Information in your profile (your name, country/region, and company name) is displayed to the public and will accompany any content you post, unless you opt to hide your company name. You may update your IBM account at any time.

All information submitted is secure.

Choose your display name



The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerWorks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

Required fields are indicated with an asterisk (*).

(Must be between 3 – 31 characters.)

By clicking Submit, you agree to the developerWorks terms of use.

 


All information submitted is secure.

Dig deeper into Java technology on developerWorks


static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=Java technology
ArticleID=187350
ArticleTitle=Automation for the people: Improving code with Eclipse plugins
publish-date=01112007