Sonar gives me the following cyclomatic complexity number : 22.
For the following program :
private static SomeDto checkSomething(AnotherDto anotherDto, String reference)
{
SomeDto someDto = new SomeDto();
// condition 1
if (!someDto.getA())
return new SomeDto("bla1", "blabla");
// condition 2
if (someDto.getName2() == null || checkSurName(anotherDto.getName()))
return new SomeDto("bla2", "blabla");
// condition 3
if (someDto.getName3() == null || checkSurName(anotherDto.getName()))
return new SomeDto("bla3", "blabla");
// condition 4
if (someDto.getName4() == null && checkSurName(anotherDto.getName()))
return new SomeDto("bla4", "blabla");
// condition 5
if (someDto.getName5() == null || checkSurName(anotherDto.getName()))
return new SomeDto("bla5", "blabla");
// condition 6
if (someDto.getName6() == null && checkSurName(anotherDto.getName()))
return new SomeDto("bla6", "blabla");
// condition 7
if (someDto.getName7() == null && checkSurName(anotherDto.getName()))
return new SomeDto("bla7", "blabla");
// condition 8
if (someDto.getName8() == null && checkSurName(anotherDto.getName()))
return new SomeDto("bla8", "blabla");
// condition 9
if (someDto.getName9() == null && checkSurName(anotherDto.getName()))
return new SomeDto("bla9", "blabla");
// condition 10
if (someDto.getName10() == null && checkSurName(anotherDto.getName()))
return new SomeDto("bla10", "blabla");
// condition 11
if (someDto.getName11() == null && checkSurName(anotherDto.getName()))
return new SomeDto("bla11", "blabla");
return someDto;
}
The issue i get is the following :
"The Cyclomatic Complexity of this method "checkSomething" is 22 which is greater than 12 authorized."
My question is : considering the McCabe formula V(G) = E - N + 2, how does Sonar reach the number of 22 ?
Where :
E = number of edges
N = number of nodes
How many edges and nodes are there in this method ? What is the control flow for this method ?
We're on SonarQube Version 6.3 (build 19869).
Well, after further investigation and according to this link (checkstyle tool), I have concluded that the McCabe formula is not really applied to calculate the cyclomatic complexity in a Java program.
So if I apply this rule to the previous sample code :
Correct me if i am wrong. Shouldn't the return statements (except the one that is the last statement of the method) be taken into account at least ?
Anyways, the result number of 22 makes sense. This method has an awful number of successive "if" conditions and something should be done about it to improve its maintainability.