问题
I wanted to ask whether you know about some free C# libraries (dlls) that calculate CK metrics (mainly Cyclomatic Complexity).
I would need that for a project I'm planning to do. I know that there are already some finished solutions that calculate CK metrics and display it to you in various forms, but what I would need is one that I could use from within my application. So before starting and writing one myself I first wanted to ask you.
Thanks
回答1:
DrivenMetrics is a open source C# command line tool. The core functionalities are isolated from the command line console client as a library (Core project is available here).
Even if quite simple, it may fit your need: it's free, counts the the number of lines and calculates the cyclomatic complexity (number of potential code paths) of methods.
This is performed through direct analysis of the IL thanks to Mono.Cecil (the same library NDepend relies on). This allows the analysis to be performed on assemblies built from code written in C#, VB.Net,...
- The project has been announced here.
- The code source is available on github.
- A packaged release is also available.
- It works both on Windows and Mono.
UPDATE:
Another option would be the amazing Gendarme, a static analysis tool from the Mono project.
As a sample of usage, the code below display the cyclomatic complexity of every method in an assembly.
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);
}
}
- The project is described here
- The code source is available on github
- Packaged releases are also available
- It works both on Windows and Mono
回答2:
I am using SourceMonitor, which is a nice freeware app that measures code complexity and other metrics for a variety of languages including C#. We drive it from the command line to produce XML output, then we use LINQ to XML to extract and sort the data we are interested in. We then use NVelocity to create HTML reports.
I know its not a managed library, but you might find it can do what you need.
回答3:
There is a tool from Microsoft I am using to compute code metrics for C# assemblies.
It includes cyclo complex, maintainability index and more.
Details here:
http://blogs.msdn.com/b/camerons/archive/2011/01/28/code-metrics-from-the-command-line.aspx
Download here:
http://www.microsoft.com/en-us/download/details.aspx?id=9422
回答4:
It isn't free but I've had good experiences with NCover for this sort of thing. They also integrate pretty well with a lot of CI tools out there.
回答5:
With 82 code metrics supported NDepend is the code metrics Roll's Royce tooling for .NET developers (however it is a commercial tool).
来源:https://stackoverflow.com/questions/1052269/free-c-sharp-metrics-calculation-library-dll