Lines of Code in application source is one of the most common metrics associated with software development projects, mainly to measure productivity.
Table of Contents
How to Count Lines of Code in Visual Studio?
There are various tools available to Count the Lines of Code (LOC) in .NET solution.
But many developers may still not be knowing that there is a direct tool made available by Microsoft which will help us to measure C# Loc,VB.NET Loc or in general, count the lines of code(LOC) in any .NET application using visual studio.
Code Metrics Tool to Count Lines of Code (LOC) in C#
The tool is called Code Metrics which available from Visual Studio 2008 and higher versions.
This tool is used to count the Lines of code in a .NET solution.
But this code analysis tool ,Code Metrics, is only available in the Visual Studio Premium and above editions.
That means you cannot see the Code Metrics tool option in Visual Studio Professional Edition.
Read the post How to use Code Metrics in Visual Studio Professional editions here.
Code Metrics Tool can be used for calculating the code maintainability index, cyclomatic complexity of the code, coupling between classes, inheritance depth and number of lines of code (LOC) for members and types of a .NET assembly.
So Code metrics tool is meant for determining the quality, extensibility, and maintainability of a software to a great extent.
Usage of the Code Metrics tool is straight forward and easy.
Here in this post, I explain how to use code metrics tool for code analysis from visual studio. Follow the steps mentioned under below headline
Count Lines of Code (LOC) of .NET application in Visual Studio
Open your .NET solution which you need to count LOC.
Go to Solution Explorer, Right-click on the solution and select the context menu option “Code Metrics Viewer”.
Code Metrics Viewer window will open. Click the Analyze Solution button in the Code Metrics Viewer.
You can see the code analysis report screen as shown below window.
Code Metrics tool helps the user in finding the Maintainability Index, Cyclomatic Complexity, Class Coupling, Depth of Inheritance & Lines of Code.
See the brief definition of each of these code quality parameters below.
Maintainability Index calculates the relative ease of maintaining your code. The value is between 0 and 100.Maintainability Index value should be higher to have better maintainability.
Maintainability Index = MAX(0,(171 – 5.2 * log(Halstead Volume) – 0.23 * (Cyclomatic Complexity) – 16.2 * log(Lines of Code))*100 / 171)
Cyclomatic Complexity measures the total number of individual paths through the code by counting the number of decision points in the code.
Decision points are if blocks, for each, for loops, switch cases, do, while, etc. The Lower the value of Cyclomatic complexity, better the code is.
Class Coupling is the total number of dependencies the class has on other classes.
Coupling is expected to be as low as possible to avoid dependency among classes. If the dependency is more classes are called tightly coupled.
Class coupling does not consider the dependency to built-in primitive types such as Object,Int32, string etc.
Depth of Inheritance
The depth of inheritance of your code indicates the number of types that are of the specific type in the inheritance hierarchy.
Interface implementations are not considered while calculating Depth of Inheritance. The depth of Inheritance is one important code quality parameter.
Line of Code
Code metrics helps to get the total number of executable lines of your code excluding white spaces, comments, braces, types and namespaces and the declarations of members.
Lower the number of lines of code in your application better your code.
In this article we covered using visual studio to count lines of code in a .NET solution. With the tool Code Metrics in Visual Studio you can measure C# LOC or count the lines of code in any project within Visual Studio Ultimate edition. We hope you understood and enjoyed the article. I would like to have your feedback in the comment section below.
Leave a Reply