Rules that flag suboptimal code.
Edit me

AvoidDmlStatementsInLoops

Since: PMD 5.5.0

Priority: Medium (3)

Avoid DML statements inside loops to avoid hitting the DML governor limit. Instead, try to batch up the data into a list and invoke your DML once on that list of data outside the loop.

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

Example(s):

public class Something {
    public void foo() {
        for (Integer i = 0; i < 151; i++) {
            Account account;
            // ...
            insert account;
        }
    }
}

This rule has the following properties:

Name Default Value Description Multivalued
cc_categories Performance Deprecated Code Climate Categories yes. Delimiter is ‘|’.
cc_remediation_points_multiplier 150 Deprecated Code Climate Remediation Points multiplier no
cc_block_highlighting false Deprecated Code Climate Block Highlighting no

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

<rule ref="category/apex/performance.xml/AvoidDmlStatementsInLoops" />

AvoidSoqlInLoops

Since: PMD 5.5.0

Priority: Medium (3)

New objects created within loops should be checked to see if they can created outside them and reused.

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

Example(s):

public class Something {
    public static void main( String as[] ) {
        for (Integer i = 0; i < 10; i++) {
            List<Account> accounts = [SELECT Id FROM Account];
        }
    }
}

This rule has the following properties:

Name Default Value Description Multivalued
cc_categories Performance Deprecated Code Climate Categories yes. Delimiter is ‘|’.
cc_remediation_points_multiplier 150 Deprecated Code Climate Remediation Points multiplier no
cc_block_highlighting false Deprecated Code Climate Block Highlighting no

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

<rule ref="category/apex/performance.xml/AvoidSoqlInLoops" />

AvoidSoslInLoops

Since: PMD 6.0.0

Priority: Medium (3)

Sosl calls within loops can cause governor limit exceptions.

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

Example(s):

public class Something {
    public static void main( String as[] ) {
        for (Integer i = 0; i < 10; i++) {
            List<List<SObject>> searchList = [FIND 'map*' IN ALL FIELDS RETURNING Account (Id, Name), Contact, Opportunity, Lead];
        }
    }
}

This rule has the following properties:

Name Default Value Description Multivalued
cc_categories Performance Deprecated Code Climate Categories yes. Delimiter is ‘|’.
cc_remediation_points_multiplier 150 Deprecated Code Climate Remediation Points multiplier no
cc_block_highlighting false Deprecated Code Climate Block Highlighting no

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

<rule ref="category/apex/performance.xml/AvoidSoslInLoops" />