Wednesday, 18 May 2022

Cyclomatic Complexity

Image Blog QAC Cyclomatic Complexity


Here, we explain what is cyclomatic complexity, cyclomatic complexity McCabe, and provide cyclomatic complexity examples.


What Is Cyclomatic Complexity?

  • It is a metric for software quality
  • Developed by Thomas J. McCade Sr.
  • Refers to the no. of linearly independent path through a program's source code.

Cyclomatic complexity (CYC) is a software metric used to determine the complexity of a program. It is a count of the number of decisions in the source code. The higher the count, the more complex the code. 

What are the effects of high Cyclomatic Complexity on your code?

The higher the number, the more complex the code.

And, the more complex the code the more likely it will:

  • Have defects
  • Be difficult to test
  • Be difficult to read
  • Be difficult to maintain
Complex code is inefficient, unreliable and low quality.

Why Is Cyclomatic Complexity Important?

Cyclomatic complexity can be used in two ways, to:

  • Limit code complexity.
  • Determine the number of test cases required.

How to Calculate Cyclomatic Complexity

Use the following formula to calculate cyclomatic complexity (CYC):
CYC = E – N + 2P

In this equation:

  • E refers number of edges or transfers of control
  • N refers number of nodes or sequential group of statements containing only one transfer of control
  • P refers number of disconnected parts of the flow graph such as a calling program or a subroutine
This translates to the number of decisions + 1.

Binary decisions — such as “if” and “while” statements — add 1 to complexity.

Boolean operators can add either one or nothing to complexity. For instance, one may be added if a Boolean operator is found within a conditional statement.


Two Cyclomatic Complexity Examples

Cyclomatic Complexity Example 1

Here's an example of CYC.

void foo(void)
{
    if (a && b)
        x=1;
    else
        x=2;
}

At first, it looks like there is one decision in this example. So, CYC = 2.

However, when you consider the side effect of the Boolean operator, there are actually two decisions.

void foo(void)
{
    if (a)
        if (b) 
            x=1;
    else
        x=2;
 }

So, CYC = 3 in this example. 

Cyclomatic Complexity Example 2

There are other variations of CYC, too.

Myers' Interval is an extension to CYC. It accounts for complexity caused by compound predicates. It uses CYC as its lower bound. The upper bound is defined as the total number of conditions in the code plus 1. It's presented as two values separated by colon :

CYC: NUMBER OF LOGICAL OPERATORS


Here's an example using Myers' Interval.

int divide (int x, int y)
{
    if (y != 0) /* Condition 1 */
    {
    return x / y;
    }
    else if (x == 0 && y > 2) /* Condition 2*Conditional expression 1 */
    {
        return 1;
    }
    else
    {
        printf (“div by zero\n”);
        return 0;
    } 

The example above has a Myers Cyclomatic Complexity value of 3:4. Its CYC is 3. And there is one connective (&&) used in the conditions. 

However, complexity is just one measure of quality.


Manually computing Cyclomatic complexity can be a time consuming process that is vulnerable to human error. However static analyser tool such as Helix QAC, the process is quick and error free.

Learn how to reduce the complexity of your code — and maintain a readable codebase — by using a static code analyzer such as Helix QAC.




Skill is Vision

Author & Editor

It is possible to fly without motors, but not without knowledge and skill. The future belongs to those who learn more skills and combine them in creative ways.

0 comments:

Post a Comment