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
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
|Tool||Purpose||URL for Eclipse plugin|
|CheckStyle||Coding standard analysis||http://eclipse-cs.sourceforge.net/update/|
|Coverlipse||Test code coverage||http://coverlipse.sf.net/update|
|JDepend||Package dependency analysis||http://andrei.gmxhome.de/eclipse/|
Now that you know where to find some useful plugins, installing them is a simple process. Start Eclipse, and then follow these steps:
- Select Help | Software Updates | Find and Install, as shown in Figure 1:
Figure 1. Finding and Installing an Eclipse plugin
- Select the Search for new features to install radio button and then click Next.
- 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
- 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
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.
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:
- Select Project and then select the Properties item from the Eclipse menu.
- Select the CheckStyle active for this project check box and click OK.
Figure 4. Configuring CheckStyle plugin 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
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
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
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
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
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
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
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:
- 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
- Select the Window menu from Eclipse to open the Metrics view and then select Show View | Other....
- 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
- 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
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.
- Automation for the people (Paul Duvall, developerWorks): Read the complete series.
- "Improving Code Quality with PMD and Eclipse" (Levent Gurses, Jacoozi, January 2005): This article looks at PMD as an Eclipse plugin and the ways it can be used to improve the code quality and shorten the code review process.
- "Measure test coverage with Cobertura" (Elliotte Rusty Harold, developerWorks, May 2005): Elliotte Rusty Harold shares how you can take advantage of Cobertura using code-coverage best practices.
- "Don't be fooled by the coverage report" (Andrew Glover, developerWorks, January 2006): This article takes a closer look at what the numbers on the coverage report really mean, as well as what they don't.
- "Managing Your Dependencies with JDepend" (Glen Wilcox, OnJava, January 2004): In this article, Glen Wilcox introduces JDepend, a freely available tool that can provide insight into several qualities of your software architecture.
- "Code quality for software architects" (Andrew Glover, developerWorks, April 2006): Andrew Glover explains how to monitor continuously and correct quality aspects of code that can affect the long-term viability of your software architecture.
- "Automation for the people: Continuous Inspection" (Paul Duvall, developerWorks, August 2006): Paul Duvall looks at how automated inspectors like CheckStyle, JavaNCSS, and CPD enhance the development process and when you should use them.
- "Detecting Duplicate Code with PMD's CPD" (Tom Copeland, OnJava, March 2003): Tom Copeland introduces you to an open source utility called CPD (the copy/paste detector) that finds duplicate Java code for you.
- "Maintain organizational standards with code audits" (testearly.com): Coding standards facilitate a common understanding of a code base among a diverse group of developers.
- developerWorks Java technology zone: Hundreds of articles about every aspect of Java programming.
Get products and technologies
- Eclipse plugin for PMD: Use the PMD plugin to find copy and paste issues within code.
- Eclipse plugin for JDepend: This plugin facilitates analyzing package dependencies within a code base.
- Eclipse plugin for Coverlipse: Coverlipse is an Eclipse plugin which provides code coverage information.
- Eclipse Metrics: This plugin provides metrics such as cyclomatic complexity, which can be quite helpful in finding complex code.
- Eclipse plugin for CheckStyle: Check for adherence to your project's coding standards.
- 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.
Dig deeper into Java technology on developerWorks
Get samples, articles, product docs, and community resources to help build, deploy, and manage your cloud apps.
Experiment with new directions in software development.
Software development in the cloud. Register today to create a project.
Evaluate IBM software and solutions, and transform challenges into opportunities.