Cyclomatic complexity

Purpose

Cyclomatic complexity shows complexity calculated based on the number of paths through the code weighted by file, class or function/method amount. 

How metric helps

Cyclomatic Complexity is one of the most valuable code quality metrics helping one to make a judgement on how complex the code is. The more complex the code, the higher the level of code defects, making the code costlier to maintain. So, by reducing complexity, the number of bugs and defects can be reduced, along with its lifetime cost.

How metric works

Chart Overview

The chart represents the level of cyclomatic complexity (Axis Y) distributed over time (Axis Y). The level of complexity per each project child unit is clickable in the legend, which allows display/hide the series on the chart.

The chart can be viewed for the number of paths through the code weighted in different ways:

  • complexity per file

  • complexity per class 

  • complexity per function/method

Calculation

Complexity = E - N + 2*P,

where

E - number of edges in the flow graph.

N - number of nodes in the flow graph.

P - number of nodes that have exit points

RAG thresholds: Red - metric value > 200, Amber - metric value > 60, otherwise Green.

Data Source

Data for the metric can be collected from Sonar.

Related pages