Formula

# SEI / Visual Studio variant, normalized to 0–100 MI_raw = 1715.2 × ln(HV) − 0.23 × CC16.2 × ln(LOC) MI = max(0, (MI_raw / 171) × 100) Edge cases: HV = 0 → ln(HV) term treated as 0 (no volume → no penalty) LOC = 0 → treat as 1 to avoid ln(0) = −∞

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

  1. Compute Halstead Volume for the function (from the AST)
  2. Compute Cyclomatic Complexity for the function
  3. Count LOC (lines spanned by the function node, including blank/comment lines)
  4. Apply the formula above
  5. Clamp result to [0, 100]

Nested functions are treated as separate units (consistent with Halstead and CC).

TypeScript example

TypeScript
// 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 rangeInterpretation
85–100Highly maintainable
65–84Moderately maintainable
40–64Difficult to maintain
0–39Very 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

Related metrics

Halstead Volume · Cyclomatic Complexity · Technical Debt

Edit on GitHub