views:

267

answers:

4

Hi all.

I'm given a string which contains an arbitrary amount of code. I want to calculate a number which represents the code complexity of that string. Something like:

int complexity = Lib.FindComplexity(someString);

I realize there are a lot of tools out there that will do this for you. These tools will not work for me, because I want to do it programmatically. I'd love for the library to be in C#, but will work with anything at this point.

Thanks in advance!

+1  A: 

Have you considered using one of those existing tools and wrapping it in a library? For instance, you might be able to use the NDepend.Console.exe by calling it from your code with the parameters you want, and parse out the result.

Brian Genisio
+1  A: 

NDepend is a great tool, although not cheap at the time I looked at it. If money isn't an option, I'd look into using reflection and http://en.wikipedia.org/wiki/Cyclomatic_complexity. It doesn't meet your requirement with any string but you could definitely test assemblies you created.

Joshua Belden
A: 

You can also use reflector and the code metrics plug-in available for it.

Greco
A: 

A library such as this must be able to parse an arbitrary language fragment, and then compute the complexity metrics over the parsed fragment. Most metrics tools have at best a parser for the entire language, not just a fragment, so you are likely to be hard pressed to find many solutions.

There is one system that can provide you what you need: our DMS Software Reengineering Toolkit. It provides parsers for many languages (such as Java and C#; it is unclear what language you want to analyze). DMS has been used already to implement these kinds of metrics for several langauges (Java, C#, JavaScript, COBOL) and the process of doing this is straigtforward. And DMS does parse langauge fragments, and amazingly, the metrics implementation actually operate on such fragments. You could customize DMS to implement exactly what you want. See http://www.semanticdesigns.com/Products/DMS/DMSToolkit.html and for derived metrics tools, http://www.semdesigns.com/Products/Metrics/index.html

Ira Baxter