Monday, December 20, 2010

Code Metrics

This is continuation of my previous post on MVVM project I recently completed.

I decided to analyze some of the code metrics. I really like NDepend add-in for Visual Studio, but it is not a free tool. So I had to resort to .NET Reflector and multiple community developed add-ins.

Here is a screen shot from CodeMetrics add-in for .NET Reflector.

image

This tool also provides nice statistics for classes, methods  or modules from IL assemblies.
Some people like to talk a lot about Cyclomatic Complexity, while it is useful only on a per method basis. Cyclomatic Complexity of the whole project grows dramatically as your project grows and becomes inadequate in such cases. The rule of thumb is if Cyclomatic Complexity greater than 15 the project will have increased difficulty in knowledge transfer, testing, debugging and maintenance. Additionally such code tends to be more rigid which means changes are more difficult to implement.

Below is a recommendation given by NDepend:

Recommendations: Methods where CC is higher than 15 are hard to understand and maintain. Methods where CC is higher than 30 are extremely complex and should be split in smaller methods (except if they are automatically generated by a tool).

In my project there are 14 193 methods, most of the ones with higher cyclomatic complexity were generated and the rest 13 923 have cyclomatic complexity of 9 or less, which is about 98% of all code base.

Another useful metrics for types (classes) is “Depth of Inheritance Tree”. Here is a recommendation given by NDepend about depth of inheritance tree:

Depth of Inheritance Tree (DIT): The Depth of Inheritance Tree for a class or a structure is its number of base classes (including the System.Object class thus DIT >= 1).
Recommendations: Types where DepthOfInheritance is higher or equal than 6 might be hard to maintain. However it is not a rule since sometime your classes might inherit from tier classes which have a high value for depth of inheritance. For example, the average depth of inheritance for framework classes which derive from System.Windows.Forms.Control is 5.3.

In SHMMP Manager Average Depth of Inheritance Tree is 4.08, which is very good.

There are other numbers CodeMetrics add-in provides, some you may use to impress some people, usually those unfamiliar with programming :). Such as number of lines of code. In SHMMP Manager there are about 200 000 lines of code. This number is even greater if taken straight from IL disassembler and is 1 295 965, which, if printed, translates to roughly 43 000 pages. Of course 90% of these lines were generated by the compiler :)

NDepend can also analyze code for testability and compositionality and provides nice dependency graphs and matrices. Based on Dependency Matrix, for an example, one may start refactoring process. Keeping track of dependencies especially important when building common class libraries or frameworks.

So try these tools and see if they are helpful.

No comments:

Post a Comment