Apex Java best practices

This article is based on the PMD documentation article. See the original article on the PMD site: PMD: Java best practices.

Below are the generally accepted best practices. 

AvoidReassigningLoopVariables

Reassigning loop variables can lead to hard-to-find bugs. Prevent or limit how these variables can be changed.

In foreach-loops, configured by the foreachReassign property:

  • deny: Report any reassignment of the loop variable in the loop body. This is the default.
  • allow: Don’t check the loop variable.
  • firstOnly: Report any reassignments of the loop variable, except as the first statement in the loop body. This is useful if some kind of normalization or clean-up of the value before using is permitted, but any other change of the variable is not.

In for-loops, configured by the forReassign property:

  • deny: Report any reassignment of the control variable in the loop body. This is the default.
  • allow: Don’t check the control variable.
  • skip: Report any reassignments of the control variable, except conditional increments/decrements (++, --, +=, -=). This prevents accidental reassignments or unconditional increments of the control variable.

This rule is defined by the following Java class: 

net.sourceforge.pmd.lang.java.rule.bestpractices.AvoidReassigningLoopVariablesRule

Example

public class Foo {
  private void foo() {
    for (String s : listOfStrings()) {
      s = s.trim(); // OK, when foreachReassign is "firstOnly" or "allow"
      doSomethingWith(s);

      s = s.toUpper(); // OK, when foreachReassign is "allow"
      doSomethingElseWith(s);
    }

    for (int i=0; i < 10; i++) {
      if (check(i)) {
        i++; // OK, when forReassign is "skip" or "allow"
      }

      i = 5;  // OK, when forReassign is "allow"

      doSomethingWith(i);
    }
  }
}


Properties

NameDefault ValueDescriptionMultivalued
foreachReassigndenyhow/if foreach control variables may be reassignedno
forReassigndenyhow/if for control variables may be reassignedno

OneDeclarationPerLine

ava allows the use of several variables declaration of the same type on one line. However, it can lead to quite messy code. This rule looks for several declarations on the same line.

This rule is defined by the following XPath expression:

//LocalVariableDeclaration
   [not(parent::ForInit)]
   [count(VariableDeclarator) > 1]
   [$strictMode or count(distinct-values(VariableDeclarator/@BeginLine)) != count(VariableDeclarator)]
|
//FieldDeclaration
   [count(VariableDeclarator) > 1]
   [$strictMode or count(distinct-values(VariableDeclarator/@BeginLine)) != count(VariableDeclarator)]

Example

String name;            // separate declarations
String lastname;

String name, lastname;  // combined declaration, a violation

String name,
       lastname;        // combined declaration on multiple lines, no violation by default.
                        // Set property strictMode to true to mark this as violation.

Properties


NameDefault ValueDescriptionMultivalued
strictModefalseIf true, mark combined declaration even if the declarations are on separate lines.no

What's here


Related content

 PMD: Java best practices




Last modified on Jul 14, 2020