cyclomatic-complexity

Refactors branchy TypeScript and React functions using early returns, guard clauses, and extract-method tactics.

1|Updated Apr 3, 2026
One-click install
npx skills add https://github.com/TierOne-Studio/spa-velocity --skill cyclomatic-complexity-tierone-studio
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: cyclomatic-complexity
Source: https://github.com/TierOne-Studio/spa-velocity/tree/main/.ruler/skills/cyclomatic-complexity
Command: npx skills add https://github.com/TierOne-Studio/spa-velocity --skill cyclomatic-complexity-tierone-studio

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Functions with deep nesting, long if-else chains, and nested ternaries are hard to test, read, and change safely. This Skill provides concrete refactoring tactics to flatten high cyclomatic complexity in TypeScript and React code without refactoring for the metric alone. ## Core Features & Use Cases - Guard Clauses and Early Returns: Replace nested validation pyramids with flat precondition checks so the happy path stays unindented. - Tactic Library: Six concrete tactics including eliminating else after return, replacing nested ternaries, extracting named hooks, splitting boolean-flag functions, and registry-based dispatch. - Complexity Heuristics: A rough 1-4 / 5-7 / 8-10 / 11+ scoring table to decide when refactoring is warranted versus when structure is fine. - Use Case: During code review of a React gate component with four levels of nested conditionals, apply guard clauses to flatten it into a readable sequence of early returns. ## Quick Start Review this function for cyclomatic complexity and refactor it using early returns and guard clauses.

Frequently Asked Questions about cyclomatic-complexity

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I reduce cyclomatic complexity in a function?

Reduce cyclomatic complexity by replacing nested validation with guard clauses that return early, removing else after return or throw, and extracting named methods for distinct steps. Keep the happy path unindented at the bottom of the function.

How to fix nested ternaries in React JSX?

Replace nested ternaries in JSX with early returns from the component for loading, error, and empty states, or extract a named function with an if cascade. This makes the render logic readable and easier to debug and diff.

What cyclomatic complexity is too high?

Complexity of 1-4 is fine, 5-7 warrants looking for early-return wins, 8-10 should be refactored before adding more, and 11+ is hard to test and should be refactored before merging. The metric is a smell, not a hard rule.

When should I not refactor for cyclomatic complexity?

Skip refactoring when code is genuinely linear, has one simple if/else, follows framework-imposed structure like single-path route handlers, or is configuration-style data. Never split a clear function just to lower the metric.

Should I split a function that takes a boolean flag?

Yes, when the boolean branches the entire function body, split it into two separately named functions so callers pick an explicit intent. Keep the flag only when it toggles a small detail like a debug option.