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
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.”
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.”
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 (http://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.
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>:
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):
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:
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.