PMD: Improving Apex Code Quality

  1. Installation and configuration in Visual Studio Code

Software development is difficult to get right. Even the best developers struggle to avoid defects and consistently produce high-quality work. Resolving those defects gets more expensive the longer they remain in the code. Studies show a 500 - 2,000% cost reduction in fixing a defect during coding, as opposed to in production.

“Static analysis” is a process that automatically identifies potential defects in code. PMD is a free static analysis tool that works with Apex code, will help you find bugs, and make you a better coder!

This is the first in a series of posts on how to utilize PMD within your Visual Studio Code projects.

Installation

  1. Download the latest version of PMD
  2. Extract the zip file to a convenient location. The default folder name will be something like “pmd-bin-6.22.0”, but I like to shorten that to “pmd”. My preference is to put this folder inside a “Tools” folder, which is a sibling of my project folders.
  3. Open your Salesforce project in Visual Studio Code.
No alt text provided for this image
  1. Navigate to the Extensions panel (View | Extensions), search for “Apex PMD” by Charlie Jonas, and click “Install.” See Charlie’s GitHub repository for more information on his plugin.


Configuration

  1. In Settings (Code | Preferences | Settings), search for “PMD” and set “Pmd Bin Path” to the location of the folder you created in step 2.
No alt text provided for this image
  1. Adjust any other settings as desired. Priority thresholds: which issues will be reported as errors and which as warnings. Run on File Open / Run on File Save: these settings dictate when PMD will run on a file. Output options: specify whether you’d like to see warning and error output.
  2. In a later post, I’ll talk about rulesets and how to customize them.

Scanning Your Code

Now that PMD is installed and configured, running a scan will be simple. By default, a file will be scanned when you open or save it. In addition, there are a couple of ways to manually scan:

  • In the Command Palette (View | Command Palette), choose “Apex Static Analysis: On File” or “Apex Static Analysis: On Workspace”

Checking the results

No alt text provided for this image

PMD posts its results to the Problems panel (View | Problems). Click a row to navigate directly to the line with the problem. Stay tuned for a later post where I’ll talk about the meanings of various warnings and how to resolve or silence them.That’s it for now. Stay safe and happy coding!

***

Steve Cox is the Development Center of Excellence Lead at Xede Consulting Group, and a senior software developer with over 20 years of experience. Steve's thought leadership includes a book: Salesforce Defect Domination: A handbook for finding, fixing, and preventing defects in Force.com development, Dreamforce presentations, and blog posts.

To view or add a comment, sign in

More articles by Steve Cox

Others also viewed

Explore content categories