GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • PMD Source Code Analyzer Project
  • Nav
  • Download
  • GitHub
  • Edit on GitHub

This view

  • Original page
  • New address

GeezerVison is showing docs.pmd-code.org — Best Practices | PMD Source Code Analyzer

 
  • PMD 7.31.0-SNAPSHOT
  • Release date: 28-December-2026
  • About
    • Home
    • Release notes
    • Getting help
    • Release policies
    • Support lifecycle
    • Security
  • User Documentation
    • Migration Guide for PMD 7
    • Installation and basic CLI usage
    • Making rulesets
    • Configuring rules
    • Best practices
    • Suppressing warnings
    • Incremental analysis
    • PMD CLI reference
    • PMD Report formats
    • 3rd party rulesets
    • Signed Releases
    • CPD reference
      • Copy-paste detection
      • CPD Report formats
    • Extending PMD
      • Introduction to writing rules
      • Your first rule
      • XPath rules
      • Java rules
      • Rule designer reference
      • Defining rule properties
      • Rule guidelines
      • Testing your rules
      • Creating (XML) dump of the AST
    • Tools / Integrations
      • Maven PMD Plugin
      • Gradle
      • Ant
      • PMD Java API
      • bld PMD Extension
      • CI integrations
      • IDE Plugins
      • Other Tools / Integrations
  • Rule Reference
    • Apex Rules
      • Index
      • Best Practices
      • Code Style
      • Design
      • Documentation
      • Error Prone
      • Performance
      • Security
    • HTML Rules
      • Index
      • Best Practices
    • Java Rules
      • Index
      • Best Practices
      • Code Style
      • Design
      • Documentation
      • Error Prone
      • Multithreading
      • Performance
      • Security
    • Java Server Pages Rules
      • Index
      • Best Practices
      • Code Style
      • Design
      • Error Prone
      • Security
    • JavaScript Rules
      • Index
      • Best Practices
      • Code Style
      • Error Prone
      • Performance
    • Kotlin Rules
      • Index
      • Best Practices
      • Error Prone
    • Maven POM Rules
      • Index
      • Error Prone
    • Modelica Rules
      • Index
      • Best Practices
    • PLSQL Rules
      • Index
      • Best Practices
      • Code Style
      • Design
      • Error Prone
    • Salesforce Visualforce Rules
      • Index
      • Security
    • Scala Rules
      • Index
    • Swift Rules
      • Index
      • Best Practices
      • Error Prone
    • Velocity Template Language (VTL) Rules
      • Index
      • Best Practices
      • Design
      • Error Prone
    • WSDL Rules
      • Index
    • XML Rules
      • Index
      • Best Practices
      • Error Prone
    • XSL Rules
      • Index
      • Code Style
      • Performance
  • Language-Specific Documentation
    • Overview
    • Language configuration
    • Apex
    • C/C++
    • C#
    • CSS
    • Coco
    • Dart
    • Fortran
    • Gherkin
    • Go
    • HTML
    • Java
    • JavaScript / TypeScript
    • JSP
    • Julia
    • Kotlin
    • Lua
    • Matlab
    • Modelica
    • Objective-C
    • Perl
    • PHP
    • PLSQL
    • Python
    • Ruby
    • Rust
    • Scala
    • Swift
    • T-SQL
    • Visualforce
    • Velocity Template Language (VTL)
    • XML and XML dialects
  • Developer Documentation
    • Contributing
      • Contributing
      • Developer resources
      • Newcomers' Guide
      • Writing documentation
    • Building PMD
      • General Info
      • Building PMD from source
      • Building PMD with IntelliJ IDEA
      • Building PMD with Eclipse IDE
      • Building PMD with VS Code IDE
      • Building PMD with Netbeans IDE
    • Roadmap
    • GitHub Actions Workflows
    • How PMD works
    • Logging
    • Pmdtester
    • Rule Deprecation Policy
    • Major contributions
      • Rule Guidelines
      • Adding a new dialect
      • Adding a new language (JavaCC)
      • Adding a new language (ANTLR)
      • Adding a new CPD language
    • Experimental features
      • List of experimental Features
  • Project documentation
    • Trivia about PMD
      • PMD in the press
      • Products & books related to PMD
      • Similar projects
      • What does 'PMD' mean?
    • Logo
    • FAQ
    • License
    • Credits
    • Old Release Notes
      • Release Notes PMD 7.x
      • Detailed Release Notes PMD 7.0.0
      • Release Notes PMD 6.x
      • Release Notes PMD 5.x
      • Release Notes PMD 4.x and earlier
    • Decisions
    • Project management
      • Infrastructure
      • Release process
      • Merging pull requests
      • Main Landing page

Best Practices

Rules which enforce generally accepted best practices.
Table of Contents

ApexAssertionsShouldIncludeMessage

Since: PMD 6.13.0

Priority: Medium (3)

The second parameter of System.assert/third parameter of System.assertEquals/System.assertNotEquals is a message. Having a second/third parameter provides more information and makes it easier to debug the test failure and improves the readability of test output.

This rule is defined by the following Java class: net.sourceforge.pmd.lang.apex.rule.bestpractices.ApexAssertionsShouldIncludeMessageRule

Example(s):

@isTest
public class Foo {
    @isTest
    static void methodATest() {
        System.assertNotEquals('123', o.StageName); // not good
        System.assertEquals('123', o.StageName, 'Opportunity stageName is wrong.'); // good
        System.assert(o.isClosed); // not good
        System.assert(o.isClosed, 'Opportunity is not closed.'); // good
    }
}

Use this rule by referencing it:

<rule ref="category/apex/bestpractices.xml/ApexAssertionsShouldIncludeMessage" />

ApexUnitTestClassShouldHaveAsserts

Since: PMD 5.5.1

Priority: Medium (3)

Apex unit tests should include at least one assertion. This makes the tests more robust, and using assert with messages provide the developer a clearer idea of what the test does. Custom assert method invocation patterns can be specified using the ‘additionalAssertMethodPattern’ property if required.

This rule is defined by the following Java class: net.sourceforge.pmd.lang.apex.rule.bestpractices.ApexUnitTestClassShouldHaveAssertsRule

Example(s):

@isTest
public class Foo {
    public static testMethod void testSomething() {
        Account a = null;
        // This is better than having a NullPointerException
        // System.assertNotEquals(a, null, 'account not found');
        a.toString();
    }
}

This rule has the following properties:

Name Default Value Description
additionalAssertMethodPattern   A regular expression for one or more custom test assertion method patterns.

Use this rule with the default properties by just referencing it:

<rule ref="category/apex/bestpractices.xml/ApexUnitTestClassShouldHaveAsserts" />

Use this rule and customize it:

<rule ref="category/apex/bestpractices.xml/ApexUnitTestClassShouldHaveAsserts">
    <properties>
        <property name="additionalAssertMethodPattern" value="" />
    </properties>
</rule>

ApexUnitTestClassShouldHaveRunAs

Since: PMD 6.51.0

Priority: Medium (3)

Apex unit tests should include at least one runAs method. This makes the tests more robust, and independent from the user running it.

This rule is defined by the following Java class: net.sourceforge.pmd.lang.apex.rule.bestpractices.ApexUnitTestClassShouldHaveRunAsRule

Example(s):

@isTest
private class TestRunAs {
   public static testMethod void testRunAs() {
        // Setup test data
        // Create a unique UserName
        String uniqueUserName = 'standarduser' + DateTime.now().getTime() + '@testorg.com';
        // This code runs as the system user
        Profile p = [SELECT Id FROM Profile WHERE Name='Standard User'];
        User u = new User(Alias = 'standt', Email='standarduser@testorg.com',
        EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US',
        LocaleSidKey='en_US', ProfileId = p.Id,
        TimeZoneSidKey='America/Los_Angeles',
         UserName=uniqueUserName);

        System.runAs(u) {
              // The following code runs as user 'u'
              System.debug('Current User: ' + UserInfo.getUserName());
              System.debug('Current Profile: ' + UserInfo.getProfileId());
          }
    }
}

Use this rule by referencing it:

<rule ref="category/apex/bestpractices.xml/ApexUnitTestClassShouldHaveRunAs" />

ApexUnitTestClassShouldHaveRunRelevantTestsAnnotation

Since: PMD 7.28.0

Priority: Medium (3)

Apex unit test classes should declare either @IsTest(critical=true) or @IsTest(testFor='...') (Beta, Salesforce API v66.0+), used together with the RunRelevantTests deployment test level. Without one of these modifiers, a test class relies entirely on Salesforce’s automatic dependency detection to decide whether it runs during a RunRelevantTests deployment. For test classes covering critical paths, or classes that exercise code reached only through dynamic mechanisms (e.g. Type.forName, metadata-driven routing/dispatch), automatic detection can miss the dependency, and the test class may silently be skipped during a deployment even though the code it protects changed.

Classes whose meta.xml declares an API version older than 66.0 are not flagged, since these modifiers aren’t available to them. Classes whose API version couldn’t be determined are still flagged.

See more here: IsTest Annotation

This rule is defined by the following Java class: net.sourceforge.pmd.lang.apex.rule.bestpractices.ApexUnitTestClassShouldHaveRunRelevantTestsAnnotationRule

Example(s):

@isTest
private class OpportunityEODTest { // not good, no 'critical' or 'testFor' modifier
    @isTest
    static void validatesOrgWideBehavior() {
        // ...
    }
}

@isTest(critical=true) // good, explicitly marked as always relevant
private class OpportunityEODTest {
    @isTest
    static void validatesOrgWideBehavior() {
        // ...
    }
}

@isTest(testFor='ApexClass:InvoiceEngine, ApexTrigger:InvoiceTrigger') // good, explicitly tied to its dependencies
private class InvoiceDynamicFlowTests {
    @isTest
    static void validatesDynamicInvocationPath() {
        // ...
    }
}

Use this rule by referencing it:

<rule ref="category/apex/bestpractices.xml/ApexUnitTestClassShouldHaveRunRelevantTestsAnnotation" />

ApexUnitTestMethodShouldHaveIsTestAnnotation

Since: PMD 6.13.0

Priority: Medium (3)

Apex test methods should have @isTest annotation instead of the testMethod keyword, as testMethod is deprecated. Salesforce advises to use @isTest annotation for test classes and methods.

This rule is defined by the following XPath expression:

//Method[ModifierNode[@DeprecatedTestMethod = true()]]

Example(s):

@isTest
private class ATest {
    @isTest
    static void methodATest() {
    }
    static void methodBTest() {
    }
    @isTest static void methodCTest() {
        System.assert(1==2);
    }
    static testmethod void methodCTest() {
        System.debug('I am a debug statement');
    }
    private void fetchData() {
    }
}

Use this rule by referencing it:

<rule ref="category/apex/bestpractices.xml/ApexUnitTestMethodShouldHaveIsTestAnnotation" />

ApexUnitTestShouldNotUseSeeAllDataTrue

Since: PMD 5.5.1

Priority: Medium (3)

Apex unit tests should not use @isTest(seeAllData=true) because it opens up the existing database data for unexpected modification by tests.

Most ConnectApi methods require seeAllData=true in tests. Set the property reportConnectApi to false, and the rule does not report seeAllData=true on a test class or test method that calls such a method:

  • For seeAllData=true on a test class, the rule looks for the ConnectApi call in every method of the class, helper methods too.
  • For seeAllData=true on a test method, the rule looks for the ConnectApi call only in the body of that test method.
  • A ConnectApi method whose name starts with setTest, such as ConnectApi.ChatterFeeds.setTestGetFeedElementsFromFeed, registers test data and uses no org data. So a call to it does not stop the rule from reporting seeAllData=true.
  • The regular method that a setTest method registers test data for, such as ConnectApi.ChatterFeeds.getFeedElementsFromFeed, returns that test data and uses no org data. So when the same test class or test method also calls its setTest method, a call to the regular method does not stop the rule from flagging seeAllData=true either.
  • The rule does not look in other classes. If the test calls a method of another class, and only that class calls ConnectApi, the rule flags seeAllData=true.

See more here: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/connectAPI_TestingApex.htm

This rule is defined by the following Java class: net.sourceforge.pmd.lang.apex.rule.bestpractices.ApexUnitTestShouldNotUseSeeAllDataTrueRule

Example(s):

@isTest(seeAllData = true)
public class Foo {
    public static testMethod void testSomething() {
        Account a = null;
        // This is better than having a NullPointerException
        // System.assertNotEquals(a, null, 'account not found');
        a.toString();
    }
}

This rule has the following properties:

Name Default Value Description
reportConnectApi true Report seeAllData=true even where the annotated test class or test method calls a ConnectApi method that uses org data. Most ConnectApi methods require seeAllData=true in tests. Set this to false, and the rule does not report seeAllData=true on a test class or test method that calls one.

Use this rule with the default properties by just referencing it:

<rule ref="category/apex/bestpractices.xml/ApexUnitTestShouldNotUseSeeAllDataTrue" />

Use this rule and customize it:

<rule ref="category/apex/bestpractices.xml/ApexUnitTestShouldNotUseSeeAllDataTrue">
    <properties>
        <property name="reportConnectApi" value="true" />
    </properties>
</rule>

AvoidFutureAnnotation

Since: PMD 7.19.0

Priority: Medium Low (4)

Usage of the @Future annotation should be limited. The @Future annotation is a legacy way to execute asynchronous Apex code, but it has several limitations:

  • Methods must be static
  • Only primitive data types, primitive arrays, or collections of primitive data types can be parameters
  • No way to monitor job status or handle failures
  • No support for chaining jobs

Consider implementing the Queueable interface instead, which provides:

  • Better error handling and monitoring capabilities
  • Support for more complex data types
  • Ability to chain jobs

See more here: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_queueing_jobs.htm

This rule is defined by the following XPath expression:

//Method/ModifierNode/Annotation[lower-case(@Name) = 'future']

Example(s):

// Not recommended: Using @Future annotation
public class FutureExample {
    @Future
    public static void futureMethod(String accountId) {
        // Process account asynchronously
        Account acc = [SELECT Id, Name FROM Account WHERE Id = :accountId];
        // ... process account
    }
}

// Recommended: Using Queueable interface with Finalizer to detect and handle job failure
public class QueueableExample implements Queueable, Finalizer {
    private String accountId;

    public QueueableExample(String accountId) {
        this.accountId = accountId;
    }

    public void execute(QueueableContext context) {
        System.attachFinalizer(this);
        Account acc = [SELECT Id, Name FROM Account WHERE Id = :accountId];
        // ... process account
    }

    public void execute(FinalizerContext ctx) {
        if (ctx.getResult() == ParentJobResult.SUCCESS) {
            // Handle success
        } else {
            // Handle failure
        }
    }
}

// Usage:
// System.enqueueJob(new QueueableExample(accountId));

Use this rule by referencing it:

<rule ref="category/apex/bestpractices.xml/AvoidFutureAnnotation" />

AvoidGlobalModifier

Since: PMD 5.5.0

Priority: Medium (3)

Global classes should be avoided (especially in managed packages) as they can never be deleted or changed in signature. Always check twice if something needs to be global. Many interfaces (e.g. Batch) required global modifiers in the past but don’t require this anymore. Don’t lock yourself in.

This rule is defined by the following Java class: net.sourceforge.pmd.lang.apex.rule.bestpractices.AvoidGlobalModifierRule

Example(s):

global class Unchangeable {
    global UndeletableType unchangeable(UndeletableType param) {
        // ...
    }
}

Use this rule by referencing it:

<rule ref="category/apex/bestpractices.xml/AvoidGlobalModifier" />

AvoidLogicInTrigger

Since: PMD 5.5.0

Priority: Medium (3)

As triggers do not allow methods like regular classes they are less flexible and suited to apply good encapsulation style. Therefore delegate the triggers work to a regular class (often called Trigger handler class).

See more here: https://developer.salesforce.com/page/Trigger_Frameworks_and_Apex_Trigger_Best_Practices

This rule is defined by the following Java class: net.sourceforge.pmd.lang.apex.rule.bestpractices.AvoidLogicInTriggerRule

Example(s):

trigger Accounts on Account (before insert, before update, before delete, after insert, after update, after delete, after undelete) {
    for(Account acc : Trigger.new) {
        if(Trigger.isInsert) {
            // ...
        }

        // ...

        if(Trigger.isDelete) {
            // ...
        }
    }
}

Use this rule by referencing it:

<rule ref="category/apex/bestpractices.xml/AvoidLogicInTrigger" />

DebugsShouldUseLoggingLevel

Since: PMD 6.18.0

Priority: Medium (3)

The first parameter of System.debug, when using the signature with two parameters, is a LoggingLevel enum.

Having the Logging Level specified provides a cleaner log, and improves readability of it.

This rule is defined by the following XPath expression:

//MethodCallExpression[lower-case(@FullMethodName)='system.debug'][count(*)=2
    or ($strictMode=true() and count(*)=3 and lower-case(VariableExpression/@Image)='debug')]

Example(s):

@isTest
public class Foo {
    @isTest
    static void bar() {
        System.debug('Hey this code executed.'); // not good
        System.debug(LoggingLevel.WARN, 'Hey, something might be wrong.'); // good
        System.debug(LoggingLevel.DEBUG, 'Hey, something happened.'); // not good when on strict mode
    }
}

This rule has the following properties:

Name Default Value Description
strictMode false If true, mark statements that use the DEBUG enum of LoggingLevel.

Use this rule with the default properties by just referencing it:

<rule ref="category/apex/bestpractices.xml/DebugsShouldUseLoggingLevel" />

Use this rule and customize it:

<rule ref="category/apex/bestpractices.xml/DebugsShouldUseLoggingLevel">
    <properties>
        <property name="strictMode" value="false" />
    </properties>
</rule>

QueueableWithoutFinalizer

Since: PMD 7.8.0

Priority: Low (5)

Detects when the Queueable interface is used but a Finalizer is not attached. It is best practice to call the System.attachFinalizer(Finalizer f) method within the execute method of a class which implements the Queueable interface. Without attaching a Finalizer, there is no way of designing error recovery actions should the Queueable action fail.

This rule is defined by the following Java class: net.sourceforge.pmd.lang.apex.rule.bestpractices.QueueableWithoutFinalizerRule

Example(s):

// Incorrect code, does not attach a finalizer.
public class UserUpdater implements Queueable {
    public List<User> usersToUpdate;

    public UserUpdater(List<User> usersToUpdate) {
        this.usersToUpdate = usersToUpdate;
    }

    public void execute(QueueableContext context) { // no Finalizer is attached
        update usersToUpdate;
    }
}

// Proper code, attaches a finalizer.
public class UserUpdater implements Queueable, Finalizer {
    public List<User> usersToUpdate;

    public UserUpdater(List<User> usersToUpdate) {
        this.usersToUpdate = usersToUpdate;
    }

    public void execute(QueueableContext context) {
        System.attachFinalizer(this);
        update usersToUpdate;
    }

    public void execute(FinalizerContext ctx) {
        if (ctx.getResult() == ParentJobResult.SUCCESS) {
            // Handle success
        } else {
            // Handle failure
        }
    }
}

Use this rule by referencing it:

<rule ref="category/apex/bestpractices.xml/QueueableWithoutFinalizer" />

UnusedLocalVariable

Since: PMD 6.23.0

Priority: Low (5)

Detects when a local variable is declared and/or assigned but not used.

This rule is defined by the following Java class: net.sourceforge.pmd.lang.apex.rule.bestpractices.UnusedLocalVariableRule

Example(s):

public Boolean bar(String z) {
        String x = 'some string'; // not used

        String y = 'some other string'; // used in the next line
        return z.equals(y);
    }

Use this rule by referencing it:

<rule ref="category/apex/bestpractices.xml/UnusedLocalVariable" />
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.