Cyclomatic Complexity in JavaCode 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. 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 ComplexityGenerally, 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: ExamplesLet'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 PointsThere are various decision points that affect the cyclomatic complexity by 1.
A developer or programmer should write code in such a way that it reduces cyclomatic complexity. Benefits of Reducing Cyclomatic ComplexityThe following are some of the benefits of reducing the cyclomatic complexity.
Tools For Computing the Cyclomatic ComplexityThe following are the tools for the computation of cyclomatic complexity.
In the build lifecycle of the code, these tools can be incorporated. Advantages and Disadvantages of the Cyclomatic ComplexityIn this tutorial, we are going to discuss the pros and cons of cyclomatic complexity. Let's start with the advantages first. Advantages
Disadvantages
Usage of The Cyclomatic ComplexityThe following is the usage of cyclomatic complexity.
Next TopicDeprecated Meaning in Java |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India