← Back to Gene Catalog

code-complexity

Native code.analyze

Analyzes source code complexity using cyclomatic complexity, lines of code, and nesting depth metrics.

README

# code-complexity

A Native Gene that measures source code complexity using industry-standard metrics.

## Usage

```bash
rotifer test code-complexity --input '{
"code": "function example(x) {\n if (x > 0) {\n for (let i = 0; i < x; i++) {\n if (i % 2 === 0) console.log(i);\n }\n }\n}",
"language": "javascript",
"threshold": 10
}'
```

## Metrics

| Metric | Description |
|--------|-------------|
| **Cyclomatic Complexity** | McCabe's complexity — number of independent paths through code |
| **Lines of Code** | Non-blank, non-comment lines |
| **Max Nesting Depth** | Deepest level of nested control structures |
| **Function Count** | Number of functions/methods |

## Rating Scale

| Score | Rating | Meaning |
|-------|--------|---------|
| 1-5 | Low | Simple, easy to maintain |
| 6-10 | Moderate | Manageable complexity |
| 11-20 | High | Consider refactoring |
| 21+ | Very High | Refactoring strongly recommended |

## Supported Languages

TypeScript, JavaScript, Python, Rust, Go

Phenotype

Input

PropertyType Req Description
code string Source code to analyze
language typescript | javascript | python | rust | go Programming language
threshold number = 10 Complexity threshold for warnings

Output

PropertyType Description
rating low | moderate | high | very-high Complexity rating
linesOfCode number Non-blank, non-comment lines
suggestions array Improvement suggestions if complexity is high
functionCount number Number of functions/methods found
maxNestingDepth number Maximum nesting depth of control structures
cyclomaticComplexity number McCabe cyclomatic complexity score
Raw JSON Schema

inputSchema

{
  "type": "object",
  "required": [
    "code",
    "language"
  ],
  "properties": {
    "code": {
      "type": "string",
      "description": "Source code to analyze"
    },
    "language": {
      "enum": [
        "typescript",
        "javascript",
        "python",
        "rust",
        "go"
      ],
      "type": "string",
      "description": "Programming language"
    },
    "threshold": {
      "type": "number",
      "default": 10,
      "description": "Complexity threshold for warnings"
    }
  }
}

outputSchema

{
  "type": "object",
  "properties": {
    "rating": {
      "enum": [
        "low",
        "moderate",
        "high",
        "very-high"
      ],
      "type": "string",
      "description": "Complexity rating"
    },
    "linesOfCode": {
      "type": "number",
      "description": "Non-blank, non-comment lines"
    },
    "suggestions": {
      "type": "array",
      "items": {
        "type": "string"
      },
      "description": "Improvement suggestions if complexity is high"
    },
    "functionCount": {
      "type": "number",
      "description": "Number of functions/methods found"
    },
    "maxNestingDepth": {
      "type": "number",
      "description": "Maximum nesting depth of control structures"
    },
    "cyclomaticComplexity": {
      "type": "number",
      "description": "McCabe cyclomatic complexity score"
    }
  }
}

Arena History

Date Fitness Safety Calls
Mar 17 0.8790 0.88 1