Index of the code metrics available out of the box to Apex rule developers.

# Index of code metrics

## Cognitive Complexity (COGNITIVE)

Operation metric. Can be calculated on any non-abstract operation.

Class metric. Can be computed on classes and enums - it’s just the sum of all method’s cogntive complexities.

### Description

See the corresponding Java metric Cognitive Complexity for a general description.

The rule `CognitiveComplexity` by default reports methods with a complexity of 15 or more and classes the have a total complexity (sum of all methods) of 50 or more. These reported methods should be broken down into less complex components.

## Cyclomatic Complexity (CYCLO)

Operation metric. Can be calculated on any non-abstract operation.

### Description

Number of independent paths through a block of code [Lanza05]. Formally, given that the control flow graph of the block has `n` vertices, `e` edges and `p` connected components, the cyclomatic complexity of the block is given by `CYCLO = e - n + 2p` [McCabe76]. In practice it can be calculated by counting control flow statements following the standard rules given below.

The standard version of the metric complies with McCabe’s original definition:

• Methods have a base complexity of 1.
• +1 for every control flow statement (`if`, `catch`, `throw`, `do`, `while`, `for`, `break`, `continue`) and conditional expression (`?:`).
• `else`, `finally` and `default` don’t count;
• +1 for every boolean operator (`&&`, `||`) in the guard condition of a control flow statement.

### Code examples

``````class Foo {
void baseCyclo() {                // Cyclo = 1
highCyclo();
}

void highCyclo() {                // Cyclo = 10
int x = 0, y = 2;
boolean a = false, b = true;

if (a && (y == 1 ? b : true)) { // +3
if (y == x) {                 // +1
while (true) {              // +1
if (x++ < 20) {           // +1
break;                  // +1
}
}
} else if (y == t && !d) {    // +2
x = a ? y : x;              // +1
} else {
x = 2;
}
}
}
}
``````

## Weighted Method Count (WMC)

Class metric. Can be computed on classes and enums.

Lanza05: Lanza, Marinescu; Object-Oriented Metrics in Practice, 2005.

McCabe76: McCabe, A Complexity Measure, in Proceedings of the 2nd ICSE (1976).

Tags: