Updated: Roslyn Metrics extension

Roslyn seemed to be a good choice…

The Roslyn compiler API has evolved over time… the latest pre-release available on Nuget performs way better than the CTP published almost two years ago, so I decided to continue working on my metrics calculator that is based on Microsoft´s new compiler API. In addition to the initially implemented metrics, like maintainability index, cyclomatic complexity, depth of inheritance, class coupling and lines of code, I added a bunch of other metrics. For instance, lack of cohesion of methods (a metric that can be used to find poorly cohesive types), the number of overloads of a method, the number of lines of comments and some other low-hanging fruits likes counters for fields, method parameters, methods, types and so on…

The Roslyn metrics extension does not analyze compiled assemblies, instead syntax- and semantic models are used to aggregate information right from the source code. In my opinion, this has some significant advantages, because calculated metric results are closer to the code typed by the developer (since the code is not optimized by the compiler). It´s also possible to calculate metric results even on projects which do not compile (for instance, due to missing third-party assemblies or any other kind of build dependency).

Calculating metrics on non-optimized (as typed) code also allows the definition of metrics that put code readability more into focus. For instance, I could imagine a metric that finds all variable declarations in a method, which could be moved closer to their usage, which is something that drastically improves readability and stability, because narrowing the scope of a variable can help to prevent situations where a variable has not been set to a valid instance or value. The purpose of the lines of comments metric (as already implemented) is obvious, I think… comments are adding noise to the code, which has a poor impact on readability (apart from that, comments are usually not more than a pack of lies). Code containing comments might also hold potential for refactoring.

Anything else? Yes. Now, the metrics calculator is also available as a standalone application, so it´s possible to use the tool in a CI build environment. The download can be found at http://bit.ly/roslynmetricsutil