Class ApexMetrics

    • Field Detail

      • CYCLO

        public static final Metric<ApexNode<?>,​Integer> CYCLO
        Number of independent paths through a block of code. 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. 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 do not count;
        • +1 for every boolean operator (&&, ||) in the guard condition of a control flow statement. That’s because Apex has short-circuit evaluation semantics for boolean operators, which makes every boolean operator kind of a control flow statement in itself.

        Code example:

        
         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;
               }
             }
           }
         }
         
      • COGNITIVE_COMPLEXITY

        public static final Metric<ApexNode<?>,​Integer> COGNITIVE_COMPLEXITY
        See the corresponding 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.

      • WEIGHED_METHOD_COUNT

        public static final Metric<ASTUserClassOrInterface<?>,​Integer> WEIGHED_METHOD_COUNT
        Sum of the statistical complexity of the operations in the class. We use CYCLO to quantify the complexity of an operation.