Skip to main content

Posts

Showing posts from May, 2010

Use Discretion when Reviewing Code Metrics

Code metrics are interesting creatures. Some are just raw numbers, such as depth of inheritance or lines of code, while others are a bit more subjective, like a maintainability index. But ultimately they are all meaningless without broader context and an understanding of the code. As a brief example, consider the following C# function which accepts a string and returns the 40-character hexadecimal representation of the string's SHA1 hash . public static String Sha1(String text) { using (SHA1Managed sha1 = new SHA1Managed()) { Byte[] textBytes = Encoding.Unicode.GetBytes(text); Byte[] hashBytes = sha1.ComputeHash(textBytes); return Convert.ToBase64String(hashBytes); } } The function accomplishes one task. Variables are defined closest to their usage. Function calls are clear and do not nest other function calls. Even using ( ) is used so the run-time can automatically dispose of the SHA1Managed resource. Yet a scan using Visual Studio 2010