Measuring lines of code

Previous Topic Next Topic
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view

Measuring lines of code

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 {}

You received this message because you are subscribed to the Google Groups "VA Smalltalk" group.
To view this discussion on the web visit
To post to this group, send email to [hidden email].
To unsubscribe from this group, send email to [hidden email].
For more options, visit this group at