Search code examples
pseudocodecyclomatic-complexity

Calculating Cyclomatic Complexity [Pseudocode]


The Cyclomatic Complexity of the pseudocode below is "4".

Read A
Read B
IF A > 0 THEN
     IF B  = 0 THEN
    Print “No values”
     ELSE
    Print B
    IF A > 21 THEN
        Print A
    ENDIF
     ENDIF
ENDIF

How do we count it? I heard that it's # of conditions + 1? Do we count those else statements? I'm confused.

EDIT: Case 2: What if we have:

IF (x < y)
statment 1

IF (x < z)
statemnt 2

What will be the Cyclomatic complexity? 2? or 3?


Solution

  • No. Main flow + 3 x 'If's = 4

    From the wiki:

    The cyclomatic complexity of a section of source code is the count of the number of linearly independent paths through the source code. For instance, if the source code contained no decision points such as IF statements or FOR loops, the complexity would be 1, since there is only a single path through the code.

    If the code had a single IF statement containing a single condition there would be two paths through the code, one path where the IF statement is evaluated as TRUE and one path where the IF statement is evaluated as FALSE.