complexity-analyzer

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Complexity Analyzer

复杂度分析工具

Quick Start

快速开始

Analyze complexity with tools:
bash
undefined
使用以下工具分析复杂度:
bash
undefined

JavaScript

JavaScript

npx eslint --plugin complexity --rule 'complexity: [error, 10]' src/
npx eslint --plugin complexity --rule 'complexity: [error, 10]' src/

Python

Python

pip install radon radon cc src/ -a -nb
pip install radon radon cc src/ -a -nb

General

General

npx complexity-report src/
undefined
npx complexity-report src/
undefined

Instructions

操作步骤

Step 1: Calculate Cyclomatic Complexity

步骤1:计算圈复杂度

Count decision points + 1:
  • if, else, elif
  • for, while loops
  • case statements
  • && and || operators
  • ternary operators
  • catch blocks
Example:
javascript
function example(x) {  // +1 (base)
  if (x > 0) {         // +1
    return x;
  } else if (x < 0) {  // +1
    return -x;
  }
  return 0;
}
// Complexity: 3
统计决策点数量再加1:
  • if、else、elif
  • for、while循环
  • case语句
  • && 和 || 运算符
  • 三元运算符
  • catch块
示例:
javascript
function example(x) {  // +1 (base)
  if (x > 0) {         // +1
    return x;
  } else if (x < 0) {  // +1
    return -x;
  }
  return 0;
}
// Complexity: 3

Step 2: Identify Complex Functions

步骤2:识别复杂函数

ComplexityRatingAction
1-10SimpleNo action needed
11-20ModerateConsider refactoring
21-50ComplexShould refactor
50+Very ComplexMust refactor
复杂度评级操作建议
1-10简单无需操作
11-20中等考虑重构
21-50复杂应当重构
50+极复杂必须重构

Step 3: Analyze Cognitive Complexity

步骤3:分析认知复杂度

Cognitive complexity considers:
  • Nesting depth (each level adds to complexity)
  • Breaks in linear flow (if, loops, catch)
  • Recursion
javascript
// Cyclomatic: 4, Cognitive: 7
function sumOfPrimes(max) {        // +0
  let total = 0;
  for (let i = 2; i <= max; i++) { // +1 (loop)
    let isPrime = true;
    for (let j = 2; j < i; j++) {  // +2 (nested loop)
      if (i % j === 0) {           // +3 (nested if)
        isPrime = false;
        break;                     // +1 (break)
      }
    }
    if (isPrime) {                 // +1 (if)
      total += i;
    }
  }
  return total;
}
认知复杂度考量以下因素:
  • 嵌套深度(每一层都会增加复杂度)
  • 线性流程中断(if、循环、catch)
  • 递归
javascript
// Cyclomatic: 4, Cognitive: 7
function sumOfPrimes(max) {        // +0
  let total = 0;
  for (let i = 2; i <= max; i++) { // +1 (loop)
    let isPrime = true;
    for (let j = 2; j < i; j++) {  // +2 (nested loop)
      if (i % j === 0) {           // +3 (nested if)
        isPrime = false;
        break;                     // +1 (break)
      }
    }
    if (isPrime) {                 // +1 (if)
      total += i;
    }
  }
  return total;
}

Step 4: Generate Report

步骤4:生成报告

markdown
undefined
markdown
undefined

Complexity Report

复杂度报告

High Complexity (>20)

高复杂度(>20)

processOrder() - src/orders.js:45
  • Cyclomatic: 28
  • Cognitive: 35
  • Lines: 127
  • Recommendation: Extract validation, calculation, and persistence logic
validateUser() - src/auth.js:12
  • Cyclomatic: 22
  • Cognitive: 28
  • Lines: 89
  • Recommendation: Extract individual validation rules
processOrder() - src/orders.js:45
  • 圈复杂度:28
  • 认知复杂度:35
  • 代码行数:127
  • 建议:提取验证、计算和持久化逻辑
validateUser() - src/auth.js:12
  • 圈复杂度:22
  • 认知复杂度:28
  • 代码行数:89
  • 建议:提取独立的验证规则

Moderate Complexity (11-20)

中等复杂度(11-20)

[List functions with complexity 11-20]
undefined
[列出复杂度为11-20的函数]
undefined

Simplification Strategies

简化策略

Extract Method

提取方法

javascript
// Before: Complexity 15
function processData(data) {
  if (data.type === 'A') {
    // 20 lines of processing
  } else if (data.type === 'B') {
    // 20 lines of processing
  }
}

// After: Complexity 3
function processData(data) {
  if (data.type === 'A') return processTypeA(data);
  if (data.type === 'B') return processTypeB(data);
}
javascript
// Before: Complexity 15
function processData(data) {
  if (data.type === 'A') {
    // 20 lines of processing
  } else if (data.type === 'B') {
    // 20 lines of processing
  }
}

// After: Complexity 3
function processData(data) {
  if (data.type === 'A') return processTypeA(data);
  if (data.type === 'B') return processTypeB(data);
}

Replace Nested Conditionals

替换嵌套条件

javascript
// Before: Cognitive 12
function getPrice(user, product) {
  if (user) {
    if (user.isPremium) {
      if (product.onSale) {
        return product.price * 0.7;
      } else {
        return product.price * 0.9;
      }
    }
  }
  return product.price;
}

// After: Cognitive 4
function getPrice(user, product) {
  if (!user || !user.isPremium) return product.price;
  if (product.onSale) return product.price * 0.7;
  return product.price * 0.9;
}
javascript
// Before: Cognitive 12
function getPrice(user, product) {
  if (user) {
    if (user.isPremium) {
      if (product.onSale) {
        return product.price * 0.7;
      } else {
        return product.price * 0.9;
      }
    }
  }
  return product.price;
}

// After: Cognitive 4
function getPrice(user, product) {
  if (!user || !user.isPremium) return product.price;
  if (product.onSale) return product.price * 0.7;
  return product.price * 0.9;
}

Use Polymorphism

使用多态

javascript
// Before: Complexity 8
function getArea(shape) {
  if (shape.type === 'circle') {
    return Math.PI * shape.radius ** 2;
  } else if (shape.type === 'square') {
    return shape.side ** 2;
  }
  // ... more types
}

// After: Complexity 1 per class
class Circle {
  getArea() { return Math.PI * this.radius ** 2; }
}
class Square {
  getArea() { return this.side ** 2; }
}
javascript
// Before: Complexity 8
function getArea(shape) {
  if (shape.type === 'circle') {
    return Math.PI * shape.radius ** 2;
  } else if (shape.type === 'square') {
    return shape.side ** 2;
  }
  // ... more types
}

// After: Complexity 1 per class
class Circle {
  getArea() { return Math.PI * this.radius ** 2; }
}
class Square {
  getArea() { return this.side ** 2; }
}