Often times, we find ourselves designing throwaway code which only fulfills a one-time requirement. The following piece of coding is one such piece of code.
What it is designed to do is generate an actual count of lines of code within an SQL file (it can be modified accordingly to work with other language files).
In order to do this, the code iterates through the lines of text and skips counting on empty lines, as well as comment blocks.
private static int GetActualLineCount(string filename)
TextReader tr = new StreamReader(filename);
string line = String.Empty;
int actualLineCount = 0;
bool commentedLine = false;
while ((line = tr.ReadLine()) != null)
if (line.StartsWith("/*")) commentedLine = true;
if (line.StartsWith("--") || line.Trim() == String.Empty) continue;
if (line.EndsWith("*/")) commentedLine = false;
As it turns out, the 177,251 lines of code file I scanned has 154,109 lines of actual code. Handy!