Skip to main content

cyclomatic_complexity

Function cyclomatic_complexity 

Source
pub fn cyclomatic_complexity(edges: i64, nodes: i64) -> i64
Expand description

McCabe cyclomatic complexity: independent paths through control flow.

edges - nodes + 2, computed on the function’s control-flow graph. A straight-line function with no branches scores 1 (the minimum); each additional decision point (if, loop, case arm, and similar) adds one.

§Arguments

  • edges — number of edges in the control-flow graph.
  • nodes — number of nodes in the control-flow graph.

§Returns

The cyclomatic complexity score (an integer; can be negative for a malformed or disconnected graph, which the caller should treat as invalid input).

§Examples

use software_engineering::code_complexity::cyclomatic_complexity;

// Straight-line function: 2 nodes, 1 edge -> complexity 1.
assert_eq!(cyclomatic_complexity(1, 2), 1);