14

I have come across the recommended values for a Maintainability Index (MI) as follows:

  • 85 and more: good maintainability
  • 65-85: moderate maintainability
  • 65 and below: difficult to maintain with really bad pieces of code (big, uncommented, unstructured) the MI value can be even negative

Are these values are dependent on technology? For example, is a value of 70 good for Mainframes but difficult to maintain for Java?

Can use same yardstick independent of technologies?

George Stocker
  • 57,289
  • 29
  • 176
  • 237

5 Answers5

14

This is an explanation about meaning of maintainability index value.

Shortly this is

MI = 171 - 5.2*ln(Halstead Volume) - 0.23*(Cyclomatic Complexity) - 16.2*ln(Lines of Code)

scaled between 0 and 100.

As it's easy to see, this metric can be used for any procedural language.

Anton K
  • 4,658
  • 2
  • 47
  • 60
  • Oops. link is no longer valid. I believe this is the one now - https://blogs.msdn.microsoft.com/zainnab/2011/05/26/code-metrics-maintainability-index/ – RBT Nov 29 '16 at 12:09
  • In fact [this](https://blogs.msdn.microsoft.com/codeanalysis/2007/11/20/maintainability-index-range-and-meaning/) blog archive suggests a different formula to calculate MI as `MAX(0,(171 – 5.2 * ln(Halstead Volume) – 0.23 * (Cyclomatic Complexity) – 16.2 * ln(Lines of Code))*100 / 171)` – RBT Nov 29 '16 at 12:18
  • 2
    @RBT This is the same formula. You unwrapped the "scaled between 0 and 100" sentence. – Anton K Nov 29 '16 at 18:44
9

The 65 and 85 thresholds come from the original paper introducing the Maintainability Index in 1992/1994.

Visual Studio has slightly adjusted the metric (mutiplying by 100/171) to make it fit to a 1-100 scale. Visual Studio uses 10 and 20 as thresholds.

In general I would not take this metric and its thresholds too seriously: See also my blog post "Think Twice Before Using the Maintainability Index."

avandeursen
  • 8,458
  • 3
  • 41
  • 51
3

The Maintainability Index is a empirical formula. It is, was constructed a model based in observation and adaptation. If you search for more detail, will discover that the equation have to be cabibrated for a specific language. The version of SEI is calibrated for Pascal and C, and used a bunch of programs, average 50KLOC, maintained by Hewlett-Packard.

The calibration of Visual Studio version is the same of SEI version, but was padronized to restrict the domain from 0 to 100.

2

I don't think you can put a number on how easy a piece of code will be for a developer to maintain.

Different people will look at the same code and interpret it in their own way, depending on experience, culture, reading comprehension, etc.

That being said, metrics would definitely be different across technologies. You're looking at completely different syntax, conventions, terminology, etc. How can you quantify the difficulty difference between low level mainframe code and a high level language like Java or C#?

I think metrics are good for one thing, and one thing only: guidelines. In terms of code quality, I don't think they should be used for anything other than describing a code base. They should not be used as a determining factor of difficulty or "grok-ability".

karlgrz
  • 14,485
  • 12
  • 47
  • 58
0

It depends how the "Maintainability Index" is calculated. This doesn't seem to me like something which would work across languages simply because languages are so different.

It might seem reasonable to do a simple compare of "number of lines per function", but what happens when you try and compare C code which is full of pointers, or C++ code full of templates, or C# with LINQ queries, or Java with generics?
All of those things affect maintainability but can't be measured in any meaningful cross-language way, so how can you ever compare numbers between 2 languages?

Orion Edwards
  • 121,657
  • 64
  • 239
  • 328
  • 2
    Lambdas in particular aren't handled by the metrics. Every lambda function adds an extra amount to your complexity, even though in reality it simplifies code. We gave up on the metrics. – MichaelGG Feb 27 '09 at 03:38
  • 1
    I can get why lambdas would hurt the complexity score and it's a legitimate issue for junior devs as it essentially concentrates complexity. There may need to be a different complexity measure for senior devs though... – Brian Knoblauch May 20 '16 at 18:05