You are using an older browser that might negatively affect how this site is displayed. Please update to a modern browser to have a better experience. Sorry for the inconvenience!

PMD (Standard Rule Customization)


Programming Mistake Detector (PMD) is one of the source code analyzer tools available in the market. PMD detects empty catch blocks, unnecessary objects and common programming mistakes like unused variables etc. It supports Java, JavaScript, Salesforce.com Apex and Visualforce, XML, PLSQL, and XSL. PMD also performs the CPD (Copy Paste Detector).  CPD finds the duplicate code in Java, JavaScript, C, C++, PHP, PLSQL, Salesforce.com Apex and Visualforce. Using PMD, we can run static code analysis standalone. PMD can be part of ANT build to generate error reports.

PMD detects source code issues in Apex and other languages and looks for unexpected problems like Possible bugs, Dead code, Suboptimal code, Over complicated expressions, and Duplicate code.

Requirements: 

Install the “Eclipse IDE” software from Google.

Supported Versions: 

Eclipse version 4.4 and later versions.

Implementation:

Steps:

1) Once the “Eclipse” is installed, we must install “PMD” latest version, Eclipse [Symbol] Help [Symbol] Install new software

2) Enable PMD using “Add Repository” and also add the location URL for PMD (it will be available in PMD website).

3) Choose the current PMD version for “Available software”, click the “PMD for eclipse 4” check box, and finally click the “Finish” button.

Once we have installed the PMD in Eclipse, PMD folder will be automatically created in our local system. The folder has rules, Sample and report files. We can store Apex classes and controllers in the Sample folder.

4) Choose the Apex class and right click select PMD[Symbol]Check Code, it will show the violation view.

5) Standard Rule Customization:

PMD provides some standard rule sets (ApexUnit, Braces, Complexity, Performance, Security, Style). The ruleset can check the code and show the error messages. We can run the ruleset using “Command Prompt”.

Example Standard rule set:

The following XML ruleset is used to find the deeply nested “if” statements in our Apex code.

<?xml version="1.0"?> 
<ruleset name="Apex Rules" xmlns="http://pmd.sourceforge.net/ruleset/2.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://pmd.sourceforge.net/ruleset/2.0.0 http://pmd.sourceforge.net/ruleset_2_0_0.xsd"> 
<rule name="AvoidDeeplyNestedIfStmts" message="Avoid creating deeply nested if-then statements since they are harder to read and error-prone to maintain." class="net.sourceforge.pmd.lang.apex.rule.complexity.AvoidDeeplyNestedIfStmtsRule"> 
      <description> 
      Avoid creating deeply nested if-then statements  
      </description> 
        <priority>3</priority>  
      <properties> 
<property name="problemDepth" value="3"/> 
<property name="cc_categories" value="Complexity"/> 
<property name="cc_remediation_points_multiplier" value="150"/> 
<property name="cc_block_highlighting" value="false"/> 
</properties> 
    </rule> 
</ruleset> 

Here, we have changed(Customize) the problemDepth, cc_categories, cc_remediation_points_multiplier and cc_block_highlighting values based on our requirements.

Once we have run the command prompt, the error messages will be shown in our browser, and create error file in report folder.

Error Message: 

This error message for the Apex classes will be shown as below:

6) CPD (Copy Paste Detector):

If any developer uses same code in multiple classes, then CPD will detect it. Eclipse [Symbol] Window [Symbol] Preferences [Symbol] PMD [Symbol] CPD Preferences [Symbol] Launch CPD