Line Counting Trickery within Visual Studio
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 (e.g. 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 (only available in Premium versions) 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 :
- 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, its 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 :
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 last column that appears in the results will indicate the number of lines of code, which is what we are looking for:
Visual Studio Non-Premium Versions (e.g. Professional and Express)
This is the section where the real "trickery" mentioned in the title of this 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 :
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:
// Visual Studio 2012 Version
^(?([^rn])s)*[^s+?/]+[^n]*$
// Versions prior to 2012
^:b*[^:b#/]+.*$
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 :
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.