Supported Metrics Suite
Relevant source files
The following files were used as context for generating this wiki page:
rust-code-analysis provides a comprehensive suite of static code metrics designed to analyze and extract maintainability, complexity, and size information from source code across multiple programming languages. These metrics are calculated by traversing an Abstract Syntax Tree (AST) generated by Tree Sitter, allowing for granular analysis at the file, class, or function level.
The metrics suite serves as a tool for developers to identify complex code segments, compare quality across different languages, and discover potential issues early in the development lifecycle.
Sources: rust-code-analysis-book/src/metrics.md, rust-code-analysis-book/src/commands/README.md
Core Metric Categories
The suite is categorized into size metrics, complexity metrics, and specialized object-oriented or maintainability indices.
Size and Line Count Metrics (LoC)
Size metrics track the physical and logical dimensions of the source code. The project implements a robust Loc trait to handle language-specific syntax for comments and statements.
| Metric | Name | Description |
|---|---|---|
| SLOC | Source Lines of Code | Total number of lines in a source file/scope. |
| PLOC | Physical Lines of Code | Number of instruction lines (excluding blanks and comments). |
| LLOC | Logical Lines of Code | Number of logical statements. |
| CLOC | Comment Lines of Code | Number of lines containing comments. |
| BLANK | Blank Lines | Number of empty lines in a source file. |
Sources: src/metrics/loc.rs:253-294, rust-code-analysis-book/src/metrics.md
Maintainability and Complexity Metrics
These metrics measure how difficult a piece of code is to understand, maintain, or implement.
- Cyclomatic Complexity (CC): Examines the control flow of a program to calculate complexity.
- Cognitive Complexity: Measures how hard it is for a human to understand a unit of code.
- Maintainability Index (MI): A composite metric evaluating software maintainability using original, SEI, and Visual Studio derivative formulas.
- Halstead Suite: Estimates effort, size, difficulty, bugs, and time required for implementation.
Sources: src/metrics/mi.rs:46-77, rust-code-analysis-book/src/metrics.md
Function and Object-Oriented Metrics
Specific metrics target function-level data and class-based structures (interfaces/classes).
- NOM (Number of Methods): Counts functions and closures.
- NARGS: Counts the number of arguments per function/method.
- NEXITS: Counts the possible exit points from a function.
- WMC (Weighted Methods per Class): Sums the Cyclomatic Complexity of all methods defined in a class.
- NPM / NPA: Counts public methods and public attributes in classes or interfaces.
Sources: src/metrics/nom.rs:188-202, src/metrics/wmc.rs:17-25, src/metrics/npm.rs:14-25
Architectural Logic and Data Flow
The metrics system relies on a trait-based architecture where each metric defines a compute method. This allows the system to apply different logic based on the programming language being analyzed.
Metric Calculation Flow
The following diagram illustrates how metrics are aggregated from AST nodes into a consolidated Stats structure.
The system differentiates between "Unit" spaces (files) and "Function/Class" spaces. Metrics are computed locally for a node and then merged into the parent's statistics.
Sources: src/metrics/loc.rs:242-251, src/metrics/wmc.rs:60-75, src/output/dump_metrics.rs:94-110
Maintainability Index (MI) Calculation
The MI metric is unique because it is derived from other metrics (Loc, Cyclomatic, and Halstead).
MI Formula Implementation (Original):
171.0 - 5.2 * (self.halstead_volume).ln() - 0.23 * self.cyclomatic - 16.2 * self.sloc.ln()
Sources: src/metrics/mi.rs:55-58, src/metrics/mi.rs:81-93
Data Structures
NOM Stats Structure
The nom::Stats structure tracks both function and closure counts, maintaining minimum, maximum, and average values across spaces.
pub struct Stats {
functions: usize,
closures: usize,
functions_sum: usize,
closures_sum: usize,
functions_min: usize,
functions_max: usize,
closures_min: usize,
closures_max: usize,
space_count: usize,
}
Sources: src/metrics/nom.rs:11-21
WMC Stats Structure
The Wmc (Weighted Methods per Class) metric focuses on object-oriented structures, accumulating complexity based on the SpaceKind.
| Field | Type | Description |
|---|---|---|
class_wmc_sum | f64 | Cumulative CC for all methods in classes. |
interface_wmc_sum | f64 | Cumulative CC for all methods in interfaces. |
space_kind | SpaceKind | Identifies if the current context is a Class, Interface, or Function. |
Sources: src/metrics/wmc.rs:17-25, src/metrics/wmc.rs:60-75
Metric Output and Visualization
Metrics can be dumped to the console in a tree-like structure or exported to formats like JSON, TOML, YAML, or CBOR.
Output Sequence
The visualization logic traverses the FuncSpace hierarchy to print metrics for each discovered code block.
Example JSON Output (MI Metric)
{
"mi_original": 151.2033158832232,
"mi_sei": 142.64306171748976,
"mi_visual_studio": 88.42299174457497
}
Sources: src/metrics/mi.rs:120-124
Summary
The Supported Metrics Suite in rust-code-analysis provides a technical foundation for multi-language code quality assessment. By utilizing specialized traits for different metric types (LoC, NOM, WMC, MI), the project ensures that language-specific nuances (like Java's public method modifiers or Python's docstrings) are correctly handled during the static analysis process.