Cyclomatic Complexity in Java

Code management in Java is a concept that one may be familiar with. It deals with how to organize one's source code such that dealing with it during maintenance can be somewhat more straightforward. Apart from other time complexities, this cyclomatic complexity is computed on the basis of the control flow of the program.

For example: If a program has no control statement, then the cyclomatic complexity of the program is considered to be 1.

FileName: CyclomaticComplexity.java

Output:

Inside the main method.

Analysis: The program contains only one print statement and no control statements or decision-making statements, making the cyclomatic complexity of the program as 1.

Formula for Cyclomatic Complexity

Generally, the formula for computing the cyclomatic complexity is given as:

So, on the basis of the above formula, we can also find the cyclomatic complexity of the program written above. The total number of decision points in the above-written program is zero. Note that the print statement is not a decision-making statement. Thus, as per the formula:

Examples

Let's see various examples and compute the cyclomatic complexity for it.

Example - 1

FileName: CyclomaticComplexity1.java

Output:

Inside the foo method.

Analysis: The program has one decision point which is the if condition, thus applying the formula of the cyclomatic complexity we get:

Cyclomatic Complexity = 1 + 1 = 2. 2 means there are two control flows. One goes via the body of if-statement and other one goes via the else block.

Example - 2

FileName: CyclomaticComplexity2.java

Output:

Inside the first if-condition.
Inside the foo method.

Analysis: In the above case, the number of decision points is 3. Thus, as per the formula, we get the cyclomatic complexity as follows:

Cyclomatic Complexity = 3 + 1 = 4. 4 means there are four control flows. The first one goes via the body of the first if-statement and then the body of the second if-statement. The second flow goes through the body of the first if-statement, and then the else block. The third flow goes through the body of the second if-statement, and the fourth one goes through the else block.

Example - 3

FileName: CyclomaticComplexity3.java

Output:

Inside the first if-condition.
Inside the foo method.

Analysis: In the above case, the cyclomatic complexity can be computed as the total number of decision points plus a total number of logical operators plus 1. There is only one decision point as there is only one if-condition. Also, there are three logical operators two ||, and one &&. Thus, cyclomatic complexity can be computed as:

Cyclomatic Complexity = 1 + 3 + 1 = 5, which means there are five control flows. One is when the n.equals(n1) is true, and the body of the if-condition executes. The second one is when n.equals(n1) is false, and n.equals(n2) is true, and the body of the if-condition executes. The third one is when n.equals(n1) is false, and n.equals(n2) is also false, and the logical && gives a true value, and the body of the if-condition executes. The fourth one is when the n.equals(n1) is false, and n.equals(n2) is also false, and the first operand of the && operator is also false, and the body of the if-condition does not execute. The final and the fifth one is when the n.equals(n1) is false, and n.equals(n2) is also false, and the first operand of the && operator is true, but the second operator is false, and the body of the if-condition does not execute.

Various Decision Points

There are various decision points that affect the cyclomatic complexity by 1.

  • || and &&
  • for, do, while
  • ?: (ternary operator), if-else
  • Switch, case statements, catch.

A developer or programmer should write code in such a way that it reduces cyclomatic complexity.

Benefits of Reducing Cyclomatic Complexity

The following are some of the benefits of reducing the cyclomatic complexity.

  • The coupling of code gets reduced. The higher the value of cyclomatic complexity, the larger the coupling of the code. The demerit of the highly coupled code is that it is not easy to modify.
  • When the cyclomatic complexity increases, the difficulty of comprehending the code also increases. It is because the number of control paths also increases, which can lead to more unexpected results.
  • Higher cyclomatic complexity means there are higher cases that need to be tested. For example, if the cyclomatic complexity of a method is 12, then it means there are 12 independent paths through that method, and all those 12 paths need to be explored. Therefore, lowering the cyclomatic complexity means lowering the test cases making the testing of the software or program easy.

Tools For Computing the Cyclomatic Complexity

The following are the tools for the computation of cyclomatic complexity.

  • Findbugs
  • Checkstyle
  • Sonar
  • Cobertura
  • Jarchitect

In the build lifecycle of the code, these tools can be incorporated.

Advantages and Disadvantages of the Cyclomatic Complexity

In this tutorial, we are going to discuss the pros and cons of cyclomatic complexity. Let's start with the advantages first.

Advantages

  • Cyclomatic complexity can be utilized as the quality metric, given the relative complexities of the different designs.
  • It can also be utilized as the measurement of the minimum effort and the appropriate areas to focus on for the testing.
  • Easy to apply
  • Providing guidance for testing purposes

Disadvantages

  • Cyclomatic complexity only gives information about the control flow complexity and not about the time-related complexity.
  • In this complexity, conditions are harder to decipher if they are nested as compared to the non-nested conditions.
  • Even in the case of simple decision structures and comparisons, it may give a figure that is misleading.

Usage of The Cyclomatic Complexity

The following is the usage of cyclomatic complexity.

  • Cyclomatic Complexity determines the number of independent path executions that are helpful for the testers and developers.
  • It ensures that every path is verified at least once.
  • It helps to focus on the paths that are uncovered.
  • Evaluation of the risks that are associated with the program.
  • Improvement in the code coverage.