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!

Programming Mistake Detector (PMD)


Programming Mistake Detector (PMD) is a tool used to detect common mistakes made in the code by defining the rule sets. We can use the standard rules or we can define custom rules. We can also use PMD with Eclipse, but we don’t need to define the Rule set.

Features:

Using PMD we can detect the common possible flaws:

  1. Naming Conventions
  2. Unused variables, parameters
  3. High Cyclomatic Complexity
  4. DML Operations within loop
  5. SOQL In Loops
  6. Test Class without Asserts
  7. Sharing Violations

PMD With Eclipse 

Steps to add PMD with Eclipse 

  1. Go to Help [Symbol] Install New Software
  1. Click Add Button
  1. Populate Name with PMD and location as http://codescan.villagechief.com/eclipse/
  1. Select Apex PMD by expanding the tree.
  1. Click Next and accept the agreement.
  1. Restart Eclipse.

To check the Mistakes in Eclipse by PMD, follow the steps given below

  1. Go to File [Symbol] New [Symbol] Force.com Project
  1. Provide the Project Name, Username and Password (From which org you need to retrieve the components)
  1. Click Next.
  1. On Project Content Pages, Choose Apex Class. Click Finish.
  1. Once the Class from the org is retrieved, right click on the Class Folder.
  1. ApexPMD [Symbol] Run ApexPMD.

  1. The results will be displayed in the Apex PMD View Tab.

Rule Sets 

Braces 

This Rule set detects the code that is written without Braces for If statement, While Statements and For Loops. This rule set has the Priority 3.

For Example:

public class DemoClass{ 
//Code to show Rule set for Braces in apex PMD 
public void BracesDemoMethod(List<Account> newAccountList){ 
Integer i; 
for(Account acc : newAccountList){ 
if(acc.Industry == 'Government') 
acc.Description = 'Account Related to Government'; 
} 
for(i = 0; i < 5; i++) 
InProgressEntry(); 
} 
public void InProgressEntry() 
{ 
} 
}

PMD Result for the above code

Complexity 

Complexity Rule set is used to detect the code size or complexity which includes Deeply Nested If Statements, Excessive Parameter List, excessive Class Length, Standard Cyclomatic Complexity, etc. This rule set has the priority of 3.

Performance 

Performance rule set is used to detect whether the code is written by following the best practices or not. Best Practices include SOQL inside Loop, DML inside Loop. This rule set has the Priority 3.

Style 

By using Style rule set, PMD will detect the Naming Conventions for Class, Methods and Variables used in the class, Logic implemented in Apex Trigger. The Priority for the rule set is 1.

PMD Without Eclipse: 

We can also use PMD without adding the Plugin to Eclipse. To use PMD without Eclipse, follow the steps given below,

  1. Download PMD from PMD 5.6.0 Snapshot
  1. Extract the Zip File.
  1. Download the apex rule set from ApexRuleSet.Xml

To Run PMD From Command Prompt

Command to Run PMD: 

pmd.bat -dir <source_code_root_path> -rulesets <ruleset_xml_file_path> -format <report_format_type> -reportfile <report_file_location>

Example:

pmd.bat -dir “C:\0\Code\Source Code ” -rulesets “C:\0\Tools\SFDC\apex-ruleset.xml” -format html -reportfile “C:\0\Tools\SFDC\report.html”

Conclusion:

Using PMD, we can automatically detect the common flaws which will reduce the time for code review. Only PMD version 5.6.0 and above support apex. To use PMD in command prompt, we need Java 8 run time Environment.

Reference Link: 

http://pmd.sourceforge.net/snapshot/pmd-apex/rules/apex/apexunit.html

https://github.com/pmd/pmd