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 –