PhpMetrics icon indicating copy to clipboard operation
PhpMetrics copied to clipboard

Formula for Maintainability index

Open krukru opened this issue 6 years ago • 5 comments

I noticed that the formula for maintainability index takes into consideration code comments. We frequently try to keep comments to a minimum and use self-explanatory code and php7 types whenever possible. Sadly this means we are almost aways 20 points down on the MI because of no phpdocs.

The formula you use is a small derivation of the original formula (see reference).

I would like to be able to provide a custom implementation for calculating MI, Microsoft Visual Studio formula looks very promising. Do you think that this can be done? I could help with a PR, just point me in the right direction.

krukru avatar Nov 02 '17 08:11 krukru

Hi!

The reference your are pointing seems to be a bit wrong according to my research.

The original formula written by Paul Oman and Jack Hagemeister in 1992 and promoted by the SEI in 1997 is the one with the percentage of the lines of comments. If you go deeper in the formula, you may also see that the "magic" 2,4 constant is just an approximation of the real value (pi²/4), that's why you can sometimes find the "magic" constant to "2,46" instead of "2,4", more precise.

The formula without comment is a simpler formula in case of no usage of documentation in the metrics (foreign language from a fetch foreign project so docs are unreadable, very old and unconsistants docs, ...)

The formula with comments has a range [0;221[ The formula without comments has a range [0;171]

The Visual studio's formula is just the same as the formula without comments but spread on a 100% line. This means if the formula without comments says your class have an MI of 171, the MVS's formula will say your MI is 100. For a MI (without comments) of 85,5, MVS will say 50.

To me, there are only 2 formulas, but the one without comments is not really relevant if you have projects with real usefull documentation pieces in. Indeed, I'm not sure about implementing it into a second MI calculation, but if that's something you really need, I will let you make a PR for this. But it must be a new value that must not be taking into account in the MI graphic and some valuable reports.

BTW, using self-explanatory code with php7 types are good things to quickly understand the basics, but a PHPDoc or some inline comments for tricky business part of your project are really recommanded.

niconoe- avatar Nov 06 '17 12:11 niconoe-

+1 to @krukru These are metrics of ~80k lines project before and after autogenerating phpdoc for whole project - which I think shouldn't raise (so much?) maintainability metric in (correctly typed) PHP7 project: phpmetrics

jzawadzki avatar Jan 02 '18 17:01 jzawadzki

Hi !

Thanks for this discussion.

First, PhpMetrics provides the "Maintainability Index" (Mi), the "Maintainability Index comment weight" (MIcw) and "Maintainability Index Without comment" (MIwoc)

You're right, there are many way to calculate the maintainability index. I've tried lot of of them, on thousands of projects (Open Source or / and from real companies). For some of these projets, I've audited manually the project before, during many days

My conclusions are :

  • some formulas are too restrictives for PHP projects ;
  • with the Visual Studio formula, lots of real projects are totally red ;
  • yes, it's easy to improve (without legitimacy) the Maintainability index generating phpdoc ;

But Maintainability index is one the universal and language agnostic metrics, so I prefer to keep comments in the formula.

An idea can be to add a checkbox "exclude comments for calculation of maintainability". If checked, graph use the MIwoc instead of the Mi. What do you thin about this proposition ?

Halleck45 avatar Jul 25 '18 06:07 Halleck45

+1 for Maintainability Index Without comment.

It would be nice if we can add the option in console argument (configuration) instead of this

An idea can be to add a checkbox "exclude comments for calculation of maintainability". If checked, graph use the MIwoc instead of the Mi. What do you thin about this proposition ?

pattisahusiwa avatar Nov 12 '19 17:11 pattisahusiwa

@Halleck45 As this is released in 2.6, do you think we can close that issue?

niconoe- avatar Apr 02 '20 21:04 niconoe-

New option is available since v2.6 to use MI or MIwoc, letting the choice to the end user.

Closing this issue as solved.

niconoe- avatar Sep 22 '23 07:09 niconoe-