Code Metrics in Visual Studio
Code metrics feature is available in Visual Studio 2010/2013 Premium and Ultimate.
What is code metrics?
Code metrics is a set of software measures that provide developers better insight into the code they are developing. By taking advantage of code metrics, developers can understand which types and/or methods should be reworked or more thoroughly tested. Development teams can identify potential risks, understand the current state of a project, and track progress during software development.
Code metrics provides Maintainability Index, Cyclomatic Complexity, Depth of Inheritance, Class Coupling and Lines of Code. See below screen.
How to generated code Metrics data?
- Click on “Analyze Menu”, then it will display drop down menu, after that click on “Calculate code metrics for solution”
- Click Solution Explorer and then click on “Calculate code metrics”
- After clicking on “calculate code matrix “your “code metrics result” window will display.
Maintainability Index: Maintainability Index is based on a formula that returns a value between 0 and 100 that describe / represents the overall maintaining the code or relative maintaining the code. A high value means better maintainability.
- 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.
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.
Depth of Inheritance:
Depth of inheritance is also called as depth of inheritance tree, Depth of inheritance calculate the maximum length/number of path from class to root class in inheritance structure of software.
If you are using deeper hierarchy for a particular method then it becomes difficult to understand.
Class coupling measures the coupling 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.
If you are using high coupling classes that means your design is more difficult to reuse and maintain, because of its many interdependencies on other types. Use independent classes for good software design.
Lines of Code:
Lines of code Indicates the approximate number of lines in your 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.