Apex PMD | Static code analysis

PMD is very well known source code analyzer for Java and many more languages. Its also supports Apex. In this session/article we are going to make a static code review for salesforce Apex code using the PMD static code analyzer. Static code analysis is a method for automatically analyzing some source code without executing it.

What is PMD?

PMD stand for Programming Mistake Detector. It is an open source static source code analyzer that reports on issues found within application code. It finds common programming flaws like unused variables, empty catch blocks. It will allow us to have a better quality and avoid maintenance, performance and bug problems in our Apex code.

In this session we will also talk about Apex PMD extension for VsCode. and how to setup Apex PMD.

Setup PMD

  • Download the PMD from here.
  • Extract the zip-archive, e.g. to C:\pmd-bin-6.30.0
  • Add folder C:\pmd-bin-6.30.0\bin to PATH, either
    1. Permanently: Using System Properties dialog > Environment variables > Append to PATH variable
    2. Temporarily, at command line: SET PATH=C:\pmd-bin-6.30.0;%PATH%
  • Execute below command
pmd.bat -d "C:\ApexPMD\MyProject\src\classes" -f html -R "category/apex/design.xml" -reportfile "C:\ApexPMD\output.html"

Customize Apex PMD Rules

You can make your own ruleset file in which you can define a bunch of standard rules and custom rules. Then distribute it to other developers so that they must follow the standards while writing code. Here is sample ruleset.xml file.

<?xml version="1.0" encoding="UTF-8"?>
<ruleset xmlns="http://pmd.sourceforge.net/ruleset/2.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="Default ruleset used by the CodeClimate Engine for Salesforce.com Apex" xsi:schemaLocation="http://pmd.sourceforge.net/ruleset/2.0.0 https://pmd.sourceforge.io/ruleset_2_0_0.xsd">
    <description> Custom Rule Set </description>

    <rule ref="category/apex/bestpractices.xml" />
    <rule ref="category/apex/documentation.xml" />
    <!--excluding some rules -->
    <rule ref="category/apex/codestyle.xml">
	<exclude name="WhileLoopsMustUseBraces"/>    
    </rule>

</ruleset>

Execute below command to test custom ruleset

pmd.bat -d "C:\ApexPMD\MyProject\src\classes" -f html -R "C:\ApexPMD\ruleset.xml" -reportfile "C:\ApexPMD\output.html"

Recording

Learn about Apex PMD Tool. Here is Agenda :

  1. What is PMD
  2. Why do we need tools like PMD (Enforce Clean Code, Security, Agility, Automation)
  3. How to start and how to leverage
  4. Questions & Answers

Here is recording of session.


Speaker           :  Robert Sösemann
Date                 :  Saturday, 27 July 2019 10:00 AM EST ( 7:30 PM IST)


Don’t forget to check Robert Sösemann pluralsight course on “Code Analysis in Apex” freely available to everybody. Please note that we have limit of 100 attendees that can join the online sessions. However, recording will be posted on our YouTube channel. Make sure to subscribe our YouTube channel to get notification for video upload.

Check our “Session in 2019” page for all upcoming and old sessions of 2019.

Sharing is Caring so Share with your friends

1 thought on “Apex PMD | Static code analysis”

Leave a Comment