Java 声纳圈复杂度规则问题 - 不鼓励多个返回语句
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23802945/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Sonar cyclomatic complexity rule issue - discourages multiple return statements
提问by gopalsaob
For the following piece of code, sonarqube computes the method cyclomatic complexity as 9
对于以下代码段,sonarqube 计算方法圈复杂度为 9
String foo() {
if (cond1) return a;
if (cond2) return b;
if (cond3) return c;
if (cond4) return d;
return e;
}
I understand as per the rules for computation http://docs.sonarqube.org/display/SONAR/Metrics+-+Complexitythe complexity of 9 is correct. So complexity of the method is = 4 (if) + 4 (return) + 1 (method) = 9
我理解根据计算规则http://docs.sonarqube.org/display/SONAR/Metrics+-+Complexity9 的复杂性是正确的。所以方法的复杂度是 = 4 (if) + 4 (return) + 1 (method) = 9
This complexity can be reduced, if I have a single exit point.
如果我只有一个出口点,则可以降低这种复杂性。
String foo() {
String temp;
if (cond1) {
temp = a;
} else if (cond2) {
temp = b;
} else if (cond3) {
temp = c;
} else if (cond4) {
temp = d;
} else {
temp = e;
}
return temp;
}
I believe this code is more cluttered and unreadable than the previous version and I feel having methods with return on guard conditions is a better programming practice. So is there a good reason why return statement is considered for computation of cyclomatic complexity? Can the logic for computation be changed so that it doesn't promote single exit point.
我相信这段代码比以前的版本更混乱和不可读,我觉得有返回保护条件的方法是更好的编程实践。那么是否有充分的理由考虑使用 return 语句来计算圈复杂度?是否可以更改计算逻辑,使其不提升单个出口点。
回答by Peter Lawrey
I agree you should use some common sense and go with the code which you believe is simplest.
我同意您应该使用一些常识并使用您认为最简单的代码。
BTW You can simplify you code and have just one return if you use ? :
顺便说一句,如果您使用,您可以简化代码并且只有一个返回 ? :
String foo() {
return cond1 ? a :
cond2 ? b :
cond3 ? c :
cond4 ? d : e;
}
回答by matt freake
"So is there a good reason why return statement is considered for computation of cyclomatic complexity? Can the logic for computation be changed so that it doesn't promote single exit point."
“那么,是否有充分的理由考虑将 return 语句用于计算圈复杂度?是否可以更改计算逻辑,使其不会促进单一出口点。”
In your example having multiple returns doesn't add to the complexity and as @Peter Lawrey says you should employ common sense.
在您的示例中,多次返回不会增加复杂性,正如@Peter Lawrey 所说,您应该运用常识。
Does this mean that allexamples of multiple return statements do notto complexity and it should be removed? I don't think so. If would be very easy to come up with an example of a method which is hard-to-read because of multiple return statements. Just imagine a 100 line method with 4 different return statement sprinkled throughout. That is the kind of issue this rules tries to catch.
这是否意味着多个 return 语句的所有示例都不复杂,应该将其删除?我不这么认为。如果想出一个由于多个 return 语句而难以阅读的方法示例将非常容易。想象一下一个 100 行的方法,其中散布着 4 个不同的 return 语句。这就是该规则试图解决的问题。
回答by Paul Johnson
This is a known problem with cyclomatic complexity.
这是一个已知的圈复杂度问题。
Also there is good reason to think that cyclomatic complexity is useless. It correlates strongly with SLOC and only weakly with actual bugs. In fact SLOC is just as good a predictor of defects as cyclomatic complexity. The same goes for most other complexity metrics.
也有充分的理由认为圈复杂度是无用的。它与 SLOC 有很强的相关性,而与实际错误的相关性很弱。事实上,SLOC 与圈复杂度一样可以很好地预测缺陷。大多数其他复杂性指标也是如此。
See http://www.leshatton.org/Documents/TAIC2008-29-08-2008.pdf, starting around slide 16.
请参阅http://www.leshatton.org/Documents/TAIC2008-29-08-2008.pdf,从幻灯片 16 开始。
回答by perfectionist
Other answers have made good points about the computation involved.
其他答案对所涉及的计算提出了很好的观点。
I'd like to point out that your assertion that the code is less readable is false, because in one instance you have braces, and in the other you don't.
我想指出您关于代码可读性较差的断言是错误的,因为在一种情况下您有大括号,而在另一种情况下您没有。
String foo() {
String output = e;
if (cond1) output = a;
else if (cond2) output = b;
else if (cond3) output = c;
else if (cond4) output = d;
return output;
}
This is as readable as the example you gave with return statements. Whether or not you allow braceless if statements is a question of style that you should probably be consistent with across all your code.
这与您使用 return 语句给出的示例一样具有可读性。是否允许无括号 if 语句是一个风格问题,您可能应该在所有代码中保持一致。
The more important issue that cyclomatic complexity doesaddress is that if computing the value of cond1, cond2 etc have side effects, i.e. if they were a stateful method rather than a field in this case, then the conceptual complexity of the code is much higher if you might return early compared to if you can't.
圈复杂度确实解决的更重要的问题是,如果计算 cond1、cond2 等的值有副作用,即如果在这种情况下它们是有状态的方法而不是字段,那么代码的概念复杂度要高得多,如果如果你不能,你可能会早点回来。