## Cyclomatic Complexity, Perceived Complexity, and ABC Metric

The story has three parts:

## Goal

In Part Two: Calculations, we have seen a couple of examples to understand the metrics calculations. In this part, we will analyze the ** interdependence **of the metrics and figuring out the maximum values for the RuboCop analysis.

## Cyclomatic Complexity and Perceived Complexity

The cyclomatic and perceived complexity values are determined by ** if**,

**,**

*and***,**

*or***,**

*case***,**

*when***,**

*for***,**

*while***, and,**

*until***nodes:**

*rescue*- The
construct with*if…elsif…else*elsif branches, then, the contribution to*n*is*cyclomatic complexity*,*n + 1*andis*perceived complexity*because an else branch contributes to perceived complexity score.*n + 2* - Each of the
,*and*,*or*,*for*,*while*, and,*until*nodes, the contributions to cyclomatic and perceived complexity values are the same.*rescue* - The
construct has*case…when…else*when branches, then, the contribution to*n*is*cyclomatic complexity*,*n*andis*perceived complexity*.*0.8 + 0.2 * n*

It is not possible to predict perceived complexity value given the cyclomatic complexity value, but we can derive an equation.

- Let
be the total number of*E*branches for*else*only.*if* - Let
be the total number of*W*branches.*when* - Let
be the total number of*C*nodes.*case* - Let
be*CC*.*cyclomatic complexity* - Let
be*PC*.*perceived complexity*

Then, *PC = CC + E − 0.8 * (W − C)*

## Proof

- Let
be the total number of*a*constructs without any elsif and else branch.*if* - Let
be the total number of*b*constructs without any else branch, and each such construct have*if…elsif*,*f1*, …,*f2*elsif branches respectively.*fb* - Let
be the total number of*c*constructs with else branch and each such construct have*if…elsif…else*,*g1*, …,*g2*elsif branches respectively.*gc* - Let
be the total number of*d*constructs and each such construct have*case…when…else*,*h1*, ….,*h2*when branches.*hd* - Let
the total number of*e*,*and*,*or*,*for*,*when*, and,*until*nodes collectively.*rescue*

Therefore,

CC = a + b + f1 + f2 + ... + fb + c + g1 + g2 + ... + gc + h1 + h2 + ... + hd + ePC = a + b + f1 + f2 + ... + fb + c + g1 + g2 + ... + gc + c + 0.8 * d + 0.2 * (h1 + h2 + ... + hd) + e=> PC - CC = c + 0.8 * d - 0.8 * (h1 + h2 + ... + hd)

=>PC = CC + c + 0.8 * d - 0.8 * (h1 + h2 + ... + hd)

Note that, ** c** is the total number of

**branches,**

*else***is the total number of**

*d***nodes, and,**

*case***is the total number of**

*h1 + h2 + … + hd***branches. So using**

*when***,**

*E***, and,**

*C***per the assumption:**

*W*`PC = CC + E + 0.8 * C - 0.8 * W`

=> *PC = CC + E - 0.8 * (W - C)*

It is easy to count the values of ** E**,

**, and,**

*C***from the code itself, so, we need not calculate the perceived complexity using the pseudocode from part two.**

*W*def hoge --CC = 1

return :foobarfooifbar --&&baz =CC = 3msg --caseC = 1:qux --when,W = 1CC = 4

qux:quux --when,W = 2CC = 5

quux

else

:baz

end plugh =corge --ifCC = 6

:corgegraultelsifgarply --&&CC = 8

:waldograult --elsifCC = 9

:graultgarply --elsifCC = 10

:garply--elseE = 1

:plugh

end quuz =xyzzy --caseC = 2:foo --when,W = 3CC = 11

foo:bar --when,W = 4CC = 12

bar:thud --when,W = 5CC = 13

thud

endbazif(plugh&&quuz) --||CC = 16

do_this--elseE = 2

do_that

end

endPC = CC + E - 0.8 * (W - C)

= 16 + 2 - 0.8 * (5 - 2)

= 16 + 2 - 2.4

= 16 - 0.4

= 15.6

## Cyclomatic Complexity and ABC Metric

The conditions component of the vector <A, B, C> is the sum of the total number of comparison operations except for the spaceship operator and cyclomatic complexity. No direct relation between cyclomatic complexity and ABC metric is possible.

Let ** a **— the

**, be the total number of**

*largest component***,**

*assignments***be the total number of**

*b***, and,**

*branches***be the total number of**

*c***For**

*conditions.*

*0 ≤***,**

*x, y ≤ a***and**

*b = a − x***. So,**

*c = a − y***. Consider a**

*a² + b² + c² = a² + (x − a)² + (y − b)²***of length**

*square***, and**

*a***on the bottom-right and top-right vertices with a radius of**

*quarter-circles***and**

*(a − x)***respectively, then the area of these shapes describes the behavior of**

*(a − y)***.**

*a² + b² + c²*