How is Code Complexity Calculated?


I see that codecov calculates a metric called Complexity, but I can’t find any resources or documentation on what this metric means so that I can improve it. Can someone provide an explanation or documentation? Thank you!



Additional Information

Hey @jones-chris – sorry for the lengthy delay. Complexity coverage, sometimes also called Branch coverage, refers to Cyclomatic Complexity of the code.

Some languages and test frameworks, like Java / Jacoco, have Cycolmatic Complexity out of the box, in which case we ingest that data.

Cyclomatic Complexity

JaCoCo also calculates cyclomatic complexity for each non-abstract method and summarizes complexity for classes, packages and groups. According to its definition by McCabe1996 cyclomatic complexity is the minimum number of paths that can, in (linear) combination, generate all possible paths through a method. Thus the complexity value can serve as an indication for the number of unit test cases to fully cover a certain piece of software. Complexity figures can always be calculated, even in absence of debug information in the class files.

The formal definition of the cyclomatic complexity v(G) is based on the representation of a method’s control flow graph as a directed graph:

v(G) = E - N + 2

Where E is the number of edges and N the number of nodes. JaCoCo calculates cyclomatic complexity of a method with the following equivalent equation based on the number of branches (B) and the number of decision points (D):

v(G) = B - D + 1

Based on the coverage status of each branch JaCoCo also calculates covered and missed complexity for each method. Missed complexity again is an indication for the number of test cases missing to fully cover a module. Note that as JaCoCo does not consider exception handling as branches try/catch blocks will also not increase complexity.