[Post] automate development: Use Eclipse plug-ins to improve code Quality

Source: Internet
Author: User
Tags coding standards checkstyle

From: http://www.javaeye.com/topic/140298

DevelopmentSoftwareOne of my main goals is to either prevent defects from being introduced.CodeLibrary, or restrict the lifetime of the defect; in other words, identify the defect as soon as possible. Obviously, the more you understand how to write better code and how it worksTest SoftwareTo capture defects as early as possible. I also want a security network that can discover potential defects.

In this series of articles in February, I came to the conclusion thatToolsIntegration into the build process (for example, using ant or Maven) can establish a method to find potential defects. Although this method makes consistency possible and surpasses IDE, it also has a pointReaction. You must build the software locally or wait for the running of the continuous integration build. If you use eclipsePlug-ins, You can build or integrate through the continuous integrationBeforeSome such conflicts are found. This leadsProgressive ProgrammingIn this way, a certain degree of quality test can be performed during the encoding process-it can no longer be earlier than this!

This article covers the five major fields of code analysis I think:

· Coding standards

· Repeated code

· Code coverage

· Dependency analysis

· Complexity monitoring

The following flexible Eclipse plug-ins can be used to reveal these analysis fields:

· Checkstyle: used for encoding standards

· Pmd cpd: helps to detect code duplication

· Coverlipse:MeasurementCode coverage

· Jdepend: Provides dependency analysis

· Eclipse metric Plugin: effectively detects complexity

EclipseNot your build system
There is no conflict between using the Eclipse plug-in and using these verification tools for the build process. In fact, what you want to ensure is that the following rules using the Eclipse plug-in are the rules applied to the build process.

Install Eclipse plug-in

Installing the Eclipse plug-in is no longer easy, but only a few steps are required. Before getting started, you 'd better prepare the URL of the plug-in download site. Table 1 lists the plug-ins used in this article:

Table1.Code improvement plug-ins and corresponding download sitesURL

Tools
Purpose
Eclipse plug-in URL

Checkstyle
Encoding standard analysis
Http://eclipse-cs.sourceforge.net/update/

Coverlipse
Test code coverage
Http://coverlipse.sf.net/update

CPD
Copy/paste Test
Http://pmd.sourceforge.net/eclipse/

Jdepend
Package dependency analysis
Http://andrei.gmxhome.de/eclipse/

Metrics
Complexity monitoring
Http://metrics.sourceforge.net/update

After learning about these useful plug-ins, installing the plug-ins is an extremely simple process. Start eclipse and follow these steps:

1. SelectHelp | software updates | find and install, 1:
Figure1.Find and installEclipsePlug-ins

2. SelectSearch for new features to installClickNext.

3. ClickNew Remote Site, Enter the plug-in name and URL to install (see figure 2), and clickOKAnd then clickFinishTo display the eclipse update manager.
Figure2.Configure a new remote site

4. In the eclipse update manager, there is an option to view various features of the plug-in. I usually select top-level items, as shown in 3. Select the required option and clickFinish. Eclipse now installs this plug-in. You need to restart the eclipse instance.
Figure3.InstallEclipsePlug-ins

Follow these steps to install other Eclipse plug-ins. You only need to change the plug-in name and the corresponding download location.

Standard Calibration with checkstyle

The maintainability of the code library directly affects the entire cost of software. In addition, poor maintainability can also lead to a headache for developers (which leads to a lack of developers)-the easier the code is to modify, the easier it is to add new product features. Tools such as checkstyle can help find areas that affect maintainability and conflict with coding standards, such as too large classes, too long methods, and unused variables.

RelatedPMD
Another open-source tool called PMD provides similar functions as checkstyle. I prefer checkstyle, but it also has a lot of followers, so I suggest you understand this tool. After all, it is also favored by some people.

The advantage of using the checkstyle plug-in of eclipse is that various encoding conflicts in the source code context can be learned during the encoding process, making it more likely that developers can really handle these conflicts before checking in the code. You can also regard the checkstyle plug-in as a continuous code review tool!

Install the checkstyle plug-in and make the following configurations (see figure 4 ):

1. SelectProjectAnd selectPropertiesMenu item.

2. SelectCheckstyle active for this projectCheck box, clickOK.
Figure4.InEclipseConfiguringCheckstylePlug-ins

Eclipse re-constructs the workspace and lists the detected encoding conflicts in the eclipse console, as shown in Figure 5:

Figure5. EclipseMediumCheckstyleList of code conflicts

Using the checkstyle plug-in to embed encoding standards in eclipse is a great method.During encodingActively improve the Code to discover potential defects in the source code early in the development cycle. Doing so has more benefits, such as saving time, reducing failures, and thus reducing project costs. Yes, this is a proactive approach!

Use coverlipse to confirm coverage

Coverlipse is an Eclipse plug-in for cobertura. cobertura is a code coverage tool that can be used to evaluate the ratio of source code with corresponding tests. Cobertura also provides an ant task and Maven plug-in, but with cobertura, you canWhen writing codeEvaluate code coverage. Have you ever seen this mode?

By selecting eclipse menu itemsRunInstall the coverlipse plug-in and associate it with JUnit. This operation displays a series of running configuration options, such as JUnit, SWT, and Java applications. Right-click it and selectJUnit w/coverlipseNodeNew. Here, you need to determine the position of the JUnit test, as shown in 6:

Figure6.ConfigurationCoverlipseTo get code coverage

Once you clickRun, Eclipse runs coverlipse and embeds the mark in the source code (7). This mark shows the code part with the relevant JUnit test:

Figure7. coverlipseGenerated reports with embedded class tags

As you can see, using the coverlipse Eclipse plug-in can quickly determine code coverage. For example, this real-time data function helps you check the code into the CM system.BeforeBetter test. What does this mean for progressive programming?

Capture code duplication with CPD

Eclipse's PMD plug-in provides a function called CPD (or copy and paste detector) to find duplicate code. To use this convenient tool in eclipse, you need to install the Eclipse plug-in with PMD, which has the CPD function.

To find duplicate code, right-click an Eclipse project and selectPMD | find Suspect cut and paste, As shown in Figure 8:

Figure8.UseCPDPlugin running copy and paste Test

Once CPD is run, a report is created under your eclipse root directory.FolderContains a file called cpd.txt, which lists all repeated codes. Figure 9 shows an example of a cpd.txt file:

Figure9. EclipseGenerated by the plug-inCPDText Files

Finding duplicate code manually is a challenge, but using a plug-in like CPD can easily discover duplicate code during coding.

Use jdepend for dependency check

Jdepend is an open-source tool that can be obtained free of charge. It provides object-oriented measurements for the package dependency to indicate the elasticity of the code library. In other words, jdepend can effectively measure the robustness (vice versa) of an architecture ).

In addition to the Eclipse plug-in, jdepend also provides an ant task, Maven plug-in, and a Java application to obtain these metric values. They have different transfer mechanisms for the same information, but the special and advantages of the Eclipse plug-in are that it can be closer to the source code (that is, during encoding).

Figure 10 shows how to use the eclipse jdepend plug-in: Right-click the source folder and selectRun jdepend Analysis. Be sure to select a source folder with source code; otherwise, you will not be able to see this menu item.

Figure10.UseJdepend AnalysisAnalyze code

Figure 11 shows the report generated when jdepend analysis is run. The package is displayed on the left and the dependency metric value for each package is displayed on the right.

Figure11. EclipsePackage dependencies in the project

As you can see, the jdepend plug-in provides a large amount of information that helps you continuously observe changes in architecture maintainability-the biggest benefit of this is that you can see the data during coding.

Use metrics to measure complexity

The final part of "five major" code analysis is to measure complexity. Eclipse provides a plug-in called metrics, which can be used to measure a lot of useful code, including the number of unique paths in a measurement method.

Install the metrics plug-in and restart eclipse. follow these steps:

1. Right-click your project and selectPropertiesMenu. In the result window, selectEnable metrics pluginCheck box and clickOK, 12:
Figure12.Configure a projectMetrics

2. Select from eclipseWindowChoose metricsShow View | other....

3. SelectMetrics | metrics ViewOpen the window shown in 13. You need to use the Java perspective and re-build the project to display these metric values.
Figure13.OpenEclipseInMetrics View

4. ClickOKTo display the window in 14.
In this example, I am viewing the circle complexity of a separate method. You can double-click the method in the metrics list to open the source code for this method in the eclipse editor. This makes the correction super simple (if needed )!
Figure14.View the circle complexity of a Method

As I mentioned earlier, the Eclipse Metrics Plug-In also provides many powerful metrics that help you improve Code while developing software-visible, it is a plug-in the progressive programming sense!

The right is the best

As you can see in this article, the "five" measurement methods, namely coding standards, code duplication, code coverage, dependency analysis, and complexity monitoring, it is very important to improve the code quality. But what suits you is good. Remember that there are many other available Eclipse plug-ins (such as PMD and findbugs) that can help you improve code quality early in the development cycle. Whatever the tool or preferred method you want, it is important to take actions to actively improve the code quality and make the manual code verification process more effective. I guess you can't leave these plug-ins any longer after using them.

References

Learning

· For more information, see the original article on the developerworks global site.

·Automate Development(Paul Duvall, developerworks): Read the complete series.

· "Improving code quality with PMD and Eclipse" (Levent gurses, jacozi, November January 2005): This article regards PMD as an Eclipse plug-in, this article introduces how to improve the Code Quality and shorten the code verification process by using PMD.

· "Measuring test coverage with cobertura" (elliotte Rusty Harold, developerworks, May 2005): elliotte Rusty Harold shares his experience on how to leverage cobertura with best practices for code coverage.

· "Do not be confused by coverage reports" (Andrew Glover, developerworks, February January 2006): This article further reveals the true meaning of the numbers in the coverage report, it also gives the meaning that these numbers cannot represent.

· "Managing Your dependencies with jdepend" (Glen Wilcox, onjava, November January 2004): In this article, Glen Wilcox introduced jdepend, a free tool, it provides insight into many quality problems in the software architecture.

· "Code quality of software architecture" (Andrew Glover, developerworks, February April 2006): Andrew Glover describes how to continuously monitor and correct code quality problems that affect the long-term viability of software architecture.

·"Automate Development: Continuous check "(Paul Duvall, developerworks, August 2006): Paul Duvall describes automated check tools (such as checkstyle, javancss, and CPD) is how to enhance the development process and when to use these tools.

· "Detecting duplicate code with PMD's CPD" (Tom Copeland, onjava, March 2003): Tom Copeland introduced an open-source tool called CPD (copy/paste detector, this tool is used to find repeated Java code.

· "Maintain organizational standards with code audits" (testearly.com): coding standards help developers reach consensus on code libraries.

· Developerworks Java Technology Zone: hundreds of articles on Java programming.

Obtain products and technologies

· Eclipse plugin for PMD: Find the copy and paste problems in the code using the PMD plug-in.

· Eclipse plugin for jdepend: This plugin helps you analyze the package dependencies in the code base.

· Eclipse plugin for cobertura: coverlipse is the Eclipse plug-in for cobertura. It provides code coverage information.

· Eclipse Metrics: this plug-in provides metric values, such as sphere complexity, which is very helpful for searching for complex code.

· Eclipse plugin for checkstyle: check the compliance of Project coding standards.

Discussion

· Forum on improving Code Quality: Andrew Glover, an active contributor to developerworks, is a consultant focusing on improving code quality. He brings a lot of professional knowledge to this forum hosted by him.

    Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.