Back to the 2025 paper

Module 3: Software Design & Metrics

20257m

Describe Cyclomatic Complexity and how it is used to measure software. What insight does it provide?

Worked SolutionAI Assisted

Cyclomatic Complexity (McCabe)

Definition

Cyclomatic complexity is a software metric that measures the number of linearly independent paths through a program's control-flow graph (CFG). It was introduced by Thomas J. McCabe in 1976 as a measure of a module's structural complexity and testability.

Computation

Given the control-flow graph (nodes = statements/decision points, edges = control flow):

V(G)=E−N+2V(G) = E - N + 2

where:

  • EE = number of edges,
  • NN = number of nodes.

Equivalent forms:
V(G)=P+1V(G) = P + 1
where PP = number of predicate/decision nodes (e.g., if, while, case, &&, ||), and
V(G)=RV(G) = R
where RR = number of regions into which the graph partitions the plane.

Example — a program with:

if (a) { ... } else { ... }     → 1 decision
while (b) { ... }               → 1 decision

P=2⇒V(G)=2+1=3P = 2 \Rightarrow V(G) = 2 + 1 = 3 (three independent paths: if-true, if-false-through-loop, and loop iterations).

What it measures / insight it provides

  1. Minimum number of test cases — V(G)V(G) equals the number of independent paths, so it gives the minimum number of test cases needed to execute every statement & branch (basis-path testing).
  2. Complexity / maintainability — high V(G)V(G) ⇒ more decision paths ⇒ harder to understand, test, and maintain; more likely to contain defects.
  3. Guidelines: modules with V(G)≤10V(G) \le 10 are considered simple and maintainable; values > 10 (some say > 15/20) flag modules that should be refactored/split or given extra testing & review.
  4. Testing focus: it identifies the structural risk areas so testers can concentrate effort where paths and branches are numerous.

Benefits

  • Objective, easily automated (computed directly from source/CFG).
  • Combines with other metrics (LOC, coupling) for quality prediction.
  • Used in safety-critical and aviation standards (e.g., DO-178B) as a threshold for module complexity.

Summary: cyclomatic complexity quantifies the logical path structure of a program — guiding how many tests to write and which modules are dangerously complex — making it one of the most widely used structural metrics.

Similar questions

Free AppAndroid App

Coursify Android App

Generate AI courses, take quick quizzes between classes, and master complex engineering concepts on the go.