Skip to content

cyclomatic-complexity

Limit branching complexity in declared functions.

Updated View as Markdown

Default severity: warning

Maximum: 10

Reports a declared function when its calculated complexity is greater than 10. A low complexity score makes control flow easier to understand, test, and change safely.

How the score is calculated

Every function starts at 1. Strider adds one for each:

  • if, for, range, or type-switch statement;
  • non-default case clause;
  • non-default select communication clause; and
  • && or || binary expression.

The rule currently runs on function declarations. Branches in a function literal nested inside a declaration contribute to the enclosing declaration’s score.

Bad

func route() string {
	if first() { return "first" }
	if second() { return "second" }
	if third() { return "third" }
	if fourth() { return "fourth" }
	if fifth() { return "fifth" }
	if sixth() { return "sixth" }
	if seventh() { return "seventh" }
	if eighth() { return "eighth" }
	if ninth() { return "ninth" }
	if tenth() { return "tenth" }
	return "fallback"
}

Good

func route(value any, state routeState) string {
	if text, ok := value.(string); ok {
		return routeText(text, state)
	}
	return routeOther(value, state)
}

Extract cohesive decisions into named helpers. Avoid splitting code solely to satisfy the number; each helper should represent a meaningful unit.

Configuration

The maximum cyclomatic-complexity score is fixed at 10. Configure the rule’s severity and path exclusions in strider.toml:

[checks.cyclomatic-complexity]
severity = "error"
excludes = ["generated/**"]
Navigation

Type to search…

↑↓ navigate↵ selectEsc close