Cyclomatic Complexity

ID

go.cyclomatic_complexity

Severity

low

Remediation Complexity

medium

Remediation Risk

low

Remediation Effort

medium

Resource

Complexity

Language

Go

Tags

complexity

Description

Reports functions and methods whose cyclomatic complexity (CCN) exceeds a configurable threshold.

CCN is the number of linearly independent paths through a function — effectively the count of decision points (if, else if, case, &&, ||, loops) plus one. It is the canonical single number for how branchy a function is.

Rationale

High CCN correlates with hard-to-test, hard-to-maintain code: more paths mean more tests to cover them, more state combinations for a reader to keep in mind, and more places for a bug to hide. Deeply nested if chains, long else if ladders, and compound boolean conditions all raise CCN, so a single threshold captures patterns that would otherwise need several rules.

The value is computed from the function’s data-flow graph as arcs - nodes + 2.

Remediation

Extract helpers, replace chains with table lookups, and simplify compound boolean conditions until the function’s CCN is below the threshold. Prefer many small, testable functions over one large one.

// Before — high CCN
func classify(code int) string {
    if code == 1 {
        return "one"
    } else if code == 2 {
        return "two"
    } else if code == 3 {
        return "three"
    }
    // ... many more ...
    return "unknown"
}

// After — low CCN
var table = map[int]string{1: "one", 2: "two", 3: "three"}

func classify(code int) string {
    if name, ok := table[code]; ok {
        return name
    }
    return "unknown"
}

Configuration

Property Default Description

maxCcn

30

Cyclomatic complexity strictly greater than this value is flagged.