AvoidDeeplyNestedIfStmts
Since: PMD 5.5.0
Priority: Medium (3)
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(s):
public class Foo {
public void bar(Integer x, Integer y, Integer z) {
if (x>y) {
if (y>z) {
if (z==x) {
// !! too deep
}
}
}
}
}
This rule has the following properties:
Name | Default Value | Description | Multivalued |
---|---|---|---|
cc_categories | Style | Code Climate Categories | yes. Delimiter is ‘|’. |
cc_remediation_points_multiplier | 1 | Code Climate Remediation Points multiplier | no |
cc_block_highlighting | false | Code Climate Block Highlighting | no |
problemDepth | 3 | The if statement depth reporting threshold | no |
Use this rule by referencing it:
<rule ref="category/apex/design.xml/AvoidDeeplyNestedIfStmts" />
CyclomaticComplexity
Since: PMD 6.0.0
Priority: Medium (3)
The complexity of methods directly affects maintenance costs and readability. Concentrating too much decisional logic in a single method makes its behaviour 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. Additionnally, 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(s):
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;
}
}
}
}
This rule has the following properties:
Name | Default Value | Description | Multivalued |
---|---|---|---|
methodReportLevel | 10 | Cyclomatic complexity reporting threshold | no |
cc_categories | Style | Code Climate Categories | yes. Delimiter is ‘|’. |
cc_remediation_points_multiplier | 1 | Code Climate Remediation Points multiplier | no |
cc_block_highlighting | false | Code Climate Block Highlighting | no |
classReportLevel | 40 | Total class complexity reporting threshold | no |
Use this rule by referencing it:
<rule ref="category/apex/design.xml/CyclomaticComplexity" />
ExcessiveClassLength
Since: PMD 5.5.0
Priority: Medium (3)
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 managable and ripe for reuse.
This rule is defined by the following Java class: net.sourceforge.pmd.lang.apex.rule.design.ExcessiveClassLengthRule
Example(s):
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
}
}
This rule has the following properties:
Name | Default Value | Description | Multivalued |
---|---|---|---|
topscore | Top score value | no | |
minimum | Minimum reporting threshold | no | |
cc_categories | Style | Code Climate Categories | yes. Delimiter is ‘|’. |
cc_remediation_points_multiplier | 1 | Code Climate Remediation Points multiplier | no |
cc_block_highlighting | false | Code Climate Block Highlighting | no |
sigma | Sigma value | no |
Use this rule by referencing it:
<rule ref="category/apex/design.xml/ExcessiveClassLength" />
ExcessiveParameterList
Since: PMD 5.5.0
Priority: Medium (3)
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(s):
// 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) {
// ...
}
This rule has the following properties:
Name | Default Value | Description | Multivalued |
---|---|---|---|
topscore | Top score value | no | |
minimum | Minimum reporting threshold | no | |
cc_categories | Style | Code Climate Categories | yes. Delimiter is ‘|’. |
cc_remediation_points_multiplier | 1 | Code Climate Remediation Points multiplier | no |
cc_block_highlighting | false | Code Climate Block Highlighting | no |
sigma | Sigma value | no |
Use this rule by referencing it:
<rule ref="category/apex/design.xml/ExcessiveParameterList" />
ExcessivePublicCount
Since: PMD 5.5.0
Priority: Medium (3)
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(s):
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 ...]
}
This rule has the following properties:
Name | Default Value | Description | Multivalued |
---|---|---|---|
topscore | Top score value | no | |
minimum | Minimum reporting threshold | no | |
cc_categories | Style | Code Climate Categories | yes. Delimiter is ‘|’. |
cc_remediation_points_multiplier | 1 | Code Climate Remediation Points multiplier | no |
cc_block_highlighting | false | Code Climate Block Highlighting | no |
sigma | Sigma value | no |
Use this rule by referencing it:
<rule ref="category/apex/design.xml/ExcessivePublicCount" />
NcssConstructorCount
Since: PMD 5.5.0
Priority: Medium (3)
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(s):
public class Foo extends Bar {
//this constructor only has 1 NCSS lines
public Foo() {
super();
super.foo();
}
}
This rule has the following properties:
Name | Default Value | Description | Multivalued |
---|---|---|---|
topscore | Top score value | no | |
minimum | Minimum reporting threshold | no | |
cc_categories | Style | Code Climate Categories | yes. Delimiter is ‘|’. |
cc_remediation_points_multiplier | 1 | Code Climate Remediation Points multiplier | no |
cc_block_highlighting | false | Code Climate Block Highlighting | no |
sigma | Sigma value | no |
Use this rule by referencing it:
<rule ref="category/apex/design.xml/NcssConstructorCount" />
NcssMethodCount
Since: PMD 5.5.0
Priority: Medium (3)
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(s):
public class Foo extends Bar {
//this method only has 1 NCSS lines
public Integer methd() {
super.methd();
return 1;
}
}
This rule has the following properties:
Name | Default Value | Description | Multivalued |
---|---|---|---|
topscore | Top score value | no | |
minimum | Minimum reporting threshold | no | |
cc_categories | Style | Code Climate Categories | yes. Delimiter is ‘|’. |
cc_remediation_points_multiplier | 1 | Code Climate Remediation Points multiplier | no |
cc_block_highlighting | false | Code Climate Block Highlighting | no |
sigma | Sigma value | no |
Use this rule by referencing it:
<rule ref="category/apex/design.xml/NcssMethodCount" />
NcssTypeCount
Since: PMD 5.5.0
Priority: Medium (3)
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(s):
//this class only has 6 NCSS lines
public class Foo extends Bar {
public Foo() {
super();
super.foo();
}
}
This rule has the following properties:
Name | Default Value | Description | Multivalued |
---|---|---|---|
topscore | Top score value | no | |
minimum | Minimum reporting threshold | no | |
cc_categories | Style | Code Climate Categories | yes. Delimiter is ‘|’. |
cc_remediation_points_multiplier | 1 | Code Climate Remediation Points multiplier | no |
cc_block_highlighting | false | Code Climate Block Highlighting | no |
sigma | Sigma value | no |
Use this rule by referencing it:
<rule ref="category/apex/design.xml/NcssTypeCount" />
StdCyclomaticComplexity
Since: PMD 5.5.0
Priority: Medium (3)
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(s):
// 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;
}
}
}
This rule has the following properties:
Name | Default Value | Description | Multivalued |
---|---|---|---|
showMethodsComplexity | true | Add method average violations to the report | no |
showClassesComplexity | true | Add class average violations to the report | no |
cc_categories | Style | Code Climate Categories | yes. Delimiter is ‘|’. |
cc_remediation_points_multiplier | 1 | Code Climate Remediation Points multiplier | no |
cc_block_highlighting | false | Code Climate Block Highlighting | no |
reportLevel | 10 | Cyclomatic Complexity reporting threshold | no |
Use this rule by referencing it:
<rule ref="category/apex/design.xml/StdCyclomaticComplexity" />
TooManyFields
Since: PMD 5.5.0
Priority: Medium (3)
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(s):
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;
}
This rule has the following properties:
Name | Default Value | Description | Multivalued |
---|---|---|---|
cc_categories | Style | Code Climate Categories | yes. Delimiter is ‘|’. |
cc_remediation_points_multiplier | 1 | Code Climate Remediation Points multiplier | no |
cc_block_highlighting | false | Code Climate Block Highlighting | no |
maxfields | 15 | Max allowable fields | no |
Use this rule by referencing it:
<rule ref="category/apex/design.xml/TooManyFields" />