Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual Studio Code Metrics and the Maintainability index of switch case

As a person who loves to follow the best practices,

If i run code metrics (right click on project name in solution explorer and select "Calculate Code Metrics" - Visual Studio 2010) on:

    public static string GetFormFactor(int number)
    {
        string formFactor = string.Empty;
        switch (number)
        {
            case 1:
                formFactor = "Other";
                break;

            case 2:
                formFactor = "SIP";
                break;

            case 3:
                formFactor = "DIP";
                break;

            case 4:
                formFactor = "ZIP";
                break;

            case 5:
                formFactor = "SOJ";
                break;
        }

        return formFactor;
    }

It Gives me a Maintainability index of 61

(of course this is insignificant if you have only this, but if you use an utility like class whos philosophy is doing stuff like that, your utility class will have the maintainability index much worst..)

Whats the solution for this?

like image 763
pee2002 Avatar asked May 29 '10 22:05

pee2002


People also ask

What is maintainability index in code metrics?

Maintainability Index - Calculates an index value between 0 and 100 that represents the relative ease of maintaining the code. A high value means better maintainability. Color coded ratings can be used to quickly identify trouble spots in your code.

How do you get code metrics in VS code?

Generate code metrics results for one or more projects In Solution Explorer, select one or more projects. From the menu bar, select Analyze > Calculate Code Metrics > For Selected Project(s).

How do you check cyclomatic complexity in Visual Studio code?

From Visual Studio's menu, select Analyze -> Calculate Code Metrics. Then, either select “For Solution”, if you want check all the projects in the solution, or select the project you want. The results will be displayed in the Output window. Visual Studio shows the cyclomatic complexity for each function and property.


2 Answers

First of all: 61 is considered to be maintainable code. For the Maintainability Index, 100 is very easy to maintain code, while 0 is hard to maintain.

  • 0-9 = hard to maintain
  • 10-19 = moderate to maintain
  • 20-100 = good to maintain

The Maintainability Index is based on three code metrics: Namely the Halstead Volumen, the Cyclomatic Complexity and Lines of Code and based on the following formula:

MAX(0,(171 - 5.2 * ln(Halstead Volume) - 0.23 * (Cyclomatic Complexity) - 16.2 * ln(Lines of Code))*100 / 171)

In fact, in your example the root cause for the low value of the Maintainability Index is the Cyclomatic Complexity. This metric is calculated based on the various execution paths in the code. Unfortunately, the metric does not necessarily align with "human readability" of code.

Examples as your code result in very low index values (remember, lower means harder to maintain) but in fact they are very easy to read. This is common when using Cyclomatic Complexity to evaluate code.

Imagine code that has a switch-block for days (Mon-Sun) plus a switch-block for Months (Jan-Dec). This code will be very readable and maintainable but will result in a enormous Cyclomatic Complexity and thus in a very low Maintainability Index in Visual Studio 2010.

This is a well known fact about the metric and should be considered if code is judged based on the figures. Rather than looking at the absolute number, the figures should be monitored over time to understand as an indicator for the change of the code. E.g. if the Maintainability Index was always at 100 and suddenly went down to 10 you should inspect the change that caused this.

like image 149
aheil Avatar answered Oct 12 '22 13:10

aheil


The maintainability index could be higher because of the lack of expandability in the method you are choosing for your solution.

The correct solution (Mark Simpson touched on above) is the one that can be expanded to use a new form factor without the code being rebuilt - switch / case statements in code are always a sign that OO design has been forgotten and should always be seen as a bad code smell.

Personally, I would implement...

interface IFormFactor
{
    // some methods
}

class SipFormFactor : IFormFactor...

class DipFormFactor : IFormFactor...

Etc.

...and have the methods on the interface provide the desired functionality - you can think of it [I guess] as being similar in to GoF Command Pattern.

This way your higher level methods can just be...

MyMethod(IFormFactor factor)
{
    // some logic...

    factor.DoSomething();

    // some more logic...
}

...and you can come along at a later date and introduce a new form factor without having to change this code as you would with the hard-coded switch clause. You will also find this approach also lends itself easily to TDD (you should end up with this if you are doing TDD properly) as it is easy for mocking.

like image 31
James Lawson Avatar answered Oct 12 '22 12:10

James Lawson