Analyze Routine's Cyclomatic Complexity

Applies to AQTime 8.81, last modified on November 27, 2020

The cyclomatic complexity (or conditional complexity) is a metric that specifies the maximum number of paths through a routine’s code from its beginning to the end.

About Cyclomatic Complexity

Suppose you have a routine that does not contain conditional and loop operators. The cyclomatic complexity of this routine is 1 as there is only one linear path through the routine’s code. If the routine includes an if statement with one condition, the if body can be executed or skipped during the routine execution according to the condition and the routine’s complexity increases to 2.

All the conditional and loop statements -- if, if ... else, switch (case), for, while and others -- increase the complexity of routines. The more such statements your code contains, the more complex it is for analysis, modification and maintaining.

The cyclomatic complexity metric helps you estimate the complexity of your code and find routines that may require optimization. Also, it helps QA engineers to determine the number of test cases needed to cover a routine (see below).

You can find more information on cyclomatic complexity in the Web, for example, here is an article about it:

Viewing Cyclomatic Complexity in AQTime

AQTime can estimate the cyclomatic complexity of your code. Note that when AQTime measures the code’s complexity, it analyzes either binary or Intermediate Language (IL) code, but not the source code of the profiled module. This fact may affect the resulting values. For example, if you profile a module built with different compilers or compiler settings, AQTime may report different cyclomatic complexity values. The fact is that new compilers can use improved compiling algorithms and thus the cyclomatic complexity of the compiled module may decrease. Also, the compiler settings may command the compiler to optimize the code and the cyclomatic complexity may decrease after the optimization.

To obtain a cyclomatic complexity value for your code, run AQTime’s Static Analysis profiler and take a look at the Cyclomatic Complexity column of the Report panel.

Sort the results on the Cyclomatic Complexity column or activate the Cyclomatic complexity result view to quickly find routines with a high metric value.

Note: The Static Analysis profiler reports cyclomatic complexity only for native Windows code. This metric is not calculated for managed (.NET), Java and Silverlight code as well as for VBScript and JScript functions. Also, the profiler does not calculate cyclomatic complexity for functions that are imported from dynamic link libraries. For all these routines, the column will display Undefined.

Using Cyclomatic Complexity for Code Optimization

Cyclomatic complexity can help with application optimization or code reviewing and testing --

  • Before optimizing your application or before reviewing its code, it is considered to be a good practice to obtain the cyclomatic complexity metric for your routines. Large values of this metric indicate that a routine has a complex algorithmic structure and possibly requires code review and optimization.

    To decide how large the metric value is, you have to accept some complexity level. Typically, developers limit the cyclomatic complexity value by 10 or 15. So, all routines that have complexity values greater than 10 or 15 must be regarded as requiring optimization. The rest of the routines are considered to be “acceptable”. However, this is not a strict limitation and you can pick any other complexity limit that better suits your needs.

  • Another application of cyclomatic complexity is estimating the number of test cases for your routines to achieve the desired coverage level. For thorough testing, QA teams have to create more test cases or use more test data for routines with high complexity. A too small number of test cases will not cover all execution paths within a routine.

See Also

Exploring an Application's Source Code
Best Practices and How to Tutorials
Static Analysis Profiler - Overview

Highlight search results