Cyclomatic Complexity: Software testing & Examples

Cyclomatic Complexity

Cyclomatic complexity is a software metric that measures the complexity of a program by counting the number of independent paths through its source code. It provides insights into the potential number of test cases needed to achieve full coverage and helps identify areas of code that may be more error-prone or difficult to maintain.

Let’s consider an example code snippet to understand how cyclomatic complexity is calculated:

def calculate_grade(score):
if score >= 90:
grade = 'A'
elif score >= 80:
grade = 'B'
elif score >= 70:
grade = 'C'
elif score >= 60:
grade = 'D'
else:
grade = 'F'
return grade

In this example, we have a function called calculate_grade that takes a score as input and returns the corresponding grade based on the score.

To calculate the cyclomatic complexity, we count the number of decision points in the code, which include conditions, loops, and case statements. Each decision point increments the cyclomatic complexity by one.

In this case, we have four decision points:

  1. The if condition (score >= 90)
  2. The elif condition (score >= 80)
  3. The elif condition (score >= 70)
  4. The elif condition (score >= 60)

Additionally, we have one default path, represented by the else statement.

Therefore, the cyclomatic complexity of this code snippet is 5. This means that there are five independent paths through the code that need to be considered for testing.

By calculating the cyclomatic complexity, developers can identify areas of code that may require additional testing and review to ensure the robustness and maintainability of the software. It can also help in making informed decisions when refactoring or optimizing the code to reduce complexity and improve overall code quality.

Here are four more examples of code snippets with their corresponding cyclomatic complexity calculations:

Python Example 1:

def is_even(num):
if num % 2 == 0:
return True
else:
return False

In this case, there is one decision point (if condition), so the cyclomatic complexity is 2.

Python Example 2:

def max_of_three(a, b, c):
if a > b and a > c:
return a
elif b > a and b > c:
return b
else:
return c

In this example, there are two decision points (if and elif conditions), so the cyclomatic complexity is 3.

Python  Example 3:

def calculate_shipping(weight, is_international):
if weight <= 0:
return 0
elif weight <= 1:
if is_international:
return 10
else:
return 5
elif weight <= 5:
if is_international:
return 20
else:
return 10
else:
return 0

In this case, there are three decision points (if and elif conditions), so the cyclomatic complexity is 4.

Python Example 4:

def get_grade(score):
grade = ''
if score >= 90:
grade = 'A'
if score >= 80:
grade = 'B'
if score >= 70:
grade = 'C'
if score >= 60:
grade = 'D'
if score < 60:
grade = 'F'
return grade

In this example, there are five decision points (if conditions), so the cyclomatic complexity is 6.

By calculating the cyclomatic complexity of these examples, you can see how the number of decision points affects the complexity score. It helps in understanding the code’s structure and potential test coverage requirements, ultimately aiding in the development of robust and maintainable software.

Top online courses in Teaching & Academics

Related Posts

Leave a Reply