Maintainability Index
A composite score from 0–100 that combines Halstead Volume, Cyclomatic Complexity, and LOC.
Formula
Where HV = Halstead Volume, CC = Cyclomatic Complexity, LOC = lines of code.
What it measures
The Maintainability Index (MI) provides a single numeric estimate of how easy a function is to maintain. Higher values (→100) indicate clean, simple code; lower values (→0) indicate complex, difficult-to-maintain code.
MI was originally proposed by Oman and Hagemeister (1991) and later adopted by Microsoft as the Visual Studio variant used here — normalized to the 0–100 range.
How it's calculated in tsmetrics
- Compute Halstead Volume for the function (from the AST)
- Compute Cyclomatic Complexity for the function
- Count
LOC(lines spanned by the function node, including blank/comment lines) - Apply the formula above
- Clamp result to [0, 100]
Nested functions are treated as separate units (consistent with Halstead and CC).
TypeScript example
// Simple function — high MI function add(a: number, b: number): number { return a + b; } // HV ≈ 16, CC = 1, LOC = 3 // MI_raw ≈ 171 - 5.2×ln(16) - 0.23×1 - 16.2×ln(3) // ≈ 171 - 14.4 - 0.23 - 17.8 ≈ 138.6 // MI ≈ (138.6/171)×100 ≈ 81 // Complex function — lower MI (many branches, long body) function processData(data: number[]): number { let result = 0; for (let i = 0; i < data.length; i++) { if (data[i] > 0) { result += data[i] * 2; } else if (data[i] < -10) { result -= data[i]; } } return result; }
Interpreting values
| MI range | Interpretation |
|---|---|
| 85–100 | Highly maintainable |
| 65–84 | Moderately maintainable |
| 40–64 | Difficult to maintain |
| 0–39 | Very difficult to maintain — refactoring recommended |
Thresholds
MI has no configurable violation thresholds. It is included in JSON output and used as input to Technical Debt.
References
- Oman, P. & Hagemeister, J. (1991). Metrics for assessing a software system's maintainability. Proc. ICSM.
- Coleman, D. et al. (1994). Using metrics to evaluate software system maintainability. IEEE Computer.
Related metrics
Halstead Volume · Cyclomatic Complexity · Technical Debt
Edit on GitHub