cogc - Cognitive Complexity
Overview
The cogc command calculates cognitive complexity for C# code. Unlike cyclomatic complexity which measures structural complexity, cognitive complexity measures how difficult code is for humans to understand. It takes into account nesting, breaks in linear flow, and other factors that affect readability.
Syntax
nmbl cogc <ProjectFileOrDirectory>Arguments
ProjectFileOrDirectory- Path to a .csproj file or directory containing C# files
Output
Displays cognitive complexity scores for methods in your code, helping you identify methods that are difficult to understand and maintain.
Examples
Analyze a Specific Project
nmbl cogc src/MyProject/MyProject.csprojCalculates cognitive complexity for all methods in the project.
Analyze a Directory
nmbl cogc src/Services/Analyzes all C# files in the Services directory.
Compare with Cyclomatic Complexity
# First check cyclomatic complexity
nmbl cc src/MyProject/MyProject.csproj
# Then check cognitive complexity
nmbl cogc src/MyProject/MyProject.csprojComparing both metrics gives you a complete picture of code complexity.
Understanding Cognitive Complexity
Cognitive complexity differs from cyclomatic complexity by:
- Penalizing nesting - Deeply nested code is harder to understand
- Ignoring structures that don’t affect readability - Simple switches don’t increase complexity
- Accounting for breaks in linear flow - Jumps and recursion increase complexity
Complexity Guidelines:
- 0-5: Very easy to understand
- 6-10: Easy to understand
- 11-20: Moderate difficulty
- 21+: Difficult to understand, should be refactored