Click here to Skip to main content
Click here to Skip to main content
Go to top

Simple Source Line Counter in C# for C#

, 24 Jan 2005
Rate this:
Please Sign up or sign in to vote.
A simple source code line counter written in C#.

A Simple C# Line Counter

What is it?

A stand-alone utility that counts the lines of code in C# source files.

My searches on CP for source code line counters yielded some nice alternatives. However, I wanted something that was written in C# and did not install into Visual Studio. The only "integration" I was looking for was of the manual "External Tools" variety. I did not find a line counter that met these criteria, so I coded one up.

Directions

Simply unzip and run the executable. Select the directory where the source files reside (all subdirectories will be searched). Click the "Go" button to start the counting process. Only files with a ".cs" extension will be considered.

The goal for this counter was simplicity. In other words, just count the lines of code in the project. I didn't care about comment-to-source ratios or how much white space had been used - after all, we already know our source is pristine, right? We just need to find out how much of this wonderful stuff we've got. Wink | ;) In the pursuit of not driving myself totally nuts with this thing, I did put in two features. One, the application remembers the last directory selected (if it still exists). And two, the size and location of the form is restored (if the SystemInformation.WorkingArea is large enough to accommodate the previous settings).

What exactly is a line of code?

I'm glad you asked. Remember, this counter is simple. If a source code line trims down to nothing, it doesn't count. If, after being trimmed down, a line starts with #region, #endregion, or //, then it doesn't count. Lines which start with source code and end with a comment do count. For example, if a /* style comment starts halfway through a line, that line is counted. All subsequent lines up to the end comment */ are not counted. XML style comments also do not count. Now, can you defeat these little schemes and get some of your comments counted? Sure. In fact, you can count every line in the file as source if you like. What better way to get your LOC rating up to 5,000 a day, eh? In order to change the line counting behavior, we simply replace the brain of the counter.

Brain Surgery

Let's not get all stilted here and call it a parser, String.IndexOf(); and String.StartsWith(); do not make a parser. However, the counter definitely has to do something (if only a little something) to figure out whether or not a given line is really source. This all important function is the responsibility of the Brain class. In the constructor of the LineCounter class, you'll find this bit of code:

CodeFileModel _Model = new CodeFileModel();

Brain simpleton = new Simpleton();

_Model.Brain = simpleton;

Now the implementation of Simpleton, is, well, simple. He enforces all of the rules outlined above with slavish devotion. We can change his behavior though, by setting Boolean properties that represent the type of line we want to include in the count:

simpleton.CountBlankLines = true;
simpleton.CountRegions = true;
simpleton.CountSingleLineComments = true;
simpleton.CountMultiLineComments = true;
simpleton.CountXMLSummaries = true;

With this configuration, Simpleton would count all of the lines as source. Alternatively, you could implement a whole new Brain descendant. Brain contains only one function IsSource which takes a single line as a parameter and returns a Boolean indicating whether or not the line should be counted.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Tom Ollar
CEO Sagerion, LLC
United States United States
I read About Face by Alan Cooper in 1995 and immediately recognized it as a founding document for the future of software. I also recognized we had a long, long way to go - and yes, even with the advent of iOS, we are still not there yet.
 
At my company, Sagerion (say-jair-ee-on), we can take a look at your planned or existing software and suggest ways of making it better - lots better. We can develop down-to-the-pixel blueprints showing exactly what our suggestions mean. We can help manage on-going development to make sure the top-notch user-experience we've suggested really does get built. Now, honestly, how often have you ever seen all those things happen?
 
You may or may not already have great development going on - but what does that matter if you don't have great design driving it?
 
Feel free to contact me at tom@sagerion.com, I would love to hear about your next ground-breaking project.

Comments and Discussions

 
GeneralMy vote of 2 Pinmemberkuratowski11-Mar-10 2:59 

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

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

| Advertise | Privacy | Mobile
Web03 | 2.8.140905.1 | Last Updated 24 Jan 2005
Article Copyright 2005 by Tom Ollar
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid