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.
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
, 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.
Let's not get all stilted here and call it a parser,
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 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.