Learn more about checking and analyzing the complexity of code with C# and Visual Studio.
Each flow graph consists of nodes and edges. The nodes represent computational statements or expressions, and the edges represent the transfer of control between nodes besides the keywords from Figure 2.
The use of loop constructs can increase complexity (for, for each, while), switch block (number of case statements), jumps statements (continue, goto), exceptions (catch), and conditional enablers (and, or, ternary operator).
Implementing any of those constructs introduces a new path through the code.
If we analyze the complexity metrics and calculation approaches through the programming languages we can detect a discrepancy.
Unfortunately, the calculation algorithm for cyclomatic complexity is not standardized in the programming world. The official Microsoft documentation has a slightly different formula:
cyclomatic complexity = the number of edges - the number of nodes + 1.
Figure 3. Exception catch complexity.
Code from Figure 3 will generate the complexity of one, which can be seen in the image above. But according to the formula from Figure 2 should be a complexity of 2.
Based on NDepend (a static analysis tool for .NET managed code) source, it says that calculation represents the number of decisions that can be taken in the procedure.
For C# specific, the complexity of a method is 1+ for the following expressions:
And the following expressions are not calculated in the result of the cyclomatic complexity algorithm:
Checking and analyzing the complexity of the code with the help of Visual Studio (the path can vary depending on the version of Visual Studio) is a reasonably straightforward task.
Visual Studio comes with a built-in calculator for code metrics that returns simple code metric data such as maintainability index, cyclomatic complexity, etc.
In Visual Studio, for any given method, the value of 25 is considered dangerously high and causes Visual Studio to create an error.
To activate the code metrics results window in Visual Studio, we should navigate to the analysis and select Calculate Code Metrics and cyclomatic complexity.
See an example on the next page.
Figure 4. Complexity check via navigation toolbar.
After the computation is complete, the window for Code Metrics Results will appear with metrics (dependent on the version of Visual Studio) for:
With the first example, we notice that cyclomatic complexity is at 1, the lowest possible value.
Figure 5. Cyclomatic complexity of 1.
Figure 6. Cyclomatic complexity of 2.
The example from Figure 6 has an additional decision code, which increased complexity by +1.
Figure 7. Cyclomatic complexity of 6.
In the code example from Figure 7, we’ve increased the complexity to the value of 6 by adding one more if statement followed by the switch case with three decisions. At this point, we could think about lowering the complexity.
To lower the complexity of the code, we could extract statements into the separated methods.
Figure 8. Extracted methods.
As shown in Figure 7, the extracted methods lowered the main program by five and now have an initial value of 1. But now we’ve got the two separated methods LowComplexitySwitch and LowComplexityIf, with the complexity values of 4 and 3.
With that approach, we cleaned the code a bit and improved the readability and maintainability.
However, we should notice one more metric tightly correlated to the cyclomatic complexity: the maintainability index. If we compare the maintainability index from Figure 7, which is 67, with the Figure 8 value of 83, we can see the increase in the value.
In this case, of the maintainability index, the higher number makes for higher code maintainability.