A Visual Studio Code extension that calculates and displays Cognitive Complexity metrics based on SonarSource's Cognitive Complexity specification.
- Real-time Analysis: Analyzes code metrics as you write code
- CodeLens Integration: Shows complexity scores directly above functions
- Color-coded Indicators: Visual feedback with green/yellow/red status based on configurable thresholds
- Multi-language Support: Currently supports C# with more languages planned
- Configurable Thresholds: Customize warning and error complexity thresholds
- Smart Exclusions: Automatically excludes test files, build artifacts, and other specified patterns
Cognitive Complexity is a metric that measures how difficult code is to understand. Unlike Cyclomatic Complexity, it considers the cognitive burden of code constructs:
- Linear code: No complexity increase
- Nested structures: Higher complexity for deeply nested code
- Breaks in control flow: Additional complexity for jumps and returns
- Recursive calls: Extra complexity penalty
- Visual Studio Code 1.103.0 or higher
- Supported language files for analysis
This extension contributes the following settings:
codeMetrics.enabled: Enable or disable the code metrics extension (default:true)codeMetrics.showCodeLens: Show code metrics information as CodeLens above functions (default:true)codeMetrics.warningThreshold: Metrics threshold for showing warning status with yellow indicator (default:10)codeMetrics.errorThreshold: Metrics threshold for showing error status with red indicator (default:15)codeMetrics.excludePatterns: Glob patterns for files to exclude from metrics analysis (default: excludes node_modules, dist, build, out, minified files, and test files)
Install from the VS Code Extension Marketplace or search for "code-metrics" in the Extensions view.
- Open a supported language file in VS Code
- The extension will automatically analyze code metrics
- Complexity scores appear as CodeLens above each function
- Color coding indicates complexity level:
- Green: Below warning threshold (good)
- Yellow: Above warning threshold (review recommended)
- Red: Above error threshold (refactoring recommended)
- Node.js 22.x
- npm 10.9.x or higher
# Clone the repository git clone https://github.com/askpt/code-metrics.git cd code-metrics # Install dependencies npm install # Compile TypeScript npm run compile # Run linter npm run lint # Run tests (requires VS Code) npm testThis project is configured to work with GitHub Codespaces:
- Click the "Code" button on the GitHub repository
- Select "Codespaces" tab
- Click "Create codespace on main"
The development environment will be automatically configured with Node.js 22.x and all necessary dependencies.
npm run compile: Compile TypeScript to JavaScriptnpm run watch: Auto-compile on file changesnpm run lint: Run ESLintnpm test: Run extension tests
We welcome contributions! Please see CONTRIBUTING.md for guidelines.
This project is licensed under the MIT License - see the LICENSE file for details.