Automate development and improve code quality with Eclipse plug-ins

Source: Internet
Author: User
Tags coding standards checkstyle

In this phase of automating Development 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.

When developing software, one of my main goals is to either prevent defects from being introducedCodeLibrary, 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 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 seriesMarchIn that article, 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 is also somewhat counterproductive. 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 find some such conflicts before building or integrating through the continuous integration. This leads to a programming method called Progressive programming. In this way, a certain degree of quality test is allowed in the coding 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 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.

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 eclipse checkstyle plug-in is that you can understand the Encoding ProcessSource codeVarious encoding conflicts in the context make it more likely for developers to 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. This method can be used to actively improve the code During encoding, in this way, potential defects in the source code are discovered 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 corresponding tests. Cobertura also provides an ant task and Maven plug-in, but with cobertura, you can evaluate code coverage when writing code. 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 and SWT applications.ProgramAnd 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 better test the code before it is checked into the CM system. 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 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.

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

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.