Measuring lines of code

38 views
Skip to first unread message

Donald MacQueen

unread,
Jun 17, 2011, 9:47:59 PM6/17/11
to va-sma...@googlegroups.com
Here is a hack I whipped up at JWARS to pacify management when they asked how many lines of code were in the application.

First add this method to CmMethodLOC>>measureLoc: method

    | source sourceNode |

    source := self cachedSourceFor: method.
    (sourceNode := self cachedParseTreeFor: method) isNil ifTrue: [^nil].

    sourceNode statements isEmpty ifTrue: [^0].

    ^(self defaultMeasurementOf: method result: (self linesOfCodeIn: source using: sourceNode))
        results first

Then execute this script in a workspace:


| apps classes loc methods names instVars bm cml mm mi ml maxInst |

names := #('EsLogging' 'CLDT').  "<---------------------------- put your application prefixes here"

apps := System loadedApplications. instVars := loc := maxInst := 0.
maxMeth := 0. cml := CmMethodLOC new. bm := Set new.

apps := apps select: [:each ||cnt| cnt := 0. names do:
                [:each2 | cnt := cnt + (each name asString indexOfSubCollection: each2 startingAt: 1 ifAbsent: [0])].
                cnt > 0].
Transcript show: 'Apps ', apps size printString;cr.
classes := OrderedCollection new. methods := OrderedCollection new.
apps do: [:each | classes addAll: each allLocalClasses].
Transcript show: 'Classes ', classes size printString;cr.
maxCnt := 0.
classes do: [:each ||localMeth| instVars := instVars + each instSize.
    each instSize > maxInst ifTrue: [maxInst := each instSize. instCl := each name].
    localMeth := (each methodsArray select: [:e | e notNil]).
    localMeth := localMeth select: [:e | (e class = CompiledMethod)].
    localMeth size > maxMeth ifTrue: [maxMeth := localMeth size. methCl := each class name].
    locCnt := 0.
    localMeth do: [:e | locCnt := locCnt + (cml measureLoc: e)].
    locCnt > maxCnt ifTrue: [maxCnt := locCnt. maxCl := each class name].
    loc := loc + locCnt.
    methods addAll: localMeth.
        ].

Transcript show: 'Total number of methods ', methods size printString;cr. 
"methods do: [:each | loc := loc + (cml measureLoc: each)]."
Transcript show: 'Total Lines of code ', loc  printString;cr.
Transcript show: 'Average Lines of code per method ', (loc / methods size asFloat) printString;cr.


-- 
Donald [|]

A bad day in [] is better than a good day in {}



Reply all
Reply to author
Forward
0 new messages