Complexity Metrics (CC, Cognitive, WMC)
Relevant source files
The following files were used as context for generating this wiki page:
The rust-code-analysis library implements several complexity metrics to evaluate the maintainability and structural intricacy of source code. These metrics—Cyclomatic Complexity (CC), Cognitive Complexity, and Weighted Methods per Class (WMC)—provide quantitative data on how difficult a unit of code is to understand, test, or maintain.
These metrics are part of a broader suite of Supported Metrics that the tool extracts using Tree Sitter parsers. The system processes source code into an Abstract Syntax Tree (AST) and traverses it to calculate specific scores based on language-specific constructs like loops, conditionals, and logical operators.
Sources: rust-code-analysis-book/src/metrics.md, src/lib.rs:25-45
Cyclomatic Complexity (CC)
Cyclomatic Complexity measures the number of linearly independent paths through a program's source code. It is calculated by examining the control flow, specifically incrementing the complexity score whenever a decision point (like an if statement or a loop) is encountered.
Logic and Implementation
The Cyclomatic trait is implemented for various languages, defining which AST nodes trigger an increase in the complexity count. For example, in Rust, nodes such as If, For, While, Loop, and MatchArm contribute to the score.
| Component | Description |
|---|---|
cyclomatic_sum | The total sum of complexity across all analyzed spaces. |
cyclomatic_average | The sum divided by the number of spaces. |
cyclomatic_max | The highest complexity value found in a single space. |
cyclomatic_min | The lowest complexity value found in a single space. |
Sources: src/metrics/cyclomatic.rs:10-25, src/metrics/cyclomatic.rs:135-143
Decision Points by Language
Different languages have specific keywords that increment the CC score:
- Python:
If,Elif,For,While,Except,With,Assert,And,Or. - Rust:
If,For,While,Loop,MatchArm,TryExpression,&&,||. - C++:
If,For,While,Case,Catch,ConditionalExpression,&&,||.
Sources: src/metrics/cyclomatic.rs:115-175
Weighted Methods per Class (WMC)
Weighted Methods per Class is an object-oriented metric that sums the cyclomatic complexities of all methods defined within a class or interface. It provides an overview of the complexity of a class as a whole.
The diagram shows how individual method complexities are aggregated into the total class WMC score. Sources: src/metrics/wmc.rs:18-25
Data Structure
The Stats struct for WMC tracks complexity separately for classes and interfaces:
class_wmc_sum: Aggregated CC for all classes in a space.interface_wmc_sum: Aggregated CC for all interfaces in a space.total_wmc: The combined sum of class and interface complexities.
Sources: src/metrics/wmc.rs:26-34, src/metrics/wmc.rs:88-100
Cognitive Complexity
Cognitive Complexity measures how difficult a unit of code is to understand by a human reader. Unlike Cyclomatic Complexity, which counts paths, Cognitive Complexity focuses on the "mental load" required to follow the logic. It penalizes nesting and recognizes patterns that humans find difficult, such as deeply nested if statements.
Integration in Metrics Flow
Cognitive complexity is computed during the AST traversal alongside other metrics.
The flow shows that Cognitive Complexity calculation involves tracking nesting information for nodes like conditionals, functions, and lambdas. Sources: src/spaces.rs:322-355
System Architecture and Data Flow
The complexity metrics are integrated into the FuncSpace structure, which represents a logical unit of code (file, class, or function).
Space Hierarchy and Merging
When the parser moves out of a nested scope (e.g., leaving a function to return to a class), the finalize function is called. This function merges the metrics of the inner space into the outer space.
- Computation: Individual traits (
Cyclomatic,Wmc, etc.) compute values for the current node. - Aggregation: Scores are stored in
CodeMetrics. - Finalization:
finalizecomputes averages, min/max values, and merges Halstead and MI metrics.
Sources: src/spaces.rs:275-315, src/spaces.rs:340-350
Output Formatting
Metrics can be dumped to the console or exported. The dump_metrics function handles the visual representation, color-coding the output for readability.
| Metric | Field in Output | Description |
|---|---|---|
| CC | cyclomatic | Displays sum and average. |
| WMC | wmc | Displays class, interface, and total sums. |
| Cognitive | cognitive | Displays sum and average. |
Sources: src/output/dump_metrics.rs:104-125, src/output/dump_metrics.rs:252-270
Conclusion
Complexity metrics in rust-code-analysis serve as a vital tool for technical debt assessment. By quantifying the control flow (CC), mental effort (Cognitive), and object-oriented volume (WMC), the system allows developers to identify specific areas of a codebase that may require refactoring or more intensive testing. These metrics are calculated via a structured AST traversal that ensures accuracy across multiple supported programming languages.
Sources: rust-code-analysis-book/src/commands/README.md, src/lib.rs:25-35