views:

255

answers:

4

How to get code complexity metrics for a program?

well i wanted to create a program that check and score a code on how complex it is for example how many loop, if statements, if there is any nested loops etc. so for example a code with no loops, or if staement is not complicated.

EDIT: note that this question is about code complexity, not algorithmic complexity

A: 

In the worst case, you can never write such a problem because of this. But practically, you could if you timed the program on multiple inputs and regressed time on input space. But I wouldn't recommend doing that -- you should be able to look at it and figure out the complexity.

twolfe18
+4  A: 

Cyclomatic code complexity? Have a look at Cobertura.

Bruno Rothgiesser
+1  A: 

If you simply want to produce complexity metrics for a Java codebase (also C, C# etc.), I can strongly recommend Source Monitor, at http://www.campwoodsw.com/sourcemonitor.html - it's free, it's great.

anon
A: 

Your question is quite unclear, I'm going to assume you meant determining Big O notation programatically.

If you know the input space of your program, you can sort of estimate the complexity using these set of steps:

  • Generate a set of inputs that your program/algorithm can take.
  • Measure the amount of time your program takes for each input.
  • Plot your time vs input size chart, then perform a regression line analysis on the plot to estimate Big O, be it X^2, ln(x), 2^X, etc.
yx