Line counts have always been one of the most common metrics associated with software development projects, albeit not a very meaningful one.
Although Visual Studio is Microsoft’s flagship IDE, there are often a few features that many users would like to be able to easily access once in a while that simply are not built-in. An easily accessible “Lines of Code” button or display may be something that some users occasionally want to access and unless they have one of the “premium” versions (Premium or Ultimate), they may be out of luck.
This blog post will cover how to get the number of lines of code within your application in both premium and non-premium versions of Visual Studio through either the integrated Code Metrics tools (Premium-versions only) or by leveraging a crafty Regular Expression and without having to resort to using a third-party utility or tool.
Why Would You Need This?
As mentioned, earlier due to the substantial number of design patterns, programming styles, naming conventions and numerous other factors, the line count metric often doesn’t provide much meaningful information. However, there are a few situations that it could be used to help :
Although, it can be valuable in some of the following situations :
- Refactoring – When refactoring Legacy code or revisiting older projects, the line count can provide a small amount of value to help demonstrate how inefficient the coding process may have been during its initial development. If the number of lines that can be removed from the original version constitutes a large percentage of the total size of the application, while maintaining functionality and readability, then the lines that were removed could provide a valuable metric.
- Complexity – If you were provided two applications, one with 1,000 lines of code and another with 1,000,000+ lines, it would be safe to assume that the application that is several magnitudes larger is more complex.
- Productivity - Much like application complexity, lines of code provide a key insight into if a development team is actually being productive or not. If you have a situation where you have two developments teams and over a period of time (assuming similar coding styles) one of the teams has 1,000 lines of code and the other has 5,000, it’s clear which team has been more productive.
As you can see from these examples, nearly all of them require some degree of reviewing to ensure that the lines themselves are valuable and necessary.
Determining Lines of Code within Visual Studio
There are a few different methods of accessing this metric at the Solution, Project and individual file level and all of these greatly depend on the version of Visual Studio that you are running. I’ll detail each of the methods below so that you can find the version that suits your environment the best.
Visual Studio Premium & Ultimate Editions (2008, 2010, & 2012)
One of the key benefits of using a “Premium” Edition of Visual Studio provides you access to a special set of tools called Code Metrics, which can be used to generate a variety of different metrics regarding your application, such as complexity, external calls, and ultimately a line count.
The Code Metrics area can be found under the main menu within Visual Studio under the Analyze tab as seen below :
Code Metrics can be an easy method of accessing different metrics such as Line Count within Premium versions of Visual Studio.
After selecting the scope of where you would like to perform the selection on (either for the entire solution or a series of selected projects), you’ll notice that the Output area of Visual Studio should display the Code Metric Results, which will consist of several different categories which can be drilled down into for further analysis within your application.
The Results of a Code Metrics Analysis on a Project.
The last one of these however is the one that we are looking for “Lines of Code” :
Code Metrics easily reveals the number of lines within a Project or Solution.
Visual Studio Non-Premium Versions (Professional and Express)
This is the section where the real “trickery” mentioned in the name of the article comes into play.
These non-premium versions of Visual Studio lack tools such as Code Metrics to help determine the number of lines within a specific Project or Solution. However, we don’t need to fork over the cash to go and buy one of these tools, nor do you need to buy or download a third-party tool or extension.
Surprisingly, this can be accomplished through the use of Visual Studio’s Find and Replace tool and a few Regular Expressions :
Using the Find and Replace function within Visual Studio to match each line within a specific set of files.
That’s right – this technique is going to write a series of complex regular expressions that will actually match each line of code within your Visual Studio files and we will use these results to easily gauge the number of lines within an application through the following steps :
- Press CTRL+SHIFT+F to present the Find in Files prompt.
- Select Use Regular Expression under the Find Options section.
- Select the scope of your Search using the Look In drop-down.
- Further determine the type of files that you wish to search within the Look at these File Types box.
- Enter the one of the following Regular Expressions within the Find What box:
After running the expression that best suits your environment, you’ll find the following within your Output Window which details the number of lines of code based on your search :
The number of Matching Lines will reveal the number of Lines within your selected scope.
If you find that your results are heavily skewed or just don’t seem right, I would recommend checking the “Look at these File Types” box within the Find and Replace dialog and ensure that you aren’t targeting specific files that wouldn’t provide any useful information.
Reading Between the Lines.
Ultimately, it will be up to you to give any real value to the number of lines of code within your application (it may just be one of those things that becomes valuable as it decreases), but hopefully this is a neat little bit of regular expression magic that you get some use out of.