具有多个退出点的代码的循环复杂度
如何求具有多个出口点的函数的圈复杂度? 维基页面说 p-s+2 其中 p 是决策点的数量,s 是退出点的数量。
但是,更多的退出点是否会增加圈复杂度,因为它可能会导致更多独立的路径?
干杯,
阿曼
How to find the cyclomatic complexity of a function with multiple exit points?
The wiki page says
p-s+2 where p is the number of decision points and s is the number of exit points.
But should not more exit points increase the cyclomatic complexity as it may lead to more independent paths?
Cheers,
Aman
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
CC 测量线性独立路径。退出点不会向代码添加路径,它们会终止路径,从而减少 CC(或者至少,它们肯定不会增加 CC)。
换句话说,添加退出点的唯一方法是添加更多路径(如 IF 等条件)。否则,“裸”退出点之后的代码将无法访问,因此增加复杂性的是条件,而不是退出点。
CC measures linearly independent paths. Exit points don't ADD paths to the code, they TERMINATE paths, thus reducing CC (or in the very least, they certainly don't increase CC).
To put it another way, the ONLY way to add exit points is to ADD more paths (conditions like IF). Otherwise, the code after the 'naked' exit point is unreachable, so it is the conditionals that add complexity, not exit points.
为什么不尝试试用 NDepend?它将计算圈复杂度和许多其他代码度量。
Why not try a trial of NDepend? It will calculate cyclomatic complexity and many other code metrics.
谢谢迈克尔。在我发布问题后我意识到了我的错误。我的错误源于对 JavaNcss(使用源文件)和 Xdepend 的观察
(使用 jar 文件)似乎都高估了一段具有多个退出点的代码的 CC。我已在此处发布了代码。
但使用公式 p-s+2 答案似乎是 4。我缺少一些简单的解释吗?
@Richard:我尝试过 Xdepend(Ndepend 的非 .NET 版本)。它看起来是一个很好的工具。但是当它使用 jar 文件时,它会高估 CC(他们在文档中已经接受了 CC)。在这个阶段,我正在探索不同的工具。您知道还有更好的吗?
干杯。
Thanks michael. I had realised my mistake after i posted the question. My mistake stemmed from the observation that JavaNcss(which uses the source file) and Xdepend
(which uses the jar file) both seemed to overestimate the CC for a piece of code which had multiple exit points. I have posted the code here .
But using the formula p-s+2 the answer seems to be 4. Is there some simple explanation i am missing ?
@Richard: I have tried Xdepend( The non .NET version of Ndepend). It looks a good tool. But when it uses jar files, it overestimates the CC ( which they have accepted in their documentation). At this stage i am exploring different tools. Are you aware of any better one?
Cheers.