Function & Class Metrics
Relevant source files
The following files were used as context for generating this wiki page:
Function and Class Metrics within rust-code-analysis provide quantitative measurements of source code complexity, structure, and maintainability at various granularities. By utilizing Tree-Sitter for AST traversal, the library extracts static metrics such as the number of arguments, public methods, and weighted methods per class across multiple programming languages including Java, Kotlin, Python, and Rust.
These metrics are organized within "spaces," which represent logical scopes like functions, classes, or entire units of code. This hierarchical structure allows developers to identify specific areas of a codebase that may be difficult to maintain or overly complex.
Metric Architecture and Implementation
Metrics are implemented as traits associated with language-specific checkers. Each metric typically defines a Stats structure to store accumulated data and a compute function that operates on AST nodes.
Metric Calculation Flow
The library traverses the AST and applies metric computation logic whenever a relevant node (e.g., a function definition or class body) is encountered.
The diagram shows how node-specific metrics are routed based on whether a node represents a function or a class structure.
Sources: src/spaces.rs:175-212, src/metrics/nom.rs:114-122
Summary of Key Metrics
| Metric | Code | Description | Scope |
|---|---|---|---|
| Number of Arguments | NArgs | Counts parameters in functions and closures. | Function |
| Number of Methods | Nom | Counts function and closure definitions. | Unit/Class |
| Public Methods | Npm | Counts accessible methods in classes/interfaces. | Class |
| Public Attributes | Npa | Counts accessible fields in classes/interfaces. | Class |
| Weighted Methods | Wmc | Sum of cyclomatic complexities of all methods. | Class |
| Maintainability Index | Mi | A composite score of maintainability based on LOC and Halstead. | Unit/Function |
Sources: src/metrics/nargs.rs:11, src/metrics/nom.rs:9, src/metrics/npm.rs:12, src/metrics/npa.rs:12, src/metrics/wmc.rs:14, src/metrics/mi.rs:15
Function-Level Metrics
Function metrics focus on individual units of execution, primarily measuring interface complexity (arguments) and density.
NArgs (Number of Arguments)
This metric tracks the number of arguments for both standard functions and closures. It maintains statistics for sums, averages, and min/max values across the analyzed spaces.
Sources: src/metrics/nargs.rs:15-32
Nom (Number of Methods)
Nom counts the occurrences of function definitions and closures. Unlike general declarations, only definitions are considered.
Sources: src/metrics/nom.rs:77-80
pub trait Nom where Self: Checker {
fn compute(node: &Node, stats: &mut Stats) {
if Self::is_func(node) {
stats.functions += 1;
return;
}
if Self::is_closure(node) {
stats.closures += 1;
}
}
}
Sources: src/metrics/nom.rs:114-124
Class and Interface Metrics
Object-oriented metrics analyze the accessibility and complexity of classes and interfaces, particularly for languages like Java and Kotlin.
NPM and NPA (Public Accessibility)
- NPM (Number of Public Methods): Measures the public interface of a class. In Java, methods in interfaces are implicitly public.
- NPA (Number of Public Attributes): Counts public fields.
- COA/CDA: Both metrics calculate an accessibility ratio (e.g.,
class_coa = class_npm_sum / class_nm_sum).
Sources: src/metrics/npm.rs:104-125, src/metrics/npa.rs:94-114
WMC (Weighted Methods per Class)
Wmc sums the cyclomatic complexity of every method defined within a class scope. It distinguishes between classes and interfaces, providing separate sums for each.
Sources: src/metrics/wmc.rs:17-30
The relationship between the WMC trait and its statistical container.
Sources: src/metrics/wmc.rs:17-86
Maintainability Index (MI)
The Maintainability Index is a composite metric derived from Halstead Volume, Cyclomatic Complexity, and Lines of Code (LOC). rust-code-analysis supports three variants:
- Original: The baseline formula.
- SEI: Derivative formula used by the Software Engineering Institute, incorporating comment percentage.
- Visual Studio: A normalized version (0-100) used by Microsoft tools.
Sources: src/metrics/mi.rs:56-85
Calculation Logic
The index is calculated using logarithmic scales of volume and size:
pub fn mi_original(&self) -> f64 {
171.0 - 5.2 * (self.halstead_volume).ln() - 0.23 * self.cyclomatic - 16.2 * self.sloc.ln()
}
Sources: src/metrics/mi.rs:60-63
Space Management
Metrics are aggregated through a hierarchical "Space" system. A FuncSpace contains a CodeMetrics object and a vector of child spaces.
This diagram illustrates the recursive nature of function spaces and their attached metric statistics.
Sources: src/spaces.rs:96-120, src/spaces.rs:140-153
Space Finalization
During the finalization phase, the library computes averages and min/max values by merging child spaces into parent containers.
- Averages: Divided by
space_countor total functions/closures. - Min/Max: Tracks the smallest and largest values encountered in the subtree.
Sources: src/spaces.rs:194-220, src/metrics/nom.rs:105-112
Conclusion
Function & Class Metrics serve as the core of rust-code-analysis's ability to evaluate code quality. By combining low-level counts (NArgs, Nom) with structural complexity (Wmc) and high-level maintainability scores (Mi), the system provides a multi-dimensional view of software health across various scopes and languages.