.net - 如何分析一个方法中有多少个不同的代码路径
是否有一个(首选免费)工具可以分析一种方法中可能有多少种不同的组合?我目前正在重构一个有很多 if/switch 语句的方法,我很好奇这个方法有多少种可能的不同执行方式。
假设我有一个简单的方法:
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");
}
应该有 6 种可能的方法来执行此方法。 我知道有一些工具可以为我计算这个数字(我相信 Visual Studio Ultimate 可以做到这一点,但不幸的是我只有专业版)。
也许有人知道一个可以做到这一点的好工具。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
Visual Studio 的代码指标
您需要圈复杂度。
Code Metrics for Visual Studio
You need cyclomatic complexity.
您还可以使用工具 NDepend(集成在 Visual Studio 2010、2008、2005 中)来计算.NET 代码:
如果您在没有相应源代码的情况下反编译程序集,则 IL 代码的循环复杂度非常有用。
You can also use the tool NDepend (integrated in Visual Studio 2010, 2008, 2005) that can compute the Cyclomatic Complexity on .NET code:
The Cyclomatic Complexity on IL code is useful if you decompile an assembly without having the corresponding source code.
CodeMetrics 是一个免费插件,即将不再免费反射器。不知道有多好,我得到了正确的 VS 版本。
CodeMetrics is a free add-on for the soon no-longer-free Reflector. No idea how good it is, I got the right version of VS.
你的意思是循环复杂度,这个计算已经包含在VS 2010中,检查此处。
What you mean is the Cyclomatic Complexity, and this calculation is already included with VS 2010, check here on MSDN.
我一直在使用一个可以集成到VS2008和VS2010中的工具。它可以从 http://www.blunck.info/ccm.html 找到。
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.
您正在寻找的指标是“圈复杂度”。有一些免费工具,但我只使用了 VS 2008 pro 内置指标,所以我无法判断它们,但它应该更容易找到它们。
例如 http://www.codeproject.com/KB/architecture/Cyclomatic_Complexity.aspx
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