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

Code Metrics Viewer 2 CTP

I published a pre-release version of the Code Metrics Viewer 2 extension targeting Visual Studio 2012. It´s the continuation of my first contribution, but it works completely differently. The first version was just a user-interface that integrated the Code Metrics Power-Tool 10.0 into the development environment, but the new version brings its own calculation functionality – and instead of analyzing IL it acts on the source code. The current release supports the metric calculation for C#-projects, but functionality supporting Visual Basic is on schedule.

The extension is dependent on the Microsoft “Roslyn” CTP (September 2012, v1.2), which needs to be downloaded and installed separately. Because I wasn´t sure, if I am allowed to distribute the Roslyn binaries, I decided to exclude them from the extension package and defined a dependency on the Roslyn Components instead. The Extension Manager will show the following dialog to you if the required package dependency is missing.

roslyn-alert

The download can be found at the Visual Studio Developer Center: http://bit.ly/YujBX8 or on NuGet: http://bit.ly/WheTeO. If the components will be removed after installing the extension, calculation of metric results isn´t possible and the following error message will be shown: “The calculation of metric results has failed. Couldn´t find the Roslyn CTP components.”

missing-roslyn

Currently no support of VS2012

Due to the fact that the current version of the Power Tool is not compatible with Visual Studio 2012, there won´t be an upgrade for the Code Metrics Viewer extension. There is a suggestion on Microsoft´s user voice portal to either open up the code metrics calculation interface or to provide a new version of the command line utility, that is not dependent on Visual Studio 2010. Using the following link you can vote for it: http://visualstudio.uservoice.com/forums/121579-visual-studio/suggestions/3014740-please-update-visual-studio-metrics-power-tool-to

Update, 2013/01/7: I just used some time to play with the Roslyn CTP and created a tool that can calculate metrics from syntax trees and semantic models (in the strict sense a replacement for the Power Tool, that does not act on IL, but on source code). By now, I can calculate the same metric results provided by the Power Tool for C# projects and I also added some new metrics.

Update, 2013/01/14: I started to make the extension available to Visual Studio 2012; I just ported the existing code base and removed everything related to Microsoft´s power tool, that can´t be used anymore. The tool window gets a complete make-over supporting both the dark- and light theme. Maybe I have to replace functionality that is dependent on Windows Forms by using WPF, but I don´t know yet. The toolbar is now a native toolbar, that fits much better into the selected theme. As long as Roslyn isn´t ready-to-market, this extension will be a CTP as well, that requires an existing installation of the Roslyn components.

Update, 2013/01/16: A first look at the new UI…

dark-theme-ui

Update, 2013/01/25: I worked a lot on the user-interface… Instead of reusing the existing win-forms tree-listview implementation from the previous version, I decided to create a tree-listview using WPF. I am quite familiar with WPF, so I thought the hardest part of the entire project would be the implementation of the actual metric calculation functionality (which I have achieved within a couple of hours), but in the end, I used more time to style the WPF listview (thanks a lot to a Swedish friend of mine, who helped me to solve some very tricky issues and polish it). It was well worth investing the time because the new control allows scrolling horizontally, the scrollbars are themed automatically, the positioning of grid columns is now supported and I was able to remove some code that required P-Invoke (think this is a step in the right direction).

Update, 2013-01-27: I spiled Visual Studio´s glyph-service to show icons in the results view…

dark-theme-ui-2

Update, 2013-02-02: The extension is almost feature-complete; I am working on some details now. For instance, the new version of the tool doesn´t show a progress dialog anymore. Instead, it has a thin progress indicator that is embedded into the view (so it behaves the same way as any other tool windows in Visual Studio do).

dark-theme-ui-3

I reworked the trend icons (up- and down arrows) and used the colors of Visual Studio´s light theme so I can switch luminosity depending on the selected theme, to make them look good…

dark-theme-ui-4

dark-theme-ui-5