Sunday, February 06, 2011

What is Code Metrics

Visual Studio 2008/2010 comes with a nice tool that called – Code Metrics: “gives you the ability to dig deep to find those un-maintainable and complex hotspots” Code Metrics calculate 5 measures:
Lines of Code – Indicates the approximate number of lines in the code. The count is based on the IL code and is therefore not the exact number of lines in the source code file. A very high count might indicate that a type or method is trying to do too much work and should be split up. It might also indicate that the type or method might be hard to maintain.

* Why? – Bill Gates said – Measuring programming progress by lines of code is like measuring aircraft building progress by weight.  (Source - http://www.softwarequotes.com/printableshowquotes.aspx?id=579)
I don’t fully agree with that say, but if Bill said so bill knows.

Class Coupling - Measures the coupling to unique classes through parameters, local variables, return types, method calls, generic or template instantiations, base classes, interface implementations, fields defined on external types, and attribute decoration. Good software design dictates that types and methods should have high cohesion and low coupling. High coupling indicates a design that is difficult to reuse and maintain because of its many interdependencies on other types

Depth of Inheritance – Indicates the number of class definitions that extend to the root of the class hierarchy. The deeper the hierarchy the more difficult it might be to understand where particular methods and fields are defined or/and redefined.

Cyclomatic Complexity –  Measures the structural complexity of the code. It is created by calculating the number of different code paths in the flow of the program. A program that has complex control flow will require more tests to achieve good code coverage and will be less maintainable.

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. A green rating is between 20 and 100 and indicates that the code has good maintainability. A yellow rating is between 10 and 19 and indicates that the code is moderately maintainable. A red rating is a rating between 0 and 9 and indicates low maintainability.

How To Calculate Code Metrics in Visual Studio 2010:

1. Open Visual Studio and load the desire Solution for the Code Metrics Calculation.
2. Open Analysis Menu –> Choose “Calculate Code Metrics for Solution” and you’re Done!

Read more: Shai Raiten