Apex design best practices

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

Best practices that help you discover design issues.

AvoidDeeplyNestedIfStmts

Avoid creating deeply nested if-then statements since they are harder to read and error-prone to maintain.

This rule is defined by the following Java class: 

net.sourceforge.pmd.lang.apex.rule.design.AvoidDeeplyNestedIfStmtsRule

Example

public class Foo {
    public void bar(Integer x, Integer y, Integer z) {
        if (x>y) {
            if (y>z) {
                if (z==x) {
                    // !! too deep
                }
            }
        }
    }
}

Properties

NameDefault ValueDescription
cc_categories[Style]Code Climate Categories
cc_remediation_points_multiplier1Code Climate Remediation Points multiplier
cc_block_highlightingfalseCode Climate Block Highlighting
problemDepth3The if statement depth reporting threshold

CyclomaticComplexity

The complexity of methods directly affects maintenance costs and readability. Concentrating too much decisional logic in a single method makes its behavior hard to read and change.

Cyclomatic complexity assesses the complexity of a method by counting the number of decision points in a method, plus one for the method entry. Decision points are places where the control flow jumps to another place in the program. As such, they include all control flow statements, such as ‘if’, ‘while’, ‘for’, and ‘case’.

Generally, numbers ranging from 1-4 denote low complexity, 5-7 denote moderate complexity, 8-10 denote high complexity, and 11+ is very high complexity. By default, this rule reports methods with a complexity >= 10. Additionally, classes with many methods of moderate complexity get reported as well once the total of their methods’ complexities reaches 40, even if none of the methods was directly reported.

Reported methods should be broken down into several smaller methods. Reported classes should probably be broken down into subcomponents.

This rule is defined by the following Java class: 

net.sourceforge.pmd.lang.apex.rule.design.CyclomaticComplexityRule

Example

public class Complicated {
  public void example() { // This method has a cyclomatic complexity of 12
    int x = 0, y = 1, z = 2, t = 2;
    boolean a = false, b = true, c = false, d = true;
    if (a && b || b && d) {
      if (y == z) {
        x = 2;
      } else if (y == t && !d) {
        x = 2;
      } else {
        x = 2;
      }
    } else if (c && d) {
      while (z < y) {
        x = 2;
      }
    } else {
      for (int n = 0; n < t; n++) {
        x = 2;
      }
    }
  }
}

Properties

NameDefault ValueDescription
methodReportLevel10Cyclomatic complexity reporting threshold
cc_categories[Style]Code Climate Categories
cc_remediation_points_multiplier1Code Climate Remediation Points multiplier
cc_block_highlightingfalseCode Climate Block Highlighting
classReportLevel40Total class complexity reporting threshold

ExcessiveClassLength

Excessive class file lengths are usually indications that the class may be burdened with excessive responsibilities that could be provided by external classes or functions. In breaking these methods apart the code becomes more maneagable and ripe for reuse.

This rule is defined by the following Java class: 

net.sourceforge.pmd.lang.apex.rule.design.ExcessiveClassLengthRule

Example

public class Foo {
    public void bar1() {
        // 1000 lines of code
    }
    public void bar2() {
        // 1000 lines of code
    }
    public void bar3() {
        // 1000 lines of code
    }
    public void barN() {
        // 1000 lines of code
    }
}

Properties

NameDefault ValueDescription
topscore
Top score value
minimum
Minimum reporting threshold
cc_categories[Style]Code Climate Categories
cc_remediation_points_multiplier1Code Climate Remediation Points multiplier
cc_block_highlightingfalseCode Climate Block Highlighting
sigma
Sigma value

ExcessiveParameterList

Methods with numerous parameters are a challenge to maintain, especially if most of them share the same datatype. These situations usually denote the need for new objects to wrap the numerous parameters.

This rule is defined by the following Java class: 

net.sourceforge.pmd.lang.apex.rule.design.ExcessiveParameterListRule

Example

// too many arguments liable to be mixed up
public void addPerson(int birthYear, int birthMonth, int birthDate, int height, int weight, int ssn) {
    // ...
}
// preferred approach 
public void addPerson(Date birthdate, BodyMeasurements measurements, int ssn) {
    // ...
}

Properties

NameDefault ValueDescription
topscore
Top score value
minimum
Minimum reporting threshold
cc_categories[Style]Code Climate Categories
cc_remediation_points_multiplier1Code Climate Remediation Points multiplier
cc_block_highlightingfalseCode Climate Block Highlighting
sigma
Sigma value

ExcessivePublicCount

Classes with large numbers of public methods and attributes require disproportionate testing efforts since combinational side effects grow rapidly and increase risk. Refactoring these classes into smaller ones not only increases testability and reliability but also allows new variations to be developed easily.

This rule is defined by the following Java class: 

net.sourceforge.pmd.lang.apex.rule.design.ExcessivePublicCountRule

Example

public class Foo {
    public String value;
    public Bar something;
    public Variable var;
    // [... more more public attributes ...]

    public void doWork() {}
    public void doMoreWork() {}
    public void doWorkAgain() {}
    // [... more more public methods ...]
}

Properties

NameDefault ValueDescription
topscore
Top score value
minimum
Minimum reporting threshold
cc_categories[Style]Code Climate Categories
cc_remediation_points_multiplier1Code Climate Remediation Points multiplier
cc_block_highlightingfalseCode Climate Block Highlighting
sigma
Sigma value

NcssConstructorCount

This rule uses the NCSS (Non-Commenting Source Statements) algorithm to determine the number of lines of code for a given constructor. NCSS ignores comments, and counts actual statements. Using this algorithm, lines of code that are split are counted as one.

This rule is defined by the following Java class: 

net.sourceforge.pmd.lang.apex.rule.design.NcssConstructorCountRule

Example

public class Foo extends Bar {
    //this constructor only has 1 NCSS lines
    public Foo() {
        super();




        super.foo();
}
}

Properties

NameDefault ValueDescription
topscore
Top score value
minimum
Minimum reporting threshold
cc_categories[Style]Code Climate Categories
cc_remediation_points_multiplier1Code Climate Remediation Points multiplier
cc_block_highlightingfalseCode Climate Block Highlighting
sigma
Sigma value

NcssMethodCount

This rule uses the NCSS (Non-Commenting Source Statements) algorithm to determine the number of lines of code for a given method. NCSS ignores comments, and counts actual statements. Using this algorithm, lines of code that are split are counted as one.

This rule is defined by the following Java class: 

net.sourceforge.pmd.lang.apex.rule.design.NcssMethodCountRule

Example

public class Foo extends Bar {
    //this method only has 1 NCSS lines
    public Integer methd() {
        super.methd();



        return 1;
    }
}

Properties

NameDefault ValueDescription
topscore
Top score value
minimum
Minimum reporting threshold
cc_categories[Style]Code Climate Categories
cc_remediation_points_multiplier1Code Climate Remediation Points multiplier
cc_block_highlightingfalseCode Climate Block Highlighting
sigma
Sigma value

NcssTypeCount

This rule uses the NCSS (Non-Commenting Source Statements) algorithm to determine the number of lines of code for a given type. NCSS ignores comments, and counts actual statements. Using this algorithm, lines of code that are split are counted as one.

This rule is defined by the following Java class:

 net.sourceforge.pmd.lang.apex.rule.design.NcssTypeCountRule

Example

//this class only has 6 NCSS lines
public class Foo extends Bar {
    public Foo() {
        super();





        super.foo();
    }
}

Properties

NameDefault ValueDescription
topscore
Top score value
minimum
Minimum reporting threshold
cc_categories[Style]Code Climate Categories
cc_remediation_points_multiplier1Code Climate Remediation Points multiplier
cc_block_highlightingfalseCode Climate Block Highlighting
sigma
Sigma value

StdCyclomaticComplexity

Complexity directly affects maintenance costs is determined by the number of decision points in a method plus one for the method entry. The decision points include ‘if’, ‘while’, ‘for’, and ‘case labels’ calls.
Generally, numbers ranging from 1-4 denote low complexity, 5-7 denote moderate complexity, 8-10 denote high complexity, and 11+ is very high complexity.

This rule is defined by the following Java class: 

net.sourceforge.pmd.lang.apex.rule.design.StdCyclomaticComplexityRule

Example

// This has a Cyclomatic Complexity = 12
public class Foo {
1   public void example() {
2   if (a == b || (c == d && e == f)) {
3       if (a1 == b1) {
            fiddle();
4       } else if a2 == b2) {
            fiddle();
        }  else {
            fiddle();
        }
5   } else if (c == d) {
6       while (c == d) {
            fiddle();
        }
7   } else if (e == f) {
8       for (int n = 0; n < h; n++) {
            fiddle();
        }
    } else {
        switch (z) {
9           case 1:
                fiddle();
                break;
10          case 2:
                fiddle();
                break;
11          case 3:
                fiddle();
                break;
12          default:
                fiddle();
                break;
        }
    }
}

Properties

NameDefault ValueDescription
showMethodsComplexitytrueAdd method average violations to the report
showClassesComplexitytrueAdd class average violations to the report
cc_categories[Style]Code Climate Categories
cc_remediation_points_multiplier1Code Climate Remediation Points multiplier
cc_block_highlightingfalseCode Climate Block Highlighting
reportLevel10Cyclomatic Complexity reporting threshold

TooManyFields

Classes that have too many fields can become unwieldy and could be redesigned to have fewer fields, possibly through grouping related fields in new objects. For example, a class with individual city/state/zip fields could park them within a single Address field.

This rule is defined by the following Java class: 

net.sourceforge.pmd.lang.apex.rule.design.TooManyFieldsRule

Example

public class Person {
    // too many separate fields
    int birthYear;
    int birthMonth;
    int birthDate;
    float height;
    float weight;
}

public class Person {
    // this is more manageable
    Date birthDate;
    BodyMeasurements measurements;
}

Properties

NameDefault ValueDescription
cc_categories[Style]Code Climate Categories
cc_remediation_points_multiplier1Code Climate Remediation Points multiplier
cc_block_highlightingfalseCode Climate Block Highlighting
maxfields15Max allowable fields

What's here

Related content

 PMD: design best practices




Last modified on Jul 14, 2020