TICS Coding Standard Viewer 
TIOBE Software Quality Framework
Print-friendly version
©TIOBE Software www.tiobe.com
TIOBE - C Coding Standard

SR:  13#3Checked automatically with code checker

Synopsis:Functions shall be restricted to have a maximum cyclomatic complexity.
Severity Level:5


The cyclomatic complexity is a metric to measure code complexity. It directly measures the number of linearly independent paths through a program's source code. A function with a cyclomatic complexity less than 5 is considered to be ok. However, if its cyclomatic complexity is higher than 10, refactoring is recommended.

A lower cyclomatic complexity increases readability, but it also lowers the number of unit tests that needs to be written. Improving the cyclomatic complexity most often is achieved by introducing extra helper functions. There are also cases for which rearranging the code suffices. Consider the following piece of code.

if (status > 0) {
  found = true;
} else {
  found = false;

This "if" statement has a cyclomatic complexity of 2. In other words there are 2 test cases needed to have 100% branch coverage. Observe that this is semantically equal to

found = (status > 0);

which only needs 1 test case for 100% branch coverage.