moosetechnology / Famix

An abstract representation of source code. Famix is generic and can describe applications in multiple programming languages.
MIT License
13 stars 24 forks source link

CyclomaticComplexity of Pharo importer #792

Open jecisc opened 5 months ago

jecisc commented 5 months ago

The CC of Pharo importer is currently based on some hardcoded collections of methods.

See:

computeCyclomaticNumber: sel
    "The following equations defined McCabe Cyclomatic Complexity:

         1.The number of regions in a flow graph. 
            2.V(G) = E - N + 2, where E are the edges and N are the nodes. 
            3.V(G) = P + 1, where P are the predicate nodes. 

    The score is basically the number of decision points in a routine + 1. Decision points 
    are taken to be conditionals and loops."

    | branches loops |
    branches := #(#ifTrue: #ifFalse: #ifTrue:ifFalse: #ifFalse:ifTrue: #ifNil: #ifNil:ifNotNil: #ifNotNil:ifNil: #ifNotNil: #at:ifAbsent: #at:ifAbsentPut: #detect:ifNone: #on:do: #and: #or: #xor:).
    loops := #(#whileTrue: #whileTrue #whileFalse #whileFalse: #timesRepeat: #to:do: #do:separatedBy: #do: #collect: #select: #reject: #inject:into: #detect: #detect:ifNone: #anySatisfy: #allSatisfy: #piecesCutWhere:).  "-- please not that #detect:ifNone: is listed twice because it is both loop and branch!!! -- akuhn"
    (branches includes: sel) ifTrue: [ cyclomaticNumber := cyclomaticNumber + 1 ].
    (loops includes: sel) ifTrue: [ cyclomaticNumber := cyclomaticNumber + 1 ].
    "-- HERE STARTS THE OLD ERRORFUL IMPL --"
    (#(#or: #and: #xor: #& #|) includes: sel)
        ifTrue: [ booleanOperators := booleanOperators + 1.
            ^ self ].
    (#(#ifTrue: #ifFalse: #ifTrue:ifFalse: #ifFalse:ifTrue: #whileTrue #whileTrue: #whileFalse #whileFalse:) includes: sel) ifTrue: [ booleanOperators := 0 ]

But this is not so nice because it's possible we add new conditional methods in Pharo like #ifPresent:ifAbsent: that was added in P9. We miss also ifEmpty: and co or detect:ifFound:...

I wonder if there could not be a better approximation by counting the number of blocks we have as argument of a message send in the AST?

NicolasAnquetil commented 5 months ago

I don't see how this can work: