麦凯布的圈复杂性 [英] McCabe's cyclomatic complexity

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

问题描述

要计算代码的圈复杂度,我绘制了一个由节点和边组成的控制流程图,这有助于我计算V(G)= E-N + 2
在我的情况下,E = 15和N = 11.导致6的旋回复杂度。

To calculate the cyclomatic complexity of a code, I drew a control flow chart consisting of nodes and edges which helped me to calculate V (G) = E - N + 2 In my case E = 15 and N = 11. Resulting in a cyclomativ complexity of 6.

现在要确认我的答案,我想为查找代码打击的线性独立路径提供一些帮助:

Now to confirm my answer I would like some help on finding linearly independent paths for the code blow:

int maxValue = m[0][0];         
for (int i = 0; i < N; i++)         
{                       
   for (int j = 0; j < N; j++)          
   {                        
      if ( m[i][j] > maxValue )         
      {                     
         maxValue = m[i][j];            
      }                     
   }                        
}                   
cout << maxValue << endl;           
int sum = 0;                    
for (int i = 0; i < N; i++)         
{                       
   for (int j = 0; j < N; j++)          
   {                        
      sum = sum + m[i][j];          
   }                        
}                           
cout << sum << endl;  

这应该等于我的V(G)的结果,否则我的计算是错误的。谢谢您的帮助。

This should equal the result for my V (G), otherwise my calculation is wrong. Thank you for your help.

推荐答案

McCabe的圈复杂度给出了一个上限。请考虑以下内容:

McCabe's cyclomatic complexity gives an upper bound. Consider the following:

void func (const bool do_special) {
    if (do_special) {
        do_something_special_at_the_start();
    }

    always_do_this_stuff_in_the_middle();

    if (do_special) {
        do_something_special_at_the_end();
}

从图论的角度来看,其圈复杂度为3。但是,由于 do_special 是常量,因此代码中只有两条独立的路径。图论模型并不知道某些路径是不可能的。通过图的可能路径的数量有时小于圈复杂度。

From a graph theory perspective, this has a cyclomatic complexity of three. However, since do_special is constant, there are only two independent paths through the code. The graph theory model doesn't know that some paths are impossible. The number of possible paths through the graph is sometimes less than the cyclomatic complexity.

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

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