Stellarion
Quality

Maintainability Index

The Maintainability Index (MI) is a composite metric that combines multiple code characteristics into a single score representing how maintainable your code is. Originally developed by Oman and Hagemeister in 1991, it provides a holistic view of code health on a 0-100 scale.

What It Measures

This metric combines Halstead Volume (information content and program size), Cyclomatic Complexity (control flow paths), and Lines of Code (physical size) into a single 0-100 score. Higher scores indicate more maintainable code.

The Formula

Stellarion uses the standard Microsoft-normalized formula:

MI = MAX(0, (171 - 5.2×ln(HV) - 0.23×CC - 16.2×ln(LOC)) × 100/171)

Components:

  • HV = Halstead Volume (information content)
  • CC = Cyclomatic Complexity (control flow paths)
  • LOC = Lines of Code (physical size)
  • ln = Natural logarithm

The formula normalizes to 0-100, with Halstead Volume weighted at 5.2×ln(HV), Cyclomatic Complexity at 0.23×CC, and Lines of Code at 16.2×ln(LOC). The physical size (LOC component) has the strongest influence on the final score.

Rating System

Stellarion classifies maintainability using these thresholds:

ScoreRatingColorMaintainability Level
80-100ExcellentGreenHighly maintainable
50-79GoodGreenGood maintainability
20-49FairYellowModerate maintainability
10-19PoorRedDifficult to maintain
0-9PoorRedVery difficult to maintain

Why It Matters

This metric serves as a leading indicator for:

Development Velocity: Code with low MI slows development. Teams spend more time understanding, debugging, and carefully modifying complex code.

Technical Debt: MI below 50 indicates accumulating technical debt. Each change becomes riskier and more expensive.

Onboarding Efficiency: New team members struggle with low-MI codebases. High MI correlates with faster onboarding and knowledge transfer.

Defect Probability: Studies show strong correlation between low MI and defect density. Poor maintainability leads to more bugs.

How to Improve Your Score

Since MI is a composite metric, improvements target its components:

1. Reduce Cyclomatic Complexity (Primary Driver)

The CC component has the most direct impact:

  • Extract complex logic into smaller functions
  • Use guard clauses and early returns
  • Replace conditionals with polymorphism or lookup tables

2. Decrease Lines of Code

Reduce function and file length:

  • Split large functions into focused units
  • Extract classes and modules by responsibility
  • Remove dead code and redundant logic

3. Simplify Halstead Volume

Reduce operator and operand variety:

  • Use consistent naming conventions
  • Simplify expressions
  • Avoid deep nesting that requires mental tracking

Improvement Targets

Stellarion recommends these refactoring targets based on current MI:

Current MITarget MIEffort Level
0-1950+High (16+ hours per file)
20-4960+Medium (4-16 hours per file)
50-7980+Low (1-4 hours per file)

Practical Example

Consider a file with:

  • Halstead Volume: 2000
  • Cyclomatic Complexity: 25
  • Lines of Code: 400
MI = MAX(0, (171 - 5.2×ln(2000) - 0.23×25 - 16.2×ln(400)) × 100/171)
MI = MAX(0, (171 - 39.5 - 5.75 - 97.1) × 0.585)
MI = MAX(0, 28.65 × 0.585)
MI = 16.8 (Poor)

After refactoring to reduce CC to 10 and splitting into smaller files (150 LOC each):

MI = MAX(0, (171 - 39.5 - 2.3 - 81.2) × 0.585)
MI = 48 × 0.585 = 28.1 (Fair)

Further improvements to reduce Halstead Volume would push MI into the Good range.


For the complete metrics reference, see Stellarion Quality Metrics.