Maintainability Index Calculator (Halstead, Cyclomatic Complexity, Lines of Code)
The maintainability index of a function, class or file from its Halstead volume, cyclomatic complexity and lines of code, on the original scale and on the 0–100 scale used by Visual Studio.
Results
The dashed line marks the value you entered.
What this tool does
The maintainability index is a single number built from three measurements: MI = 171 − 5.2 ln(V) − 0.23 G − 16.2 ln(LOC), from Oman and Hagemeister's work in the 1990s. The logarithms mean that doubling the lines of code costs about 11 points, while each extra decision path costs a small amount. Visual Studio rescales it to 0–100, flooring at zero, and shows green from 20, yellow from 10 and red below 10. It is a rough guide, useful for spotting the worst outliers in a code base, and it says nothing about whether the code is correct or well designed.
Formula
MI = 171 − 5.2 ln V − 0.23 G − 16.2 ln LOC · Visual Studio index = max(0, MI × 100 ÷ 171) (V = Halstead volume, G = cyclomatic complexity)
Variables
| Symbol | Meaning | Unit |
|---|---|---|
v | Halstead volume of the code | — |
cx | Cyclomatic complexity | — |
loc | Lines of code | — |
MI | Maintainability index (original scale) | — |
VS | Maintainability index (0–100, as in Visual Studio) | — |
RT | Rating (Visual Studio bands) | — |
Worked example
- Halstead volume of the code1500
- Cyclomatic complexity12
- Lines of code120
- Maintainability index (original scale)52.7
- Maintainability index (0–100, as in Visual Studio)31
- Rating (Visual Studio bands)Good maintainability (20–100)
Limitations
- The result is an estimate based only on the values you type. Real situations often include factors this calculator does not know about.
- The default values are typical reference figures, not measurements of your situation. Replace them with your own data whenever you have it.