如何提高圈复杂度? [英] How to improve Cyclomatic Complexity?

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

问题描述

圈复杂度会高,是高一些判断语句,包括如果/当/ for语句的方法。那么我们该如何改善呢?



因为我正在处理一个大的项目中,我应该到消委会减少有CC> 10,而且有这个问题很多方法的方法。下面我将列出了一些例如代码模式(而不是实际的代码)与我所遇到的问题。 ?难道他们可以简化



造成很多决策语句的情况下的例子:



案例1 )

 如果(对象A!= NULL)//对象A是作为一个参数
{$ b $通行证b对象b = doThisMethod();
如果(!=对象B NULL)
{
objectC = doThatMethod();
如果(objectC!= NULL)
{
的doXXX();
}
,否则{
doYYY();
}
}
,否则
{
doZZZ();
}
}



案例2)

 如果(A<分)
MIN =一个;

如果(A<最大)
最大=一个;

如果(B&0)
的doXXX();

如果(c取代; 0)
{
doYYY();
}
,否则
{
doZZZ();
如果(C> D)
IsTrue运算= FALSE;

的for(int i = 0; I< D组;我++)
S [i] = I * D;

如果(IsTrue运算)
{
如果(E> 1)
{
doALotOfStuff();
}
}
}



案例3)

  //注意,这些字符串常量用于其他地方差异组合,
//所以你无法将它们合并为一个
如果(e.PropertyName.Equals(StringConstants.AAA)||
e.PropertyName.Equals(StringConstants.BBB)||
e.PropertyName.Equals(StringConstants.CCC)||
e.PropertyName.Equals(StringConstants.DDD)||
e.PropertyName.Equals(StringConstants.EEE)||
e.PropertyName.Equals(StringConstants.FFF)||
e.PropertyName.Equals(StringConstants.GGG)||
e.PropertyName.Equals(StringConstants.HHH)||
e.PropertyName.Equals(StringConstants.III)||
e.PropertyName.Equals(StringConstants.JJJ)||
e.PropertyName.Equals(StringConstants.KKK))
{
doStuff();
}


解决方案

案例1 - 只需通过重构为更小的功能处理这个。例如。下面的代码片段可能是一个功能:

  objectC = doThatMethod(); 
如果(objectC!= NULL)
{
的doXXX();
}
,否则{
doYYY();
}



案例2 - 完全一样的做法。就拿else子句中的内容进行到一个较小的辅助函数



案例3 - 让你想检查对字符串列表,并作出比较,对许多选项的字符串(可以使用LINQ进一步简化)

  VAR stringConstants =新的字符串的小助手功能[] {StringConstants.AAA,StringConstants.BBB等}; 
如果(stringConstants.Any((S)=> e.PropertyName.Equals(S))
{

}


Cyclomatic Complexity will be high for methods with a high number of decision statements including if/while/for statements. So how do we improve on it?

Cos I am handling a big project where I am supposed to reduced the CC for methods that have CC > 10. And there are many methods with this problem. Below I will list down some eg of code patterns (not the actual code) with the problems I have encountered. Is it possible that they can be simplified?

Example of cases resulting in many decision statements:

Case 1)

if(objectA != null) //objectA is a pass in as a parameter
{
 objectB = doThisMethod();
 if(objectB != null)
 {
  objectC = doThatMethod();
  if(objectC != null)
  {
   doXXX();
  }
  else{
   doYYY();
  }
 }
 else
 {
  doZZZ();
 }
}

Case 2)

if(a < min)
 min = a;

if(a < max)
 max = a;

if(b > 0)
 doXXX();

if(c > 0)
{
 doYYY();
}
else
{
 doZZZ();
 if(c > d)
  isTrue = false;

 for(int i=0; i<d; i++)
  s[i] = i*d;

 if(isTrue)
 {
  if(e > 1)
  {
   doALotOfStuff();
  }
 }
}

Case 3)

//note that these String Constants are used elsewhere as diff combination,
//so u can't combine them as one
if(e.PropertyName.Equals(StringConstants.AAA) ||
e.PropertyName.Equals(StringConstants.BBB) ||
e.PropertyName.Equals(StringConstants.CCC) ||
e.PropertyName.Equals(StringConstants.DDD) ||
e.PropertyName.Equals(StringConstants.EEE) ||
e.PropertyName.Equals(StringConstants.FFF) ||
e.PropertyName.Equals(StringConstants.GGG) ||
e.PropertyName.Equals(StringConstants.HHH) ||
e.PropertyName.Equals(StringConstants.III) ||
e.PropertyName.Equals(StringConstants.JJJ) ||
e.PropertyName.Equals(StringConstants.KKK)) 
{
 doStuff();
}

解决方案

Case 1 - deal with this simply by refactoring into smaller functions. E.g. the following snippet could be a function:

objectC = doThatMethod();
if(objectC != null)
{
 doXXX();
}
else{
 doYYY();
}

Case 2 - exactly the same approach. Take the contents of the else clause out into a smaller helper function

Case 3 - make a list of the strings you want to check against, and make a small helper function that compares a string against many options (could be simplified further with linq)

var stringConstants = new string[] { StringConstants.AAA, StringConstants.BBB etc };
if(stringConstants.Any((s) => e.PropertyName.Equals(s))
{
    ...
}

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

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