计算环量复杂性?

发布于 2025-01-20 16:40:58 字数 533 浏览 1 评论 0原文

您如何正确计算循环复杂性?

根据wikipedia 以下代码的周期性复杂性,以下代码的周期性复杂

if (c1())
    f1();
else
    f2();

if (c2())
    f3();
else
    f4();

性:

  • 2 * 2 = 4
  • 看图(请参阅图像)有4个不同的路径-2(左,左,左,左,右)。

我缺少什么?

How do you properly calculate Cyclmetric Complexity?

According to Wikipedia the cyclometric complexity of the following code:

if (c1())
    f1();
else
    f2();

if (c2())
    f3();
else
    f4();

is 3. But I understand it as 4:

  • 2 * 2 = 4
  • Looking at the graph (see image) there are 4 distinct paths - 2 (left left, left right, right left, right right).

enter image description here

What am I missing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

黑色毁心梦 2025-01-27 16:40:58

循环复杂度和完整分支覆盖率之间存在细微差别。

循环复杂度是一种纯粹的数学度量。简单地说 - 测量将新分支添加到当前路径集的路径数量。

所以在你的例子中 - 我们从一个空集开始。 false/false 是一条路径 (#1),然后 true/false 添加一个新分支(第一个 if)(#2) 。然后 false/true 添加一个新分支(第二个 if)(#3)。
但是路径 true/true > 不会向集合添加新分支,因为它访问的所有点也可以通过组合路径 #2 和路径 #3 来访问。

无论您将路径添加到集合中的顺序是什么,此结果都会保留 - 最后一条(第四条)路径始终不会添加先前路径未访问过的新分支。

完全分支覆盖 - 另一方面 - 更多的是一个软件指标。因为我们知道,在软件中有时一起测试两个分支可能会给出不同的结果,然后仅依靠测试第一个分支并单独测试第二个分支。

在图论中,循环复杂度不考虑分支之间的交叉关系(仅考虑“新”分支的数量),但在软件工程中,如果您想要完全覆盖,则需要测试所有可能的路径。

一般来说 - 所需的最小测试数量 <= 循环复杂度 <= 全分支覆盖的测试数量。

There is a subtle difference between cyclometric complexity and full branch coverage.

Cyclometric complexity is a pure mathematical metric. Plainly put - measuring the number of paths that add a new branch to the current paths set.

So in your example - we start with an empty set. false/false is one path (#1), and then true/false adds a new branch (the first if) (#2). Then false/true adds a new branch (the second if) (#3).
However the path true/true does not add a new branch to the set, since all of the points it visits can also be visited by combining path #2 and path #3.

This results remains no matter what is the order that you're adding the paths to the set - always the last (4th) path will not add a new branch that wasn't visited by the previous paths.

Full branch coverage - on the other hand - is more of a software metric. Since we know that in software sometimes testing two branches together can give a different result then just relying on testing the first branch, and separately testing the second branch.

In graph theory cyclometric complexity does not take into account those cross-relations between the branches (just the number of "new" branches), but in software engineering if you want full coverage you need to test all of the possible paths.

Generally - number of minimal tests required <= cyclometric complexity <= number of tests for full branch coverage.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文