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.