views:

1392

answers:

5

Are there any tools available for calculating the average number of lines of code per method?

I want to know the average size of each method, not just the total number of lines in the project. The per method count will allow me to measure how simple each method is.

This will be calculated as part of the build process, and displayed on a dashboard. The idea being that we can see if the average size of each method is increasing. And this will flag the possibility that code complexity is increasing and we may need to think about refactoring.

A: 

I am not sure if it does that, but searching, after your previous post, what is cyclomatic complexity, I went to the related Wikipedia page which pointed to Code Analyzer. There they say:

When counting for HTML or JSP files, it will count LoC correctly for javascript and vbscript code embedded within the <script> tag.

I don't know if this count is dispatched per method, but it might be worth taking a look (it is a free tool).

PhiLho
A: 

short fast and dirty : grep for ";", count the number of lines, this will give you an estimate of the number of statements.

That's pretty dirty, I don't use semicolons in JS at all (well, except for for() loops :))
Dan
Yeah. That depends on your style. Alternatively, you could search for "\n". But, again, that is prone to error.
EndangeredMassa
+1  A: 

Do a recursive "for(i in this)" search through your project, and if the object (i) is a function, call "test.toString().split("\n").length". This counts the number of newlines in the function. If it is not a function, but an object, call this function in that object. Also count the number of functions you find, and then divide the total number of newlines by the total number of functions, and then you have the average.

Edit

function calculateMethodSize(obj){
  var fcount = 0;
  var fsize = 0;
  for(i in obj){
    if(obj[i] instanceof Function){
      fcount++;
      fsize += obj[i].toString().split(";\n").length;
    }else if(obj[i] instanceof Object){
      var ret = calculateMethodSize(obj[i]);
      fcount += ret.fcount;
      fsize += ret.fsize;
    }
  }
  return {fsize:fsize, fcount:fcount};
}
var data = calculateMethodSize(this);
var average = data.fsize / data.fcount;

Be careful running this code though. If you run it with this, as I have done, then you might get a stack overflow (I did).

Marius
Sounds like an interesting approach.Can you clear up how the "for(i in this)" search would work?
Karl
I like the idea of this approach as I don't have to parse the code myself. Unfortunately it doesn't work with the way I declare objects using:var obj = function() { this.method = function(a) { return a + 1; } }
Karl
A: 

Define lines as either "\n" or ";",

You could try a simple algorithm like the following:

FOR each line in a javascript file (or chunk of text)
    IF the line starts with "function " THEN
        PUSH the first left-curly brace you find onto a stack
        WHILE the stack is non-empty
            PUSH any left-curly braces in the current line
            POP any left-curly braces when you encounter a right-curly brace
            Increment your line-count by 1
            Increment your line counter (as mentioned in the FOR loop above)
        END WHILE
        Store your total lines for this function            
     ELSE
         //ignore the line because it's probably a global var or blank
     END IF
END FOR

I don't know of a tool that can do this automatically. But, it would be fun to try to make one yourself.

EndangeredMassa
A: 

you probably want to clue in other metrics as well. any way you count the lines, just make sure it does not croak in face of functions defined without the "function" keyword or curly braces. real-world example:

var negate = bind1st(compose, not);

(here negate is a function built from functions bind1st, compose and not)

just somebody