Skip to main content

Module complexity

Module complexity 

Source
Expand description

Cyclomatic Complexity Calculation from Branch Counts

This module calculates cyclomatic complexity using the Z-coordinate (branch_count) from the 3D spatial representation of CFG blocks.

§Formula

Cyclomatic Complexity M = E - N + 2P where:

  • E = number of edges
  • N = number of nodes
  • P = number of connected components (usually 1 for a function)

Using spatial coordinates: M = 1 + sum(branch_count) for all blocks

§Complexity

  • Time: O(n) single pass through blocks
  • Space: O(1) constant space

§Example

use geographdb_core::algorithms::complexity::{calculate_cyclomatic_complexity, ComplexityBlock};

let blocks = vec![
    ComplexityBlock { id: 0, z: 0.0 },  // Entry (no branches)
    ComplexityBlock { id: 1, z: 1.0 },  // If statement (1 branch)
    ComplexityBlock { id: 2, z: 0.0 },  // Sequential
    ComplexityBlock { id: 3, z: 1.0 },  // Another if (1 branch)
];

let result = calculate_cyclomatic_complexity(&blocks);
assert_eq!(result.cyclomatic_complexity, 3);  // 1 + 1 + 1 = 3

Structs§

BlockComplexity
Complexity metrics for a single block
ComplexityBlock
A block for complexity calculation
ComplexityResult
Result of complexity analysis

Enums§

ComplexityRating
Complexity rating based on McCabe number

Functions§

calculate_cyclomatic_complexity
Calculate cyclomatic complexity from CFG blocks
calculate_per_block_complexity
Calculate complexity for each block individually
find_most_complex_blocks
Identify the most complex blocks (top N by branch count)
get_complexity_trend
Get complexity trend description
is_complexity_acceptable
Check if complexity is within acceptable limits