Java code quality detection and evaluation tools (using Eclipse plug-ins)

Source: Internet
Author: User
Tags coding standards checkstyle

Previous Reprinted Words

Appfuse integrates many code quality control tools, all of which are integrated in Maven to facilitate automated detection.

Today, I saw this article in the form of Eclipse plug-ins. problems can be found during the development period. Compared with the overall running of Maven, it is more convenient for every developer and has reference value.

I will write a Maven article when I am not busy. Haha !!!

 

Reprinted below

 

What if we can discover potential problems in the code before building the code? It is interesting that Eclipse plug-ins have such tools, such as jdepend and checkstyle, which can help you discover these problems before software problems are exposed. InAutomate DevelopmentIn this article, automation expert Paul Duvall will provide some examples about the Eclipse plug-in. You can install, configure, and use these static analysis plug-ins to prevent problems early in the development lifecycle.

About this series
As a developer, our job is to automate processes for end users. However, many of us have neglected the opportunity to automate our own development processes. For this reason, I wroteAutomate DevelopmentThis series of articles specifically explores the practical application of software development process automation and teaches youWhenAndHowApplication automation is successful.

When developing software, one of my main goals is to either prevent defects from being introduced into the code base, or limit the lifetime of the defects; in other words, identify the defects as soon as possible. Obviously, the more you understand how to write better code and how to effectively test software, the more you can capture defects as early as possible. I also want a security network that can discover potential defects.

In this series of articles in September, I came to the conclusion that the validation tool is integrated into the build process (for example, using ant or Maven, it 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 the Eclipse plug-in, you can build or integrate it through continuous integration.BeforeSome 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:

  • Encoding standard
  • Code already exists
  • 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 identify code duplication
  • Coverlipse: Measure code coverage
  • Jdepend: Provides dependency analysis
  • Eclipse metric Plugin: effectively detects complexity
Eclipse is not 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:


Table 1. Code improvement plug-ins and corresponding download site URLs

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:


    Figure 1. Find and install the Eclipse plug-in

  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.

    Figure 2. 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.

    Figure 3. Install the Eclipse plug-in

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

 



Back to Top

 

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 it is to modify the code, the easier it is to add new product features. Like checkstyle
Such tools can help you find areas that affect maintainability and conflict with coding standards, for example, too large classes, too long methods, and unused variables.

Information about PMD
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.

    Figure 4. Configure the checkstyle plug-in eclipse

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


Figure 5. checkstyle code conflict list in eclipse

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!

 



Back to Top

 

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 the corresponding test. Cobertura also provides an ant task and Maven plug-in, but uses
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:


Figure 6. Configure coverlipse to 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:


Figure 7. Reports generated by coverlipse 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?

 



Back to Top

 

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:


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

Once CPD is run,reportFolder, which contains a file called cpd.txt, which lists all repeated codes. Figure 9 shows an example of a cpd.txt file:


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

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

 



Back to Top

 

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 transmission mechanisms for the same information; however, eclipse
The plug-in has the following special advantages: it can pass this information in a closer way to the source code (that is, 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.


Figure 10. Analyze code using jdepend Analysis

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.


Figure 11. Package dependencies in the Eclipse 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.

 



Back to Top

 

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:


    Figure 12. Configure metrics for the project

  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.

    Figure 13. Open metrics view in eclipse

  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 )!

    Figure 14. 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!

 



Back to Top

 

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 suitable for you
Is good. Remember that there are many other available Eclipse plug-ins (such
Findbugs) helps you improve code quality early in the development cycle. Whatever the tool or preferred method you want, it is important to actively improve the Code Quality and enable manual code check.
The verification process becomes more effective. I guess you can't leave these plug-ins any longer after using them.

Obtain products and technologies

  • Eclipse plugin for PMD: Use the PMD plug-in to find the copy and paste problems in the code.
  • Eclipse plugin for jdepend: This plugin helps analyze the package dependencies in the code library.
  • Eclipse plugin for cobertura: coverlipse is an Eclipse plug-in for cobertura. It provides code coverage information.
  • Eclipse Metrics: this plug-in provides metric values, such as sphere complexity, which helps you find complex code.
  • Eclipse plugin for checkstyle: Checks 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.