Let development automation use Eclipse Plug-ins to improve code quality _ related skills

Source: Internet
Author: User
Tags coding standards checkstyle
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.

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 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 duplication
    • Code Coverage
    • Dependency analysis
    • Complexity monitoring

You can use the next few flexible Eclipse plug-ins to uncover these areas of analysis:

    • Checkstyle: For coding standards
    • PMD CPD: Help Find code duplication
    • Coverlipse: Measuring Code Coverage
    • JDepend: Providing dependency analysis
    • Eclipse Metric Plug-ins: 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 plugin 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:

    1. Select Help | Software Updates | Find and Install, as shown in Figure 1:



      Figure 1. Find and install the Eclipse plug-in



    2. Select the Search for new features to install radio button, click Next.
    3. 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. Configure a new remote site



    4. 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 view the Checkstyle plugin as a continuous code review tool!

Install the Checkstyle plug-in and make the following configuration (see Figure 4):

    1. Select Project, and then select the Properties menu item in the Eclipse menu.
    2. 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 Newin the JUnit w/coverlipse node. Here, you need to determine the location of the JUnit test, as shown in Figure 6:


Figure 6. Configure 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 suspectcut 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, you'll create a folder in your Eclipse root directory that report contains a file called Cpd.txt, with all the duplicate 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 JDependanalysis. Be sure to select a source folder with source code, otherwise you will not see this menu item.


Figure 10. Using the 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, followed by the following steps:

    1. 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. Configure Metrics for the project



    2. Select from Eclipse window menu opens Metrics view and then selects Show View | Other ... .
    3. 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.







    4. Click OK to display the window shown in Figure 14.

      In this example, I am looking at the cyclomatic complexity of a separate 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. View 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.

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.