# 可维护性指数

可维护性指数（maintainability index，MI）是一种复合软件度量，它把规模、复杂度和文档度量合并为一个数值，用以表达一段代码有多容易维护。该指数由当时在[爱达荷大学](https://www.edgepedia.org/zh/university-of-idaho)（University of Idaho）的保罗·欧曼（Paul Oman）和约翰·哈格迈斯特（John Hagemeister）在1992年软件维护会议（Conference on Software Maintenance）上提出；在那次会议上，可维护性因素被组织成一个可度量属性的层级结构，并可合并为单一指数<sup>[1](https://doi.org/10.1109/icsm.1992.242525)</sup>。该指数由唐·科尔曼（Don Coleman）、丹·阿什（Dan Ash）、布鲁斯·劳瑟（Bruce Lowther）和保罗·欧曼与[惠普](https://www.edgepedia.org/zh/hewlett-packard)（Hewlett-Packard）工程师合作开发并校准，成果于1994年发表在《IEEE Computer》上<sup>[2](https://www.ecs.csun.edu/~rlingard/comp589/ColemanPaper.pdf)</sup>；如今，包括微软 [Visual Studio](https://www.edgepedia.org/zh/visual-studio)（Microsoft Visual Studio）在内的工具都会计算该指数<sup>[3](https://learn.microsoft.com/en-us/visualstudio/code-quality/code-metrics-maintainability-index-range-and-meaning?trk=article-ssr-frontend-pulse_little-text-block&view=visualstudio)</sup>。

| 关键事实 | 详情 |
|---|---|
| 起源 | 由欧曼和哈格迈斯特于1992年软件维护会议提出<sup>[1](https://doi.org/10.1109/icsm.1992.242525)</sup> |
| 输入 | 霍尔斯特德体积（Halstead volume）、圈复杂度（cyclomatic complexity）、代码行数（lines of code）；可选的第四项统计注释密度<sup>[4](https://www.govinfo.gov/content/pkg/GOVPUB-C13-21d16651d79dd84b5c23324b2fd7c32f/pdf/GOVPUB-C13-21d16651d79dd84b5c23324b2fd7c32f.pdf)</sup> |
| 原始标度 | 从171向下直到无下界的负值；低于65为低可维护性，65至85为中等，高于85为高可维护性<sup>[3](https://learn.microsoft.com/en-us/visualstudio/code-quality/code-metrics-maintainability-index-range-and-meaning?trk=article-ssr-frontend-pulse_little-text-block&view=visualstudio)</sup><sup> • </sup><sup>[5](https://www.mdpi.com/2076-3417/13/5/2972)</sup> |
| 微软标度 | 通过乘以100/171并在0处截断下限，重新标定到0–100；0–9为红色，10–19为黄色，20–100为绿色<sup>[3](https://learn.microsoft.com/en-us/visualstudio/code-quality/code-metrics-maintainability-index-range-and-meaning?trk=article-ssr-frontend-pulse_little-text-block&view=visualstudio)</sup> |
| 定义变体 | NIST（美国国家标准与技术研究院）对科尔曼-欧曼（Coleman-Oman）1993年至1997年出版物的一项调查发现，该度量有13种候选定义<sup>[4](https://www.govinfo.gov/content/pkg/GOVPUB-C13-21d16651d79dd84b5c23324b2fd7c32f/pdf/GOVPUB-C13-21d16651d79dd84b5c23324b2fd7c32f.pdf)</sup> |
| 研究用途 | 与CHANGE维护工作量一道，MI是预测可维护性的研究中最常用的因变量<sup>[6](https://strathprints.strath.ac.uk/71689/)</sup> |

## 公式

三度量形式——也就是 Visual Studio 使用的形式——为

\[ \mathrm{MI} = 171 - 5.2 \cdot \ln(\mathrm{HV}) - 0.23 \cdot \mathrm{CC} - 16.2 \cdot \ln(\mathrm{LOC}) \]

其中 HV 是霍尔斯特德体积，一种由运算符和操作数计数推导出的代码信息量度量；CC 是麦凯布圈复杂度（McCabe's cyclomatic complexity），即通过代码的独立路径数；LOC 是代码行数<sup>[3](https://learn.microsoft.com/en-us/visualstudio/code-quality/code-metrics-maintainability-index-range-and-meaning?trk=article-ssr-frontend-pulse_little-text-block&view=visualstudio)</sup><sup> • </sup><sup>[7](https://onlinelibrary.wiley.com/doi/10.1155/2020/8840389)</sup>。每一项都从常数171中减去，因此更大、更复杂的代码产生更低的指数。

四度量的 SEI（Software Engineering Institute）版本增加了一个注释项：

\[ \mathrm{MI} = 171 - 5.2 \cdot \ln(\mathrm{aveV}) - 0.23 \cdot \mathrm{aveV(g')} - 16.2 \cdot \ln(\mathrm{aveLOC}) + 50 \cdot \sin\!\left(\sqrt{2.4 \cdot \mathrm{perCM}}\right) \]

其中各输入按模块取平均值，perCM 是注释行占代码行的比例<sup>[4](https://www.govinfo.gov/content/pkg/GOVPUB-C13-21d16651d79dd84b5c23324b2fd7c32f/pdf/GOVPUB-C13-21d16651d79dd84b5c23324b2fd7c32f.pdf)</sup>。当注释有助于可维护性时，优先使用四度量模型；当代码自描述、注释作用很小时，则使用三度量模型<sup>[8](https://www.mdpi.com/2076-3417/10/13/4624)</sup>。

## 标度与阈值

原始公式产生的值从171一直到无下界的负数。微软对其重新标定，因为小于0的值不会增加有用的区分：所有小于等于0的结果都按0处理，其余范围按100/171缩放，得到

\[ \mathrm{MI} = \max\left(0, \left(171 - 5.2 \cdot \ln(\mathrm{HV}) - 0.23 \cdot \mathrm{CC} - 16.2 \cdot \ln(\mathrm{LOC})\right) \cdot \frac{100}{171}\right) \]

微软在这个0–100标度上选择了保守阈值，按80–20划分以保持低噪声：0–9为红色（低可维护性），10–19为黄色（中等），20–100为绿色（良好）<sup>[3](https://learn.microsoft.com/en-us/visualstudio/code-quality/code-metrics-maintainability-index-range-and-meaning?trk=article-ssr-frontend-pulse_little-text-block&view=visualstudio)</sup>。

在原始的171分标度上，科尔曼-欧曼阈值对系统可维护性的划分为：低于65为低可维护性，65至85之间为中等，高于85为高可维护性<sup>[5](https://www.mdpi.com/2076-3417/13/5/2972)</sup>。在1994年的惠普研究中，两个规模相近的C系统在四度量模型上分别得分89和123；得89分的系统有33.4%的代码行低于质量截止线，而得123分的系统为2.8%，这与维护工程师对两个系统的非正式评估相符<sup>[2](https://www.ecs.csun.edu/~rlingard/comp589/ColemanPaper.pdf)</sup>。

## 实践与研究中的使用

该指数由若干自动化度量工具计算，包括 Visual Studio；后者按类型或方法计算，而不是从模块平均值计算<sup>[4](https://www.govinfo.gov/content/pkg/GOVPUB-C13-21d16651d79dd84b5c23324b2fd7c32f/pdf/GOVPUB-C13-21d16651d79dd84b5c23324b2fd7c32f.pdf)</sup><sup> • </sup><sup>[7](https://onlinelibrary.wiley.com/doi/10.1155/2020/8840389)</sup>。在一项覆盖1991年至2018年的可维护性预测机器学习研究的系统综述中，MI和CHANGE指标（一个类的各版本之间发生变更的行数）是最常用的因变量，通常由类级产品度量来预测<sup>[6](https://strathprints.strath.ac.uk/71689/)</sup>。一项对七个开源系统的研究使用经过人工验证的重构数据集，发现被重构的类和方法可维护性显著低于未重构元素，同时其规模、复杂度和耦合度量更高，这表明开发者会利用这些属性来选择重构目标<sup>[9](https://www.sciencedirect.com/science/article/pii/S0950584916303561)</sup>。

## 局限

该指数是在一小组用过程式语言编写的系统上提出并验证的，研究人员指出了它应用于面向对象代码时的三项局限：其回归系数是在用C和Pascal编写的软件上拟合的；它只使用传统度量，省略了内聚、耦合、多态和继承等面向对象度量；其低、中、高阈值未在面向对象软件上验证。尽管有这些顾虑，它仍获得了普遍接受，并继续频繁用于可维护性研究，而所提出的替代方案则没有<sup>[5](https://www.mdpi.com/2076-3417/13/5/2972)</sup><sup> • </sup><sup>[8](https://www.mdpi.com/2076-3417/10/13/4624)</sup>。

与实际维护工作量的相关性取决于具体情境。一项对三个大型开源系统的研究覆盖超过100万行代码和超过10,000个类，考察了12种常见静态度量，未能验证其中任何一种与维护工作量之间存在普遍相关性：相关性随时间区间不同而为正或为负，强相关只出现在特定项目和有限时间跨度内，而在整个项目历史上，所有度量最多只显示出中等相关性。作者因此主张，未经项目特定验证，不应使用此类度量<sup>[10](https://www.cs.cit.tum.de/sse/aktuelles/article/new-paper-at-saner-revisiting-inter-class-maintainability-indicators/)</sup>。另一项对静态分析工具的综述发现，这些工具提供的度量中只有很小一部分似乎在文献中得到验证<sup>[11](https://arxiv.org/abs/1909.09682)</sup>。

## References

1. [P. Oman, J. Hagemeister (1992). Metrics for assessing a software system's maintainability. Proc. ICSM, pp. 337–344.](https://doi.org/10.1109/icsm.1992.242525)
2. [D. Coleman, D. Ash, B. Lowther, P. Oman (1994). Using metrics to evaluate software system maintainability. IEEE Computer 27(8):44–49.](https://www.ecs.csun.edu/~rlingard/comp589/ColemanPaper.pdf)
3. [Code metrics - Maintainability index range and meaning, Microsoft Learn](https://learn.microsoft.com/en-us/visualstudio/code-quality/code-metrics-maintainability-index-range-and-meaning?trk=article-ssr-frontend-pulse_little-text-block&view=visualstudio)
4. [Quantities and Units for Software Product Measurements, NIST IR 8289](https://www.govinfo.gov/content/pkg/GOVPUB-C13-21d16651d79dd84b5c23324b2fd7c32f/pdf/GOVPUB-C13-21d16651d79dd84b5c23324b2fd7c32f.pdf)
5. [Exploring Maintainability Index Variants for Software Maintainability Measurement in Object-Oriented Systems, Applied Sciences 13(5):2972, 2023](https://www.mdpi.com/2076-3417/13/5/2972)
6. [H. Alsolai, M. Roper (2020). A systematic literature review of machine learning techniques for software maintainability prediction. Information and Software Technology 119:106214.](https://strathprints.strath.ac.uk/71689/)
7. [L. Ardito et al. (2020). A Tool-Based Perspective on Software Code Maintainability Metrics: A Systematic Literature Review. Scientific Programming.](https://onlinelibrary.wiley.com/doi/10.1155/2020/8840389)
8. [Impact of Historical Software Metric Changes in Predicting Future Maintainability Trends in Open-Source Software Development, Applied Sciences 10(13):4624, 2020](https://www.mdpi.com/2076-3417/10/13/4624)
9. [Empirical evaluation of software maintainability based on a manually validated refactoring dataset. Information and Software Technology 95:313–327, 2018.](https://www.sciencedirect.com/science/article/pii/S0950584916303561)
10. [Revisiting Inter-Class Maintainability Indicators, SANER 2023, TU Munich](https://www.cs.cit.tum.de/sse/aktuelles/article/new-paper-at-saner-revisiting-inter-class-maintainability-indicators/)
11. [M. Nilson, V. Antinyan, L. Gren (2019). Do internal software quality tools measure validated metrics? arXiv:1909.09682.](https://arxiv.org/abs/1909.09682)

---
*Initially written Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 软件与编程 › Software engineering and development process › Software testing and quality › Software metrics and measurement

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/maintainability-index

English original: https://www.edgepedia.org/maintainability-index
