Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.net - Howto analyze how many different code paths are in a method

Is there a (preferred free) tool that can analyze how many different combinations are possible in a method? I am currently refactoring a method that has many many if/switch statments and I am curious how many possible different execution ways this method had.

Let's say I have a simple method:

public void DoSomething(bool flag1, int value)
{

    if (flag1)
    {
        if (value > 0)
        {
            Console.WriteLine("Flag1 & value > 0");
            return;
        }
        else
        {
            Console.WriteLine("Flag1 & value <= 0");
            return;
        }
    }
    elseif (value > 0 and value < 10)
    {
        Console.WriteLine("Flag1 is false and value between 0 & 10");
        return;
    }

    if (value < 0)
    {
        Console.WriteLine("Flag1 = false & value <= 0");
        return;
    }
    elseif(value = 0)
    {
        Console.WriteLine("Flag1 = false & value >= 10");
        return;
    }

    Console.WriteLine("nothing else matched");

}

there should be 6 possible ways in which the this method could be executed. I know there are tools out there that can calculate this number for me (I believe Visual Studio Ultimate can do this but unfortunately I only own a Professional version).

Maybe someone knows a good tool, that can do this.

like image 675
Jürgen Steinblock Avatar asked Mar 01 '11 17:03

Jürgen Steinblock


3 Answers

What you mean is the Cyclomatic Complexity, and this calculation is already included with VS 2010, check here on MSDN.

like image 118
BrokenGlass Avatar answered Nov 09 '22 02:11

BrokenGlass


I have been using a tool that can be integrated into VS2008 and VS2010. It can be found from http://www.blunck.info/ccm.html.

like image 27
user639785 Avatar answered Nov 09 '22 03:11

user639785


The metric you're looking for is "cyclomatic complexity." There are some free tools but I've used only the VS 2008 pro built-in metrics so I can't judge them, but it should make it easier to find them.

e.g. http://www.codeproject.com/KB/architecture/Cyclomatic_Complexity.aspx

like image 1
Mark Sowul Avatar answered Nov 09 '22 03:11

Mark Sowul