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. 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:

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:

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.

Share

About the Author

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.

I downloaded and compiled this and when you click on the Search folders button,
a dialog box shows up but with no ability to navigate to anywhere -- This
might be a starting point for something, however I got the idea the
source code was complete.

The app looks good, but I got an exception while scrolling through the file names when running it on a big project (5,175 cs files). I haven't tried debugging yet, but thought you may have some ideas as to the problem. Here's the exception text: