Understanding Code Metrics
Posted on: February 18, 2015 | By: Phil McGaw
Code metrics can be an important tool to use following and during development to give better insight into the complexity of the solution. By taking advantage of the metrics the following benefits can be achieved;
- Understanding which types and / or methods should be refactored
- Identify potential risks with the existing development
- Understand the impact of changes to existing types and / or methods
Here are the current code metrics from Visual Studio split into the following categories.
|Maintainability index||Calculates an index between 0 and 100 that represents the relative ease of maintaining the code. The higher the code the better maintainability. Colour codes are split into three categories – Green between 20 – 100, Yellow between 10 – 19, and Red between 0 – 9|
|Cyclomatic Complexity||Measures the structural complexity of the code, calculated by the number of decision logic contained in the code. It should provide insight into how hard the code may be to test, maintain or troubleshoot as well as how likely the code will be to produce errors. The higher the number the more complex the code is deemed.|
|Depth of Inheritance||The number of class definitions that extend to the root of the class hierarchy. The higher this value the more difficult it might be to understand where particular methods and fields are defined.|
|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. A high value here indicates that the code may be difficult to reuse and maintain because of its many interdependencies.|
|Lines of Code||The approximate number of lines of code. A high number may indicate the code is trying to do too much and may be hard to maintain.|
It is important to note that sometimes values produced by the above analysis can be warranted. For example in companies which have experienced staff, formal design, structured programming, code walkthroughs or comprehensive test plans a high cyclomatic complexity value may not be as much of an issue.
One good standard which should be adopted when using code metrics is to look at the lines of code value. Often areas which contain high numbers of lines can be considered complex and increase the risk of errors, not to mention increase maintenance effort.
As with any code quality measuring tool like JetBrains ReSharper or the default Visual Studio tools, they are there to present an understanding of the code area and to assist in making good design decisions on how to structure and build your software.
Code metrics should be run at the start and at the end of a code change and the metric values documented to provide a history of the quality of the code and to highlight areas of improvement. This identification of any additional work to be undertaken can be carried out during the code review stage if the developer feels they may not have the required skills to interpret the values.