Cyclomatic Complexity measures how many independent paths exist through your function. It starts at 1 and increments for each decision point:
if, elif, for, while, case, catch&&, ||?: expressionsHigh complexity (>10) often indicates code that is hard to test and maintain. This tool:
def, JS function, ES6 arrow functions, and simple class methods.Example (JS):
function foo(x) {
if (x > 0 && x < 10) {
return x % 2 === 0 ? 'even' : 'odd';
}
return null;
}
Complexity = 1 (base) + 1 (if) + 1 (&&) + 1 (ternary) = 4
Use this tool to pinpoint the most complex functions, guide refactoring, and keep your codebase maintainable.