I use SonarQube for a Java project but the complexity calculation is not clear for me. The complexity value is 3 for the following example:
public boolean even(int i) {
if (i % 2 == 0) {
return true;
}
return false;
}
According to the User Guide the complexity is calculated as follows: “It is the cyclomatic complexity, also known as McCabe metric. Whenever the control flow of a function splits, the complexity counter gets incremented by one. Each function has a minimum complexity of 1.” In the detailed description it is mentioned that the return statement increases the complexity value by one (if it is not the last statement in the method). I do not understand why the return statement splits the control flow. In my opinion, there is only one possible way after every return statement in the control flow.
The control-flow graph of your example contains 5 edges (E), 5 nodes(N) and one connected components (P).
According to the defintion of the complexity (M=E-N+2P) the correct complexity value is 2.