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.

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
  2. Click Add Button
  3. Populate Name with PMD and location as http://codescan.villagechief.com/eclipse/
  4. Select Apex PMD by expanding the tree.
  5. Click Next and accept the agreement.
  6. 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
  2. Provide the Project Name, Username and Password (From which org you need to retrieve the components)
  3. Click Next.
  4. On Project Content Pages, Choose Apex Class. Click Finish.
  5. Once the Class from the org is retrieved, right click on the Class Folder.
  6. ApexPMD [Symbol] Run ApexPMD.
programming mistake detector
  1. The results will be displayed in the Apex PMD View Tab.
programming mistake detector

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

programming mistake detector

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.

programming mistake detector

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.

programming mistake detector

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.

programming mistake detector

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
  2. Extract the Zip File.
  3. 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”

programming mistake detector

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: 

https://github.com/pmd/pmd

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

Release Process

Release Process is a structured process with planning, scheduling, and controlling the project in each stage or environments with testing and deployments. It makes deployment stable and smooth.

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.