Counting Lines of Code

For questions and postings not covered by the other forums
avinashrao
Posts: 6
Joined: Tue Nov 09, 2010 1:41 pm

Counting Lines of Code

Postby avinashrao » Thu Mar 22, 2012 8:51 am

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.

allistar
Posts: 156
Joined: Fri Aug 14, 2009 11:02 am
Location: Mount Maunganui, Tauranga

Re: Counting Lines of Code

Postby allistar » Thu Mar 22, 2012 9:32 am

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.

User avatar
Dr Danyo
Posts: 56
Joined: Fri Aug 21, 2009 8:59 am

Re: Counting Lines of Code

Postby Dr Danyo » Thu Mar 22, 2012 9:41 pm

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.

torrie
Posts: 92
Joined: Fri Aug 14, 2009 11:24 am

Re: Counting Lines of Code

Postby torrie » Mon Mar 26, 2012 7:05 am

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.


Return to “General Discussion”

Who is online

Users browsing this forum: No registered users and 24 guests