1

声纳给了我下面的圈复杂度数:22声纳如何计算圈复杂度?

对于下面的程序:

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; 
}  

问题我得到的是以下几点:

“这种方法的圈复杂度” checkSomething“是22,它大于12授权

我的问题是: 考虑Mac Ca (公式)v(g)= e - n + 2,Sonar如何达到22的数量?

其中:

E =边缘

N =节点

在这个方法中有多少边和节点的有多少? 这种方法的控制流程是什么?

我们在SonarQube版本6.3(建立19869)。

+1

请[编辑]你的问题,包括问题 –

+1

分析仪的名称和版本,你可以发布一个具体的'if(condition *)'? –

+0

是的。 'if(anotherDto.getName()== null ||!checkSurName(anotherDto.getName())) return new SomeDto(“bla10”,“blabla”);' –

回答

1

那么,经过进一步调查并根据此link(checkstyle工具),我得出结论认为McCabe公式并不真正用于计算Java程序中的圈复杂度。

的复杂性等于决策点的数量+ 1的决策点:如果,同时,这样做,因为,:,抓,开关,case语句,和运营商& &和||在目标的身体。

所以,如果我将此规则应用于前面的示例代码:

private static SomeDto checkSomething(AnotherDto anotherDto, String reference) // 1 
{ 
SomeDto someDto = new SomeDto(); 

// condition 1 
if (!someDto.getA())                // 2 
return new SomeDto("bla1", "blabla"); 

// condition 2 
if (someDto.getName2() == null || checkSurName(anotherDto.getName()))    // 4 
return new SomeDto("bla2", "blabla"); 

// condition 3 
if (someDto.getName3() == null || checkSurName(anotherDto.getName()))    // 6 
return new SomeDto("bla3", "blabla"); 

// condition 4 
if (someDto.getName4() == null && checkSurName(anotherDto.getName()))    // 8 
return new SomeDto("bla4", "blabla"); 

// condition 5 
if (someDto.getName5() == null || checkSurName(anotherDto.getName()))    // 10 
return new SomeDto("bla5", "blabla"); 

// condition 6 
if (someDto.getName6() == null && checkSurName(anotherDto.getName()))    // 12 
return new SomeDto("bla6", "blabla"); 

// condition 7 
if (someDto.getName7() == null && checkSurName(anotherDto.getName()))    // 14 
return new SomeDto("bla7", "blabla"); 

// condition 8 
if (someDto.getName8() == null && checkSurName(anotherDto.getName()))    // 16 
return new SomeDto("bla8", "blabla"); 

// condition 9 
if (someDto.getName9() == null && checkSurName(anotherDto.getName()))    // 18 
return new SomeDto("bla9", "blabla"); 

// condition 10 
if (someDto.getName10() == null && checkSurName(anotherDto.getName()))    // 20 
return new SomeDto("bla10", "blabla"); 

// condition 11 
if (someDto.getName11() == null && checkSurName(anotherDto.getName()))    // 22 
return new SomeDto("bla11", "blabla"); 

return someDto; 
}  

纠正我,如果我错了。 至少应该考虑返回语句(除了是方法的最后一个语句之外的语句)吗?

无论如何,结果数为22是有道理的。这种方法有很多连续的“if”条件,应该做些什么来提高其可维护性。

+0

Yse。 Sonar不要使用Cyclomatic Complexity,它有自己的方式。 https://docs.sonarqube.org/display/SONAR/Metrics+-+Complexity –