什么是圈复杂度? [英] What is Cyclomatic Complexity?

查看:34
本文介绍了什么是圈复杂度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我时不时看到的一个术语是圈复杂度".在 SO 上,我看到了一些关于如何计算语言 X 的 CC"或我如何用最少的 CC 做 Y"的问题,但我不确定我是否真的理解它是什么.

A term that I see every now and then is "Cyclomatic Complexity". Here on SO I saw some Questions about "how to calculate the CC of Language X" or "How do I do Y with the minimum amount of CC", but I'm not sure I really understand what it is.

NDepend 网站 上,我看到一个解释,基本上是方法中的决策.每个 if、for 和 & 等都会为 CC分数"增加 +1.真的是这样吗?如果是,为什么这很糟糕?我可以看到人们可能想要保留为使代码易于理解,if 语句的数量相当少,但这真的是全部吗?

On the NDepend Website, I saw an explanation that basically says "The number of decisions in a method. Each if, for, && etc. adds +1 to the CC "score"). Is that really it? If yes, why is this bad? I can see that one might want to keep the number of if-statements fairly low to keep the code easy to understand, but is this really everything to it?

或者有更深层次的概念吗?

Or is there some deeper concept to it?

推荐答案

我不知道更深层次的概念.我相信它通常是在可维护性指数的背景下考虑的.特定方法中的分支越多,维护该方法操作的心智模型(通常)就越困难.

I'm not aware of a deeper concept. I believe it's generally considered in the context of a maintainability index. The more branches there are within a particular method, the more difficult it is to maintain a mental model of that method's operation (generally).

具有更高圈复杂度的方法也更难在单元测试中获得完整的代码覆盖率.(感谢 马克 W!)

Methods with higher cyclomatic complexity are also more difficult to obtain full code coverage on in unit tests. (Thanks Mark W!)

当然,这带来了可维护性的所有其他方面.错误/回归/等等的可能性.不过,核心概念非常简单.

That brings all the other aspects of maintainability in, of course. Likelihood of errors/regressions/so forth. The core concept is pretty straight-forward, though.

这篇关于什么是圈复杂度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆