Click here to Skip to main content
15,867,704 members
Articles / Web Development / ASP.NET

Line Counting Trickery within Visual Studio

Rate me:
Please Sign up or sign in to vote.
4.97/5 (16 votes)
10 Oct 2013CPOL5 min read 73.2K   25   5
This 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 USI.

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 is available from the Analysis section of the Main Menu.

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.

Code Metrics Results

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.

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 :

Matching Lines using a Regular Expression

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 :

  1. Press CTRL+SHIFT+F to present the Find in Files prompt.
  2. Select Use Regular Expression under the Find Options section.
  3. Select the scope of your Search using the Look In drop-down.
  4. Further determine the type of files that you wish to search within the Look at these File Types box.
  5. Enter the one of the following Regular Expressions within the Find What box:
    //Visual Studio 2012 Version
    ^(?([^\r\n])\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 :

Matching Lines

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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United States United States
An experienced Software Developer and Graphic Designer with an extensive knowledge of object-oriented programming, software architecture, design methodologies and database design principles. Specializing in Microsoft Technologies and focused on leveraging a strong technical background and a creative skill-set to create meaningful and successful applications.

Well versed in all aspects of the software development life-cycle and passionate about embracing emerging development technologies and standards, building intuitive interfaces and providing clean, maintainable solutions for even the most complex of problems.

Comments and Discussions

 
SuggestionWithout block chars Pin
Logman_PL26-Apr-18 6:51
Logman_PL26-Apr-18 6:51 
QuestionOr just use this PowerShell command line Pin
Sacha Barber29-Oct-15 23:49
Sacha Barber29-Oct-15 23:49 
GeneralHelped me, Thank you!! Pin
Hornwood50910-Jun-15 22:22
Hornwood50910-Jun-15 22:22 
QuestionThat was very neat... Pin
Nik Henry16-Aug-14 21:02
Nik Henry16-Aug-14 21:02 
QuestionLove that second one! Pin
MR_SAM_PIPER14-May-14 18:21
MR_SAM_PIPER14-May-14 18:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.