Writing Apex code is easy. Writing good Apex code takes discipline. Thatβs where PMD comes in. Itβs a static code analysis tool that helps:
Before you start, make sure youβve got these set up:
Download PMD from the official site, extract it, and configure environment variables.
# Windows example
Variable name: PMD_HOME
Variable value: C:\pmd
# Add to PATH
%PMD_HOME%\bin
Verify with:
pmd --version
I used a rules.xml file located in /config/rules.xml.
It checks:
Run PMD from your terminal inside the project root folder:
pmd check --dir "C:\path\to\classes" --rulesets "C:\path\to\rules.xml" --format csv
To generate a CSV report:
pmd check --dir "C:\path\to\classes" --rulesets "C:\path\to\rules.xml" --format csv --report-file pmd-report.csv
The CSV report includes:
Example:
"File","Line","Rule","Priority","Description"
"ServiceAppointmentHandler.cls","45","AvoidDmlStatementsInLoops","1","DML inside loops can cause governor limit issues."
Priority ranges from 1 (Critical) to 5 (Info). My run generated 1,037 entries across multiple Apex classes in the Gigaclear org!
β PMD helps write cleaner Apex code.
β Custom rulesets enforce Salesforce best practices.
β Reports in CSV/XML/HTML make reviewing easy.
Integrating PMD in your workflow means fewer bugs, better performance, and a happier dev team π