Improve code quality with Eclipse plug-ins

Source: Internet
Author: User
Tags coding standards checkstyle

What if you could find potential problems in your code before building your code? Interestingly, there are tools such as JDepend and CheckStyle in the Eclipse plugin that can help you find these issues before software problems are exposed. In this installment of Development Automation , the automation expert Paul Duvall will introduce 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 the process 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 for development automation that explores the practical application of software development process automation and teaches you when and how to successfully apply automation.

One of my main goals when developing software is to either prevent the introduction of defects into the code base or limit the lifetime of defects, in other words, to find defects as early as possible. It is clear that the more you understand how to write better code and how to test software effectively, the more quickly you can catch defects. I also want a safety net that can uncover potential flaws.

In the August installment of this series, I came to the conclusion that integrating inspection 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 transcends the IDE, it has a bit of a backlash . 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 leads me to a programming approach called Progressive programming , which allows for a certain degree of quality testing in the coding process--no more than this!

This article covers what I think of the "five Big" code analysis areas:

    • Coding standards
    • Code duplication
    • Code Coverage
    • Dependency analysis
    • Monitoring of complexity

You can use the next few flexible Eclipse plugins to uncover these areas of analysis:

    • CheckStyle: For coding standards
    • PMD CPD: Help Find code duplication
    • Coverlipse: Measure code Coverage
    • JDepend: Providing dependency analysis
    • Eclipse Metric Plugin: Effectively isolate complexity
Eclipse is not your build system

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

Installing the Eclipse Plugin

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

Table 1. Code improvement plug-ins and corresponding download site URLs
Tools Purpose the URL of the Eclipse plugin
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 Monitoring of complexity Http://metrics.sourceforge.net/update

Once you know these useful plugins, installing the plugin is a very simple process. Start Eclipse, and then follow these steps:

    1. Choose Help | Software Updates | Find and Install, shown in 1:

      Figure 1. Find and install the Eclipse plugin
    2. Select the Search for new features to install radio button and 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 feature. I usually select top-level items, as shown in 3. Select the options you want and click Finish. Eclipse now installs the plugin. You need to restart the Eclipse instance.

      Figure 3. Installing the Eclipse Plugin

Follow these steps to install additional Eclipse plugins, just change the plugin name and the appropriate download location.

Calibration standard with CheckStyle

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

About PMD

Another open source tool called PMD provides functionality similar to CheckStyle. I prefer CheckStyle, but PMD also has a lot of persistent followers, so I suggest you look at this tool, after all, it is quite popular with some people.

The benefit of using Eclipse's CheckStyle plug-in is the ability to understand the various encoding conflicts in the source context during encoding, making it more likely that developers will actually handle these conflicts before checking in the code. You can also view the CheckStyle plugin as a continuous code review tool!

Install the CheckStyle plugin and make the following configuration (see Figure 4):

    1. Select Project, and then select the Properties menu item in the Eclipse menu.
    2. Select the CheckStyle active for this project check box, and click OK.

      Figure 4. Configuring the CheckStyle plug-in in Eclipse

Eclipse re-builds the workspace and lists the discovered encoding conflicts in the Eclipse console, as shown in 5:

Figure 5. List of code conflicts CheckStyle in Eclipse

Embedding an encoding standard test in Eclipse with the CheckStyle plugin is a great way to aggressively improve the code at the time of coding to uncover potential flaws in the source code early in the development cycle. There are many more benefits to this, such as saving time, reducing failures, and thus reducing the cost of the project. Yes, this is a proactive way!

Confirm coverage with Coverlipse

Coverlipse is an Eclipse plug-in for Cobertura, and Cobertura is a code coverage tool that you can use to evaluate the ratio of source code with corresponding tests. Cobertura also provides an Ant task and Maven plugin, but with Cobertura, you can evaluate code coverage when writing 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, the operation displays a series of run configuration options, such as JUnit, SWT applications, 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 6:

Figure 6. Configure Coverlipse to get code coverage

Once you click Run, Eclipse runs Coverlipse and embeds the tag in the source code (7), which shows the part of the code that has the relevant JUnit tests:

Figure 7. Coverlipse generated reports with embedded class tags

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

Repeat with CPD capture code

Eclipse's PMD plug-in provides a feature called CPD (or copy-paste probe) that is used to look for 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, shown in 8:

Figure 8. Run a copy-and-paste test using the CPD plugin

Once the CPD is run, a folder is created in your Eclipse root directory report that contains a file called Cpd.txt that lists all the duplicated code in the file. Figure 9 is an example of a cpd.txt file:

Figure 9. CPD text files generated by the Eclipse plugin

Searching for duplicate code by hand is a challenge, but using a plugin like CPD makes it easy to find duplicate code when coding.

Using JDepend for dependency checking

JDepend is a free-to-access open source tool that provides object-oriented metrics for package dependencies to indicate the resilience of the codebase. In other words, JDepend can effectively measure the robustness of an architecture (conversely, fragility).

In addition to the Eclipse plugin, JDepend also provides an Ant task, a Maven plugin, and a Java application to get these metrics. They have different delivery mechanisms for the same information, but the particular point of the Eclipse plugin and its corresponding advantage is that it can pass this information in a way that is closer to the source code (i.e., when encoded).

Figure 10 illustrates how to use the Eclipse JDepend plugin by right-clicking the source folder and selecting Run JDependanalysis. Be sure to select a source folder with source code, otherwise you will not see this menu item.

Figure 10. Analyzing code using JDepend analysis

Figure 11 shows the report that was generated when you ran the JDepend analysis. The package appears on the left and the dependency metric for each package appears on the right.

Figure 11. Package dependencies in an Eclipse project

As you can see, the JDepend plug-in provides a lot of information that helps you keep observing changes in the architecture's maintainability-the biggest benefit is that you'll see that data when you encode.

Measurement of complexity with Metrics

The last item in the "Five Big" code analysis is the complexity of the measurement. Eclipse provides a plug-in called Metrics that can be used to make many useful code measurements, including cyclomatic complexity measures, which are used to measure the number of unique paths in a method.

Install the Metrics plugin and restart Eclipse, and then follow these 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 12:

      Figure 12. Configuring Metrics for a project
    2. From Eclipse, select the Window menu to open the Metrics view and select Show View | Other ....
    3. Choose Metrics | Metrics View Opens the window shown in 13. You need to display these metrics by using the Java perspective and rebuilding the project.

      Figure 13. Open the Metrics View in Eclipse
    4. Click OK to display the window in 14.

      In this example, 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 fix super simple (if needed)!

      Figure 14. View the cyclomatic complexity of a method

As I mentioned before, the Eclipse Metrics plugin also provides a number of powerful metrics to help you improve your code as you develop your software-visible, it's a plug-in in a progressive programming sense!

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 it's good for you. Keep in mind that there are many other Eclipse plugins available, such as PMD and FindBugs, that can help you improve code quality early in the development cycle. No matter what tool or preferred method you want, it's important to take action to actively improve the quality of your code and make the process of manual code validation more effective. I estimate that you can use these plugins for a period of time and you can never leave them again.

Improve code quality with Eclipse plug-ins

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.