Rochdi Badis · Aug 17, 2022

Counting line of code in all classes

Hi Guys,

is there an easy way to count all line of codes in all classes in a namespace ?

I've been asked to give the total lines of code in all our system and we have over 3000 classes, so opening each one of them to check total line would take a long time.



Product version: Caché 2014.1
0 170
Discussion (10)2
Log in or sign up to continue

I'm sure, someone has a more elegant solution, anyway, I do it the quick-and-dirty way:

ClassMethod Lines(nsp = {$namespace})
    s (cls,sum)=""
    s glo=$na(^|"^"_##class(%SYS.Namespace).GetGlobalDest(nsp,"oddDEF")|oddDEF)
    f  {s cls=$o(@glo@(cls)),mth="" q:cls=""
        f  s mth=$o(@glo@(cls,"m",mth)) q:mth=""  s sum=sum+$g(^(mth,30))
    q sum

Not really sure if it did return the right number!? I've created the method and returned only 64003 lines, knowing that in our system we have around 3021 class.

do you know what Size filed refers to, it's not the no of lines or the file size?


Hm, according to the code, it did only go through 212 classes how come!?

The count is totally correct as you asked for all lines of codes 
you get counted all executable code.
you do not count properties, comments, whatever .....

It might be easier to do an export in UDL format and let some
Notepad or similar count the lines without any qualification.

BTW. do get paid by lines ?  

beside this, is there a way maybe in system management portal where you can check the number of code lines in each class, because reading the docs on how use Objectscript_Q is going to take me a long tine to figure and set it up in our system?


No reasons at all have it inside of InterSystems out of the box.

You should have your source code in source control system, so, your code should exist as files, then you may simply count just lines in files

find src -type f | xargs wc -l

Seems like a pointless exercise to me. A question to ask is what is the objective for this?

There is a quick way to do this but it depends on how accurate you want the count to be. In the Management Portal, under System Explorer, Classes there is the option to export classes to an external XML file; there is also the option to include/exclude system classes and select individual or all classes.

This will export the classes into an XML file which you can open in a text file editor to get the number of lines. Note though, the XML export adds additional lines for tags to differentiate the code sections so the count will be greater than the actual lines shown when actually editing the code in the usual editor. 

But it could be an option for you.

Hi Rochdi,

If you have a look at class ompare.SourceHandler.Class, in Open Exchange app

It has a range of functionality for decomposing classes into their different components.

Extracting functional lines of source code but discarding comments and empty lines.

It could be enhanced to add a counter to method lines extracted in the profile class.

The schedule also happily jumps namespaces to build report information for multiple namespaces in an environment.

Kind regards,