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:
- Naming Conventions
- Unused variables, parameters
- High Cyclomatic Complexity
- DML Operations within loop
- SOQL In Loops
- Test Class without Asserts
- Sharing Violations
PMD With Eclipse
Steps to add PMD with Eclipse
- Go to Help [Symbol] Install New Software
- Click Add Button
- Populate Name with PMD and location as http://codescan.villagechief.com/eclipse/
- Select Apex PMD by expanding the tree.
- Click Next and accept the agreement.
- Restart Eclipse.
To check the Mistakes in Eclipse by PMD, follow the steps given below
- Go to File [Symbol] New [Symbol] Force.com Project
- Provide the Project Name, Username and Password (From which org you need to retrieve the components)
- Click Next.
- On Project Content Pages, Choose Apex Class. Click Finish.
- Once the Class from the org is retrieved, right click on the Class Folder.
- ApexPMD [Symbol] Run ApexPMD.
![programming mistake detector](https://www.mstsolutions.com/wp-content/uploads/2020/12/image-1027.png)
- The results will be displayed in the Apex PMD View Tab.
![programming mistake detector](https://www.mstsolutions.com/wp-content/uploads/2020/12/image-1028.png)
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](https://www.mstsolutions.com/wp-content/uploads/2020/12/image-1026.png)
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](https://www.mstsolutions.com/wp-content/uploads/2020/12/image-1025.png)
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](https://www.mstsolutions.com/wp-content/uploads/2020/12/image-1030.png)
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](https://www.mstsolutions.com/wp-content/uploads/2020/12/image-1029.png)
PMD Without Eclipse:
We can also use PMD without adding the Plugin to Eclipse. To use PMD without Eclipse, follow the steps given below,
- Download PMD from PMD 5.6.0 Snapshot
- Extract the Zip File.
- 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](https://www.mstsolutions.com/wp-content/uploads/2020/12/image-1031.png)
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: