【发布时间】:2018-02-22 06:36:59
【问题描述】:
Sonar 给了我以下圈复杂度数: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 Cabe 公式 v(g) = e - n + 2,Sonar 如何达到 22 的个数?
地点:
e = 边数
n = 节点数
这个方法有多少边和节点? 这个方法的控制流程是什么?
我们使用的是 SonarQube 版本 6.3(内部版本 19869)。
【问题讨论】:
-
请edit您的问题包括相关分析仪的名称和版本
-
你能发一个具体的
if (condition*)吗? -
是的。
if (anotherDto.getName() == null || !checkSurName(anotherDto.getName())) return new SomeDto("bla10", "blabla"); -
我编辑了方法的代码。条件 (ifs) 包含 ||或 && 操作数,第一个除外。
标签: java sonarqube graph-algorithm checkstyle cyclomatic-complexity