Article
Jose Ruperez · Oct 12, 2016 1m read

Lines of Code

With a routine like this one, you can quickly calculate how many lines of code you are working with. And it is not only for routines, it works for classes because remember that classes generate routines !

Here you have the routine source code:

LinesOfCode ;
    new SQLCODE,tRoutine
    set tTotalLOC = 0
    &sql(DECLARE ROUTINES CURSOR FOR
     SELECT NAME 
FROM %Library.RoutineIndex 
WHERE TYPE = 'MAC'
ORDER BY NAME)
    &sql(OPEN ROUTINES)
    &sql(FETCH ROUTINES INTO :tRoutine)
    while (SQLCODE=0)
    {
        write !,$j($i(tCount),5),": ",tRoutine
        set tLOC = $get(^rMAC(tRoutine,0,0))
        write "("_tLOC_")"
        set tTotalLOC = tTotalLOC + tLOC
        &sql(FETCH ROUTINES INTO :tRoutine)
    }
    &sql(CLOSE ROUTINES)
    write !!,"Total lines of code = "_tTotalLOC

This is the output in a terminal:

USER>do ^LinesOfCode

    1: %ZJRNPURGE(5)
    2: LinesOfCode(20)

Total lines of code = 25
USER>

 

50
4 0 4 580

Replies

With lines being very flexible in COS (you can put pretty much everything into one line), wouldn't size be a more useful metric? Or you could also output an average like size/#lines ?

I think comments within the code counts a line, can you introduce a change that ignores comments/blank lines

Comments are included at varying levels as they compile down.

This is due to it reviewing Macro Routines ("MAC"). If you were to review Intermediate Routines ("INT"), you would rule out some; however, there are still comments that are included within INT routines.