Page 1 of 1
Counting Lines of Code
Posted: Thu Mar 22, 2012 8:51 am
by avinashrao
Hi all,
Just a quick question - does anyone know a way to count the lines of user-written code in a schema or schemas?
Cheers in advance,
Avinash.
Re: Counting Lines of Code
Posted: Thu Mar 22, 2012 9:32 am
by allistar
Hi there,
A naive way of doing this is to count the number of Lf or Cr characters in the source for each method. Here is some (poorly commented) source that will write out the number of classes, methods and lines of code for the entire database. This assumes that schemas don't have source stripped off them:
Code: Select all
constants
Debug = false;
vars
classClass, class: Class;
numberOfClasses, numberOfMethods, linesOfCode: Integer;
meth: Method;
code: String;
classes: ClassColl;
object: Object;
delta: DeltaInfo;
sch: Schema;
schemas: SchemaColl;
prim: Type;
begin
create schemas transient;
rootSchema.allSubschemas(schemas);
foreach sch in schemas do
numberOfMethods := 0;
numberOfClasses := 0;
linesOfCode := 0;
create classes transient;
classes.add(Class);
Class.allSubclasses(classes);
Class.allSubclassesInSubschemas(classes);
foreach classClass in classes do
foreach object in classClass.instances where (object.Class.schema = sch) do
class := object.Class;
if (class.isKindOf(JadeImportedClass)) then
continue;
endif;
foreach meth in class.getGreentreeMethods do
code := meth.getPropertyValue("source").String;
if (code <> "") then
linesOfCode := linesOfCode + code.noOfLines();
endif;
numberOfMethods := numberOfMethods + 1;
if (Debug) then
write "Method " & class.name & " " & meth.name;
endif;
endforeach;
if (class.superschemaType = null) then
numberOfClasses := numberOfClasses + 1;
if (Debug) then
write "Class " & class.name;
endif;
endif;
endforeach;
endforeach;
foreach prim in sch.allPrimitives() do
prim := sch.findName(prim.name);
foreach meth in prim.getPropertyValue("methods").MethodNDict where (meth.schemaType.schema = sch) do
code := meth.getPropertyValue("source").String;
if (code <> "") then
linesOfCode := linesOfCode + code.noOfLines();
endif;
numberOfMethods := numberOfMethods + 1;
if (Debug) then
write "Method " & prim.name & " " & meth.name;
endif;
endforeach;
endforeach;
delete classes;
write sch.name & "," & numberOfClasses.String & "," & numberOfMethods.String & "," & linesOfCode.String;
endforeach;
epilog
delete schemas;
delete classes;
end;
The method String::noOfLines is (embarassingly) this:
Code: Select all
noOfLines():Integer;
//returns the number of lines in the string
vars
i, count: Integer;
begin
i := 1;
count := 1;
while (i < self.length) do
if (self[i] = Cr) then
count := count + 1;
endif;
i := i + 1;
endwhile;
return count;
end;
This doesn't count methods on primitive types. Hope this helps.
Re: Counting Lines of Code
Posted: Thu Mar 22, 2012 9:41 pm
by Dr Danyo
Hi Avinash,
Just to be clear are you after the physical lines of code (LOC) or the logical lines of code (LLOC) ?
http://en.wikipedia.org/wiki/Source_lines_of_code
Dr Danyo.
Hi all,
Just a quick question - does anyone know a way to count the lines of user-written code in a schema or schemas?
Cheers in advance,
Avinash.
Re: Counting Lines of Code
Posted: Mon Mar 26, 2012 7:05 am
by torrie
Do you use the JadeCareStart schemas? (you can download these from Jade.) There is a CnSchemaAnalyser class in there that produces some statistics including lines of code. It ignores some comments and blank lines.
Otherwise you might be better counting ";" and branching statements (if, while etc) rather than new lines.