c++ - 降低 C++ 中的圈复杂度

标签 c++

关闭。这个问题需要details or clarity .它目前不接受答案。












想改进这个问题?通过 editing this post 添加详细信息并澄清问题.

1年前关闭。




Improve this question




我想问一个带有多个条件的简单 if else 语句如何
可以降低圈复杂度吗?
例如:

   std::string x; 
   if(cond)
     x = "Value1";
   else if(cond2)
   {
    if(cond3)
       x = "Value2";
    else if(cond4)
       x = "Value3";
    else
       x = "Value4";  
   }
   else if(cond5)
     x = "Value6";
   else if(cond6)
     x = "Value7";
       
   


   

最佳答案

我不得不谷歌它。 Wikipedia说:

Cyclomatic complexity is a software metric used to indicate the complexity of a program. It is a quantitative measure of the number of linearly independent paths through a program's source code.


您可以绘制表格或图形:
                            *
                            |
                   ----------------------
                  |      |      |       |
                cond   cond2  cond5   cond6
                  |      |      |       |
                x+=1     |    x+=5    x+=6
                         |
                 -----------------
                 |       |       |
               cond3   cond4   else
                 |       |       |
               x+=2    x+=3    x+=4
现在您可以看到没有两个分支导致相同的结果。如果我们能找到导致相同结果的分支,那么我们可以尝试减少路径的数量。同样没有更多信息,我不得不假设所有条件都是独立的。如果它们之间有关系,例如cond2 => cond3那么你可以减少分支的数量。

关于c++ - 降低 C++ 中的圈复杂度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64593697/

相关文章:

c++ - 从 msvc2008 上失败的 vsnprintf 中恢复 graceflly

c++ - 哪种数据结构用于求解简单的数学方程式

c++ - 为什么这个 SFML C++ 代码不起作用?

c++ - 如何列出 XCOFF TOC 条目

c++ - 头文件中的函数定义,因为函数是从不同的项目中使用的

c++ - { } 和等号变量之间的区别

c++ - 写入数组元素 [i][0][1] 也会覆盖 [i][1][0],反之亦然

c++ - ->* 运算符应该在何时何地重载?

c++ - 由于多个抽象基类,实现两个同名但不同的非协变返回类型的函数

c++ - QTableView View 什么都不显示