Cyclomatic Complexity metrics are incorrectly summed, so are incorrect above the Method level
-
2012年2月26日 20:08
Cyclomatic Complexity is essentially B + 1, where B is the number of branches (aka decision points or conditions) in the code. However, by it's very definition, this means that summing a number of CC values by directly adding them all together is incorrect. Instead, you must subtract 1 from each value, add them together, and then add 1.
For example, if a simple Class has 3 Methods, and each method has no conditions at all and therefore a CC metric of 1, then the CC metric of the Class must also be 1 and NOT 3. It is definitely incorrect to indicate a CC metric of 3 for such a Class, which contains only linear code with no branches or loops. And yet, VS Metrics and FxCop simply sum all of the CC metrics at the Class and higher levels, yielding incorrect and misleading metrics.
全部回复
-
2012年2月27日 7:16版主
cyclomatic complexity = the number of edges - the number of nodes + 1
namespace ConsoleApplication1 { class Program { static void Main(string[] args) { } } public class Stu { public void Methoda() { Console.WriteLine(); } public void Methodb() { } public void Methodc() { } } }If there's no If or switch statement, it should be 0-0+1=1, so the result is correct, even there's no code line in the method body, but it also has a one path control flow for execution.
Mike Zhang[MSFT]
MSDN Community Support | Feedback to us
- 已建议为答案 Mike Dos ZhangMicrosoft Contingent Staff, Moderator 2012年3月1日 6:28
-
2012年3月1日 6:28版主I am writing to check the status of the issue on your side.
What about this problem now?
Would you mind letting us know the result of the suggestions?Mike Zhang[MSFT]
MSDN Community Support | Feedback to us
-
2012年3月6日 15:22
Hi,
We've noticed the same issue whilst running against the standard MVC3 application that is created when you start a new project just to test the usage of the metrics.exe tool within a build. The cyclomatic complexity comes out at 65! We can see that it is just adding the figures together from each namespace and type.
The same report using NCover comes out at 10.
There is clearly some kind of issue with this.
Any suggestions?
Thanks
Dan
-
2012年3月7日 11:04版主
Since we cannot use the NCover tool to verify the result, and research it.
If you think it would be the metrics.exe tool's issue, then can you help us to submit the details and yours concern on this web site, thanks.
Mike Zhang[MSFT]
MSDN Community Support | Feedback to us

