Metrics – MB ESQL Cyclomatic complexity

MB ESQL Cyclomatic complexity
Cyclomatic complexity is a measure of the number of paths through a section of code.

The higher a piece of codes complexity the more difficult it is to understand, maintain, test and usually limits reuse.

For the following piece of code (that has a complexity score of 1), we have broken down how that score has been calculated.

CREATE COMPUTE MODULE GenericRule_AnonymousFieldRefChecks

   CREATE FUNCTION Main() RETURNS BOOLEAN
   BEGIN
     DECLARE inRef REFERENCE TO InputRoot.XMLNSC.Item; (1) – all routines have a score of one

     – R232 rule: Anonymous field reference tests
     MOVE itemRef TO inRef.ItemWrapper.Item.*[>3];

     RETURN TRUE;
   END;
END MODULE;

For the following piece of code (that has a complexity score of 7), we have broken down how that score has been calculated.

CREATE COMPUTE MODULE GenericRule_MultipleIFs
    CREATE FUNCTION Main() RETURNS BOOLEAN
    BEGIN
        DECLARE I INTEGER; (1)

        DECLARE size CHARACTER;

        IF I = 0 THEN (2)
            SET size = ‘small’;
        ELSE (3)
            IF I = 2 THEN (4)
                SET size = ‘medium’;
            ELSE (5)
                IF I = 3 THEN (6)
                    SET size = ‘large’;
                ELSE (7)
                    SET size = ‘unknown’;
                END IF;
            END IF;
        END IF;

        RETURN TRUE;
    END;
END MODULE;

For the following piece of code (that has a complexity score of 9), we have broken down how that score has been calculated.

CREATE FUNCTION unreachableLeaveContinue () RETURNS BOOLEAN
BEGIN
   DECLARE i INTEGER; (1)
   SET i = 0;
   X : REPEAT (2)
     SET i = i + 1;

     ITERATE X; (3)

     IF i IN(2, 3) THEN (4)
       ITERATE X; (5)
     END IF;

   UNTIL
     i >= 4
   END REPEAT X;

   X : REPEAT (6)
     SET i = i + 1;

     LEAVE X; (7)

     IF i IN(2, 3) THEN (8)
       LEAVE X; (9)
     END IF;

   UNTIL
     i >= 4
   END REPEAT X;
END;