Same post but with embedded styles.
I'm trying to get a decent solution to inserting code in blog articles so the next few posts are all about the code. The sample below is a simple line counter tool I use to work out some metrics for code reviews.
using System; using System.Collections.Generic;using System.Text;using System.IO;namespace LineCount{ class Program { private static int folders; private static int files; private static int lines; private static int code; private static int comments; static void Main(string[] args) { if (args.Length == 0) { Console.WriteLine(@"Usage: linecount <root folder>e.g. linecount t:\dev\projects\mainNB: Only .cs files are considered Review time is hard coded to 200 lines per hour"); return; } CountLines(new DirectoryInfo(args[0])); Console.WriteLine("FOLDERS:{0} FILES:{1} LOC:{2} SLOC:{3} COMMENTS:{4}", folders, files, lines, code, comments); Console.WriteLine("Code density: {0:p}", (double)code / (double)lines); Console.WriteLine("Comment density: {0:p}", (double)comments / (double)lines); Console.WriteLine("Comment ratio: {0:p}", (double)comments / (double)code); Console.WriteLine("Estimated review time: {0}h", 1+ (code / 200)); } private static void CountLines(DirectoryInfo parent) { ++folders; foreach (DirectoryInfo folder in parent.GetDirectories()) { CountLines(folder); } foreach (FileInfo file in parent.GetFiles()) { CountLines(file); } } private static void CountLines(FileInfo file) { switch (file.Extension) { case ".cs": break; default: return; } ++files; using (StreamReader reader = file.OpenText()) { string line = null; bool inComment = false; while ((line = reader.ReadLine()) != null) { ++lines; line = line.Trim(); if (line.Length == 0) continue; if (line.StartsWith("//")) { if (line.Replace("//", "").Trim().Length > 0) ++comments; continue; } if (line.StartsWith("/*")) ++comments; else if (!inComment) ++code; if (line.Contains("/*")) { inComment = true; } if (inComment && line.Contains("*/")) { inComment = false; } } } } }}
Page rendered at Saturday, February 04, 2012 2:53:01 PM (GMT Standard Time, UTC+00:00)
Disclaimer The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.