Code Metrics – Calculating Metrics
Windows: Alt, N, M (selected projects); Alt, N, L (solution); Alt, V, E, M, M, M, Enter (results)
Menu: Analyze | Calculate Code Metrics For Selected Projects; Analyze | Calculate Code Metrics For Solution; View | Other Windows | Code Metric Results
Command: Analyze.CalculateCodeMetricsforSelectedProjects; Analyze.CalculateCodeMetricsforSolution; Analyze.CancelCodeMetricsCalculation; View.CodeMetricsResults
SKU: Premium, Ultimate
Versions: 2008, 2010
Code: vstipTool0129
During my many Visual Studio tours to visit customers with my good friend Clint Edmonson, we discovered that a lot of people apparently didn’t know they had access to code metrics. So, that begs the question, what are code metrics and why should you care?
According to Wikipedia:
“A software metric is a measure of some property of a piece of software or its specifications. Since quantitative measurements are essential in all sciences, there is a continuous effort by computer science practitioners and theoreticians to bring similar approaches to software development. The goal is obtaining objective, reproducible and quantifiable measurements, which may have numerous valuable applications in schedule and budget planning, cost estimation, quality assurance testing, software debugging, software performance optimization, and optimal personnel task assignments.”
https://en.wikipedia.org/wiki/Software_metric
Specifically, for our feature, referred to as code metrics, we say this:
“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.”
https://msdn.microsoft.com/en-us/library/bb385914.aspx
Code metrics essentially allows us to view, at a high level, data about our code. By reviewing this data, we can make qualitative decisions about areas that could be of concern and require additional examination. We often refer to this as looking for code smells (https://en.wikipedia.org/wiki/Code_smell).
Calculating Code Metrics
You can calculate code metrics for one project, multiple projects, or an entire solution. The scope is often determined by the size of your solution. With larger solutions you may want to calculate metrics for one or more projects to narrow down the scope where issues might occur.
Single Project
Calculating code metrics for a single project is straightforward. Make sure the project (or any item in the project) is selected in Solution Explorer. Then go to Analyze | Calculate Code Metrics For <Project Name>:
Multiple Projects
When you want to calculate code metrics for more than one project, you can CTRL + Left Click the projects in Solution Explorer:
Go to Analyze | Calculate Code Metrics For Selected Project(s):
Entire Solution
Calculating code metrics for an entire solution is simply a matter of going to Analyze | Calculate Code Metrics for Solution. Also, the Code Metrics Results window (View | Other Windows | Code Metrics Results) has a button that will calculate the metrics for the entire solution:
Special Note
As you use code metrics, you may receive the following message: “One or more projects were skipped. Code metrics are available only for C#, Visual Basic, and C++/CLI projects that are not Web Site projects.” This is fairly self explanatory and should be taken into account when calculating code metrics.
Comments
Anonymous
July 18, 2011
I don't have an "Analyze" (Analyse?) menu.Anonymous
July 18, 2011
Hanii, This feature is only available in Premium and Ultimate versions of VS. ZAnonymous
September 25, 2012
Hi, regarding your special note, “One or more projects were skipped. Code metrics are available only for C#, Visual Basic, and C++/CLI projects that are not Web Site projects.”... I use to not get this message, now I randomly do. I looked and running metrics on the entire solution works (no projects are excluded), but I still get that error and when I click on it... it isn't linked to anything (i.e. "No source available"). Any ideas?Anonymous
May 13, 2013
Vlad, Did you get the solution of your prob? I'm also facing the same problem. Please let me know if you have the solution.Anonymous
May 27, 2013
Vlad / Pal, Are you getting all the metrics but it is just giving you an error or are metrics missing? ZAnonymous
July 29, 2013
I am getting the metrics for other projects in the solution except the website project.Anonymous
August 15, 2013
Hey Pal :) You can calculate metrics for web application projects but not website projects unfortunately. ZAnonymous
October 09, 2013
I'm getting the same error running Metrics 10.0 against a VS 2010 Solution that contains two C# class library projects and a C# Web Application project. I tried running against just one of the class library project files ("BusinessLayer.csproj") and received the same error.