PMD (Standard Rule Customization)

Share on facebook
Share on twitter
Share on linkedin
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. 

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

Programming Mistake Detector

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

Programming Mistake Detector

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

Programming Mistake Detector

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.

Programming Mistake Detector

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.

Programming Mistake Detector

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:

Programming Mistake Detector

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

Programming Mistake Detector

CPD also shows the classes which use same code from other classes.

Pros: 

  1. Run many rulesets at once.
  2. Execute with ANT tasks, maven Plugins, IDE plugins.
  3. Run on Files, Directories, jars and zip files.

Cons: 

  1. All rulesets on a single file.
  2. Cannot find runtime issues.
  3. Does not detect synchronization and threading issues.

Summary: 

This article illustrates the use of PMD tool and how to apply these things in our projects to analyze code and find the errors.

Reference Link:  

https://pmd.github.io/pmd-5.8.1/

About MST

At MST Solutions our cornerstone is to adapt, engage and create solutions which guarantee the success of our clients. The talent of our team and experiences in varied business verticals gives us an advantage over other competitors.

Recent Articles

Mobile Responsive Testing

Mobile Responsive Testing is simply a Testing Process performed to make sure that your website or an app is working appropriately on all types of devices.
Whether it an app or website, they need to ensure that they are accessible from any device, anytime, anywhere.

Read Article »

Work with us.

Our people aren’t just employees, they are key to the success of our business. We recognize the strengths of each individual and allow them time and resources to further develop those skills, crafting a culture of leaders who are passionate about where they are going within our organization.