views:

320

answers:

3

I would be appreciative if someone could explain to me the difference between the following two pieces of code in terms of Visual Studio's Code Metrics rules. Why does the Maintainability Index increase slightly if I don't encapsulate everything within using ( )?

Sample 1 (MI score of 71)

public static String Sha1(String plainText)
{
    using (SHA1Managed sha1 = new SHA1Managed())
    {
        Byte[] text = Encoding.Unicode.GetBytes(plainText);
        Byte[] hashBytes = sha1.ComputeHash(text);
        return Convert.ToBase64String(hashBytes);    
    }
}

Sample 2 (MI score of 73)

public static String Sha1(String plainText)
{
    Byte[] text, hashBytes;
    using (SHA1Managed sha1 = new SHA1Managed())
    {
        text = Encoding.Unicode.GetBytes(plainText);
        hashBytes = sha1.ComputeHash(text);
    }
    return Convert.ToBase64String(hashBytes);   
}

I understand metrics are meaningless outside of a broader context and understanding, and programmers should exercise discretion. While I could boost the score up to 76 with return Convert.ToBase64String(sha1.ComputeHash(Encoding.Unicode.GetBytes(plainText))), I shouldn't. I would clearly be just playing with numbers and it isn't truly any more readable or maintainable at that point. I am curious though as to what the logic might be behind the increase in this case. It's obviously not line-count.

+4  A: 

Having your variables all laid out at the top so you know what's in the function is more "maintainable", at least that's what whoever decides the rules for the code metrics thinks.

Whether that's actually true? Totally depends on the team working on the code. It seems you already know this by the tone of the question, but take almost all code metrics with a grain of salt, they're what someone thinks is best, that may not be true for teams outside of microsoft...do what's best for your team, not what some calculator tells you.

I wouldn't make changes that are detrimental to your and your team's coding performance (unless it's for actual performance or improved error handling, etc) that you think are less readable for getting a few points on the metrics board.

All that being said, if it gives you a very low maintainability, there probably is something worth looking at or breaking down into smaller chunks, as a very low score is probably not so acceptable, for pretty much any team.

Nick Craver
I believe this is correct - in terms of explaining the numbers - but I think that the maintainability calculation is obsolete in this regard. Once upon a time it made sense to declare all your variables at the top - once upon a time (some) languages demanded it! - but that time is long past. Today, minimizing an identifier's lifespan contributes more to maintainability.
Carl Manaster
+3  A: 

Because of the increased distance between the declaration of your variables and where they are used.

The rule is to reduce the variable span as much as possible, the span is the distance between the declaration of the variable and where it is used. As this distance increases, the risk increases that later code is introduced that affects the variable without the programmer realising the impact further down in the code.

Here is a link to a good book that covers this and many other topics on code quality. http://www.amazon.com/Code-Complete-Practical-Handbook-Construction/dp/0735619670/ref=dp_ob_title_bk

Chris Taylor
This would seem to be counter to the question, as the distance between declaration and usage is greater in the **higher** score.
Nick Craver
@Nick, :) Good point.
Chris Taylor
@Nick true, but odd. I find the version with the lower score much easier to read and @Chris' explanation seams entirely sensible to me.
Damian Powell
@Damian - The explanation is backwards from the result, sensible I agree, but doesn't explain the question. I also prefer the one with the lower score, but according to this answer, it should have a **higher** score, it does not.
Nick Craver
@Nick I wondered if the OP had got the values the wrong way around. I tested it in VS2010 and it appears not. Although I get different values: 70 and 69. Strange, but highlights your point that it should be takenwith a pinch of salt.
Damian Powell
I have been thinking about this, and while I guess only someone intimately involved with the analysis engine could give a real explanation for this, I wonder if the analysis engine is measuring the line length and saying that the "complexity" of the declaration and intitializing of the variables by calling functions is impacting the metric. I do not have access to VS now, what might be interesting to try is to take the first code and move the declaration of the variable into the scope of the using clause but still initialize on separate lines, what does that come back with?
Chris Taylor
Same numbers, @Chris. A lower value when the variables are defined and used within the using() block, than when they are defined outside the block.
Timothy
@Timothy, Thanks for the update. I must admit, I think it is just a quirk of the analysis, but again I doubt anyone outside of the team developing the technology for the analysis could provide a concrete answer. Of course the difference is small enough so it should not concern you, but from an intellectual stand point it is interesting!
Chris Taylor
A: 

Myself, I'd rather see return Convert.ToBase64String(sha1.ComputeHash(Encoding.Unicode.GetBytes(plainText))); it's a should rather than a shouldn't. This form has the advantage of concisely expressing the actual data-flow; if you add a bunch of temporary variables and assignments, I now have to read the variable names and match up their occurrences to see what's actually happening.

Kevin Reid