-4

still new to the programming hope you guys give me a hand in those problem stated below.

I have a program consist of textbox for code view and labels for viewing the result for each problem below and button to execute the functions. this is the simple view for the project.

problems to solve:

  1. I want function when I press the button that read the code in the textbox and calculate the complexity for the code.
  2. I need to know how many attribute and object in the code.
  3. where is the inheritance classes.
  4. what are the far coupling classes.

Actually I've been trying to make the code for long time but I didn't get the right result. What I finished :

  1. open the sample code from .Cs file.
  2. read the code to string Builder.
  3. get the complexity with the code below, but the result is totally wrong.

this is my code (I've made it depends on match in array):

    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;
    }
  • Which part are you having a problem with? Also, what's the point of your try/catch with immediate throw? That's the default behavior of all C# code. – Matthew Mar 28 '13 at 04:37
  • I am not quite sure what this has to do with cyclomatic complexity. Where are you confused in the process? – Woot4Moo Mar 28 '13 at 04:37
  • 2
    This isn't finding cyclomatic complexity. It is counting keywords.. – Simon Whitehead Mar 28 '13 at 04:38
  • I think you can find your answer in a previous question: http://stackoverflow.com/questions/727756/deriving-cyclomatic-complexity-in-net – Roy Ashbrook Mar 28 '13 at 04:50
  • You've now turned this question into 4 or 5 questions and you still have not provided a Cyclomatic Complexity snippet. You need to ask a specific question and show us your specific problem. – Simon Whitehead Mar 28 '13 at 04:56
  • I know guys that is so confused but the only way I found it to get the complexity by counting the opration inside the code. it give me an approximate result for the simple code but when the code be more than 10 line it's totally wrong. – Yazan A. Sarierah Mar 28 '13 at 04:56
  • @RoyAshbrook : this code doesn't take text
    ModuleDefinition module = ModuleDefinition.ReadModule(fullPathToTheAssembly); foreach (var type in module.Types) { foreach (var me in type.Methods) { if (!me.HasBody || me.IsGeneratedCode() || me.IsCompilerControlled) continue; var r = AvoidComplexMethodsRule.GetCyclomaticComplexity(me); Console.WriteLine("{0}: {1}", me.ToString(), r); } }
    I don't know how to make text as module
    – Yazan A. Sarierah Mar 28 '13 at 04:58

1 Answers1

1

Check the NDepend site definition and try and emulate what they are doing:

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

Cyclomatic Complexity (CC): (defined for types, methods) (Only available for C# code, a VB.NET version is currently under development) Cyclomatic complexity is a popular procedural software metric equal to the number of decisions that can be taken in a procedure. Concretely, in C# the CC of a method is 1 + {the number of following expressions found in the body of the method}:

if | while | for | foreach | case | default | continue | goto | && | || | catch | ternary operator ?: | ??

Following expressions are not counted for CC computation:

else | do | switch | try | using | throw | finally | return | object creation | method call | field access

The Cyclomatic Complexity metric is defined on methods. Adapted to the OO world, this metric is also defined for classes and structures as the sum of its methods CC. Notice that the CC of an anonymous method is not counted when computing the CC of its outer method.

Community
  • 1
  • 1
Roy Ashbrook
  • 814
  • 8
  • 14