Java Code Quality Test Evaluation tool (with Eclipse plug-in) __java

Source: Internet
Author: User
Tags coding standards checkstyle

before the words are reproduced

Appfuse integrates a number of code quality control tools that are integrated into MAVEN to facilitate automated testing.

See this article today, is the Eclipse plug-in form, can be found in the development phase of the problem, compared to the whole of MAVEN to run, for each developer still need to be more convenient, there is value for reference.

I'll write a maven article when I'm not busy. Oh...

The following is reproduced

What if you could discover the potential problems in your code before you built it? Interestingly, there are tools in the Eclipse plug-in, such as JDepend and Checkstyle, that can help you discover these problems before a software problem is exposed. In this installment of development automation, the automation expert Paul Duvall will bring some examples of Eclipse plug-ins that you can install, configure, and use to prevent problems early in the development lifecycle.

about this series
As a developer, our job is to automate processes for end users, but many of us overlook the opportunity to automate our own development process. To this end, I have written a series of articles to automate the development process, specifically to explore the practical application of the automation of software development processes, and to teach you when and how to successfully apply automation.

One of my main goals when developing software is to either prevent the defect from being introduced into the code base or to limit the lifetime of the defect, in other words, to find the defect as soon as possible. Obviously, the more you know how to write better code and how to test the software effectively, the more you can catch bugs early. I also want a safety net that can detect potential flaws.

In the August installment of this series, I came to the conclusion that integrating the test tools into the build process (for example, using ANT or Maven) can create a way to find potential flaws. Although this approach makes consistency possible and goes beyond the IDE, it has a bit of a reaction. You must build the software locally or wait for the continuous integration build to run. If you use the Eclipse plug-in, you can find some of these conflicts before you build or integrate through continuous integration. This is what I call progressive programming, which allows for a certain level of quality testing in the coding process-no longer than this.

This article covers what I think is the "five Big" code Analysis area: Coding standard Code Repeat Code coverage dependency Analysis complexity monitoring

You can use the next few flexible Eclipse plug-ins to uncover these areas of analysis: Checkstyle: CPD for coding standard PMD: Helping find code duplication coverlipse: Measuring code Coverage JDepend: providing dependency analysis Eclipse Metric Plugins: Effectively detect complexity

Eclipse is not your build system
Using the Eclipse plug-in is not inconsistent with the use of these test tools for your build process. In fact, you want to make sure that the following rules that use the Eclipse plug-in apply to the rules in the build process.

Install Eclipse Plug-in

Installing the Eclipse plug-in is simple enough, and it takes only a few steps. Before you begin, it is a good idea to have the URL of the plugin download site ready. Table 1 is a list of the plug-ins used in this article:


table 1. Code improvement plug-in and corresponding download site URL

Tools Purpose URL of Eclipse plug-in
Checkstyle Coding standard Analysis http://eclipse-cs.sourceforge.net/update/
Coverlipse Test Code Coverage Http://coverlipse.sf.net/update
Cpd Copy/Paste Inspection http://pmd.sourceforge.net/eclipse/
JDepend Package Dependency Analysis http://andrei.gmxhome.de/eclipse/
Metrics Complexity monitoring Http://metrics.sourceforge.net/update

When you know the download address of these useful plug-ins, installing plug-ins is a very simple process. Start Eclipse, and then follow these steps: Select Help | Software Updates | Find and Install, as shown in Figure 1:



Figure 1. Find and install the Eclipse plug-in

Select the Search for new features to install radio button, click Next . Click New Remote Site , enter the name and URL of the plug-in you want to install (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, there is an option to view the various aspects of the plug-in. I usually select the top-level item, as shown in Figure 3. Select the options you want and click Finish . Eclipse now installs the plug-in. You need to restart the Eclipse instance.



Figure 3. Install Eclipse plug-in

Follow these steps to install additional Eclipse plug-ins, simply by changing the plugin name and the corresponding download location.




back to the top of the page

Calibration standard with Checkstyle

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

about PMD
Another Open-source tool, called PMD, provides similar functionality as Checkstyle. I prefer Checkstyle, but PMD also has a lot of followers, so I suggest you take a look at this tool, which, after all, is quite popular with some people.

The benefit of using Eclipse's Checkstyle plug-in is the ability to understand the various coding conflicts in the source code context during the coding process, making it more likely that developers will really deal with these conflicts before they check in the code. You can also almost see the Checkstyle plug-in as a continuous code review tool.

Install the Checkstyle plug-in and make the following configuration (see Figure 4): Select Project and select the Properties menu item in the Eclipse menu. Select Checkstyle Active for this project check box and click OK .



Figure 4. Configuring the Checkstyle plug-in in Eclipse

Eclipse rebuilt the workspace and listed the discovered coding conflicts in the Eclipse console, as shown in Figure 5:


Figure 5. Code conflict list for Checkstyle in Eclipse

Embedding a coded standard test in Eclipse using the Checkstyle plug-in is a great way to actively improve code while coding, thereby discovering potential flaws in the source code early in the development cycle. There are more benefits to this, such as saving time, reducing failure, and thus reducing the cost of the project. Yes, this is a positive way.




back to the top of the page

Confirm coverage with Coverlipse

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 can evaluate code coverage when you write code. Have you ever seen a pattern like this?

By selecting the Eclipse menu item run to install the Coverlipse plug-in and associating it with junit, this action displays a series of run configuration options, such as JUnit, SWT, and Java™ applications. Right-click it and select New in the JUnit w/coverlipse node. Here, you need to determine the location of the JUnit test, as shown in Figure 6:


Figure 6. Configuring Coverlipse to get Code coverage

Once you click Run , Eclipse runs Coverlipse and embeds the tag in the source code (shown in Figure 7), which shows the part of the code with the associated JUnit test:


Figure 7. Coverlipse-generated reports with embedded class tags

As you can see, using the Coverlipse Eclipse plug-in allows you to determine code coverage more quickly. For example, this real-time data feature helps to better test your code before it is checked into a CM system. What does this mean for progressive programming?




back to the top of the page

Capturing code duplication with CPD

Eclipse's PMD plug-in provides a feature called CPD (or copy-and-paste probes) that is used to find duplicate code. To use this handy tool in Eclipse, you need to install the Eclipse plug-in with PMD, which has CPD capabilities.

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


Figure 8. Run the copy and paste test using the CPD plug-in

Once you've run CPD, your Eclipse root will create a "show" folder that contains a file called Cpd.txt, with all the duplicated code listed in the file. An example of a cpd.txt file is shown in Figure 9:


Figure 9. CPD text file generated by the Eclipse plug-in

Looking for duplicate code manually is a challenge, but using plug-ins like CPD can easily find duplicate code when coded.




back to the top of the page

Using JDepend for dependency checking

JDepend is a free open source tool that provides object-oriented metrics for package dependencies to indicate the resilience of the code base. In other words, JDepend can effectively measure the robustness of an architecture (and vice versa).

In addition to the Eclipse plug-in, JDepend also provides an Ant task, a Maven plug-in, and a Java application to get these metrics. They have different delivery mechanisms for the same information, but the special and the advantage of the Eclipse plug-in is that it delivers this information in a way that is closer to the source code (that is, when it is encoded).

Figure 10 illustrates the use of the Eclipse JDepend plug-in by right-clicking the source folder and choosing Run JDepend analysis. Be sure to select a source folder with source code, otherwise you will not see this menu item.


Figure 10. Using JDepend Analysis Code

Figure 11 shows the report that was generated when the JDepend analysis was run. The package is displayed on the left and the dependency measure for each package is displayed to the right.


Figure 11. Package dependencies in the Eclipse project

As you can see, the JDepend plug-in provides a great deal of information to help you keep up to date on the changes in architecture maintainability--the biggest benefit is that you see this data as you encode it.




back to the top of the page

Measurement of complexity with Metrics

The final item of "Five big" code analysis is the measurement of complexity. Eclipse provides a plug-in called Metrics that can be used for many useful code metrics, including cyclomatic complexity metrics, to measure the number of unique paths in a method.

Install the Metrics plug-in and restart Eclipse, and then follow these steps: Right-click your project and select the Properties menu. In the Results window, select the Enable Metrics plugin check box and click OK , as shown in Figure 12:



Figure 12. Configuring Metrics for a project

Select the Window menu from Eclipse to open Metrics view and select Show View | Other ... 。 Select Metrics | Metrics View Opens the window shown in Figure 13. You need to use the Java perspective and rebuild the project to display these measures.



Figure 13. Open Metrics View in Eclipse

Click OK to display the window shown in Figure 14.

In this case, I'm looking at the cyclomatic complexity of a single method. The real good thing is that you can double-click the method in the Metrics list, which opens the source code for this method in the Eclipse editor. This makes the fixes super simple (if needed).



Figure 14. Viewing the cyclomatic complexity of a method

As I mentioned earlier, the Eclipse Metrics plug-in also offers a number of powerful metrics that help you improve your code during the development of your software--which is a progressive programmatic plug-in!




back to the top of the page

The right one is the best.

As you can see from this article, it is important to improve the quality of your code by using the "five Big" measurement methods-coding standards, code duplication, code coverage, dependency analysis, and complexity monitoring. But the one that suits you is good. Remember that there are many other Eclipse plug-ins available, such as PMD and FindBugs, that can help you improve code quality early in the development cycle. Regardless of the tool or preferred method you want, it is important to act aggressively to improve the quality of your code and make the process of manual code validation more effective. I reckon you can use these plug-ins for a while, and you'll never be separated from them.

Access to products and technology
Eclipse plugin for PMD: Use the PMD plug-in to find a copy and paste problem in your code. Eclipse plugin for JDepend: This plug-in helps you analyze package dependencies in a code base. Eclipse Plugin for Cobertura:coverlipse is the Eclipse plug-in for Cobertura, which provides code coverage information. Eclipse Metrics: This plugin provides metrics, such as cyclomatic complexity, that are very helpful in finding complex code. Eclipse Plugin for Checkstyle: Verify compliance with Project coding standards.

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.