Search code examples
.netvisual-studio-2010code-metrics

What should be the value of Maintainability Index for a standard .NET project?


I am working with multiple enterprise level .NET projects. As a part of Code review tasks, we calculate Code Metrics (Maintainability Index and Cyclomatic Complexity) for these projects using Visual Studio tools.

Now, for Maintainability Index, the acceptable range as suggested by MSDN blogs (that is the Green zone!) is 20-100. But in terms of metrics, that is quite a large range to be in.

I have ever been wondering what should be the ideal value which we should achieve in our code. Some industry experts suggest a range of 40-50. But that is purely a choice, without any specific reason behind.

I came across the below links but they don't suggest any standard.


Solution

  • There is no right answer to this question. There are still quite a few "it depends" issues:

    • Some methods are just complex, but not hard to maintain. An example would be an Automapper configuration, with lots of relatively simple lambda's. Each are read individually and they are pretty fluent. They'd probably score almost into red, but they'd be fine. I'd prefer a long Automapper configuration over a whole bunch of individual methods.
    • The older versions of Metrics (I see you tagged ) are especially susceptible to Lambda expressions, delegates and other items that generate code under the hood. The 2013 variant handles these a lot better, the 2015 variant (in the IDE at least), depends on Roslyn and really applies to your code..
    • Code with bad names can be much harder to read than code with good ones, the Maintainability Index can't take that sort of readability into account.
    • layout of the code can also help or destroy code readability. Ever tried to read a Minified piece of Javascript? These will get the exact same Maintainability Index score!

    Apart from these, the standard thresholds of Visual Studio are quite liberal. I remember a quote from someone at the Visual Studio team that a too tight setting would mark too much of the average customer's code in orange or red, making people hate the tool, so a slightly more tolerant setting was used. This is why many people with already pretty good codebases tend to use stricter numbers.

    The Metrics Codelens add-on or Visual studio 2013 Ultimate shows a better standard score:

    enter image description here

    Using 60-100 for green.

    What is the right setting you ask... well it can be anything between 0 and 100, as that's the range the maintainability index floats between since they re-calibrated it.

    Anything above 20 isn't too bad, anything above 60 is pretty good. And you'll certainly have exceptions, where breaking down further doesn't add readability, only obscures the intent of the code. So if you want something better, maybe take to the ones Codelens uses, but don't turn on a blanket rule saying that all code should be at least 80, all developers will hate you for it, and it will not be as productive as you might hope :).