March 1, 2021
Hot Topics:

Cleaner Code with the PMD Eclipse Plug-In

  • By Scott Nelson
  • Send Email »
  • More Articles »

Click here for a larger image.

Figure 3: Add the PMD URL to the Site List

From this point, the standard "Next, Next, Next" steps can be followed easily. Upon success, you will have a new perspective in your workbench:

Click here for a larger image.

Figure 4: The PMD Perspective

If you happen to use the PHP version of Eclipse, you will need to accept the restart Eclipse option or be annoyed by prompts telling you that your workspace is a mess.

Checking Your Code with PMD

Although PMD can be customized to meet your coding standards, you can start using it immediately after installation with the default configuration. For those who don't already have documented coding standards, these defaults can provide a good starting point.

PMD allows you to check code at any level available in the code view; in other words, at the project, package, or class level. The code check is run by right-clicking on the code level you wish to check and selecting Check Code with PMD from the PMD options:

Figure 5: PMD Options in Right-Click Menu

The location and number of violations are then displayed in the PMD view.

Click here for a larger image.

Figure 6: Check Code with PMD Results

You can drill down in the Violations Overview from the level you ran the check at all the way to individual line and violation description.

If you are introducing PMD mid-stream into a large project, the scan can take a long time. Once PMD has become part of your regular coding environment, getting in the habit of running a scan on each class you have created or updated prior to checking it into source control can save hours of bug hunting, not to mention reducing the possibility of embarrassing comments during code reviews.

After running a code check, the results can be exported by selecting Generate Reports from the PMD menu. A new folder will be added to your project named "reports" where the output will be available in several different formats.

Another cool feature is the ability to search for duplicate code with the Find Suspect Cut and Paste menu selection. The results of this search can help to pin down repeated code that should become part of a utility class.

Page 2 of 3

This article was originally published on December 4, 2008

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date