循环复杂性

时间:2013-03-28 04:35:38

标签: c# .net

编程的新手希望你们能帮我解决下面提到的那些问题。

我有一个程序,包括用于代码视图的文本框和用于查看下面每个问题的结果的标签以及用于执行这些功能的按钮。这是该项目的简单视图。

要解决的问题:

  1. 当我按下阅读文本框中代码的按钮并计算代码的复杂性时,我想要功能。
  2. 我需要知道代码中有多少属性和对象。
  3. 继承类在哪里。
  4. 什么是远耦合类。

  5. 实际上我一直试图制作代码很长时间但是我没有得到正确的结果。我完成了什么:

    1. 从.Cs文件中打开示例代码。
    2. 将代码读入字符串Builder。
    3. 使用下面的代码获得复杂性,但结果完全错误。
    4. 这是我的代码(我已经让它取决于数组中的匹配):

          public int Get_complexity(string SourceCode)
          {
              int result = 0;
              try
              {
                  StringBuilder sb = new StringBuilder();
                  sb.Append(SourceCode);
      
                  char[] delimiterChars = { ' ', '.', '{', '}', '(', ')', ';' };
      
                  string SourceCodeText = sb.ToString();
      
                  string[] words = SourceCodeText.Split(delimiterChars, StringSplitOptions.RemoveEmptyEntries);
      
                  int No_if = FindMatchesInStringArray("if", words);
                  int No_elseIf  = FindMatchesInStringArray("elseIf", words);
                  int No_while = FindMatchesInStringArray("while", words);
                  int No_for = FindMatchesInStringArray("for", words);
                  int No_foreach = FindMatchesInStringArray("foreach", words);
                  int No_case = FindMatchesInStringArray("case", words);
      
                  int No_default = FindMatchesInStringArray("default", words);
                  int No_finaly = FindMatchesInStringArray("finaly", words);
                  int No_continue = FindMatchesInStringArray("continue", words);
                  int No_continues = FindMatchesInStringArray("continues", words);
      
                  int No_try = FindMatchesInStringArray("try", words);
                  int No_catch = FindMatchesInStringArray("catch", words);
                  int No_and_op = FindMatchesInStringArray("&", words);
                  int No_or_op = FindMatchesInStringArray("||", words);
                  int No_words = words.Length;
      
                  result = No_if + No_elseIf + No_while + No_for + No_foreach + No_case + No_default + No_finaly + No_continue + No_continues + No_try + No_catch + No_and_op + No_or_op;
              }
              catch { throw; }
              return result;
          }
      
          public int FindMatchesInStringArray(string markup, string[] strArray)
          {
              int result = 0;
              try
              {
                  for (int i = 0; i < strArray.Length; i++)
                  {
                      if (markup.ToLower() == strArray[i].ToLower())
                      {
                          result += 1;
                      }
                  }
              }
              catch
              {
                  throw;
              }
              return result;
          }
      

1 个答案:

答案 0 :(得分:1)

检查NDepend网站定义并尝试模拟他们正在做的事情:

http://www.ndepend.com/Metrics.aspx#CC

  

Cyclomatic Complexity(CC):(为类型,方法定义)(仅适用于C#代码,VB.NET版本目前正在开发中)Cyclomatic复杂度是一种流行的程序软件度量,等于可以做出的决策数量采取的程序。具体地说,在C#中,方法的CC是1 + {在方法体中找到的以下表达式的数量}:

     

如果|而|为| foreach |案例|默认|继续|转到| &安培;&安培; | || |赶上|三元运算符?:| ??

     

以下表达式不计入CC计算:

     

其他|做|开关|试试|使用|扔|终于|返回|对象创建|方法调用|现场访问

     

Cyclomatic Complexity指标是根据方法定义的。适应OO世界,该度量也被定义为类和结构,作为其方法CC的总和。请注意,在计算其外部方法的CC时,不会计算匿名方法的CC。