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:
- I want function when I press the button that read the code in the textbox and calculate the complexity for the code.
- I need to know how many attribute and object in the code.
- where is the inheritance classes.
- 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 :
- open the sample code from .Cs file.
- read the code to string Builder.
- 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;
}
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