LOC Counter

Applicaton Kata „LOC Counter“

Write an application to count Lines of Code (LOC) in source files.[1] The program should be started like this:

C:\> loc c:\myproject
10 source files found with 1478 lines of code
C:\> loc program.cs adapter.cs frontend.cs
3 source files found with 426 lines of code
C:\>

The files to check are either given as command line parameters (e.g. program.cs) or can be found in a folder hierarchy passed to the program (e.g. c:\myproject).

Only non-comment and non-whitespace lines must be counted:

  • Comment line:
    • A line with a single line comment and only whitespace to the left of fit, e.g.
      // comment line starting at beginning of line
      // comment line with whitespace before comment symbol
      var i = 1; // non-comment line
    • A line where a multi-line comment starts and only whitespace to the left of it, e.g.
      /* comment starting at beginning of line
      /* comment starting after some whitespace
      var i = 1; /* non-comment line
    • A line where a multi-line comment ends and only whitespace to the right o fit, e.g.
      comment line with whitespace after closing symbol */
      non-comment line */ var i = 1;
    • A line fully within a multi-line comment
      /* comment line */
      var i = 1; /* non-comment line
      comment line in multi line comment
      non-comment line */ i = i +1;
    • Whitespace line: A line with no characters in it or just space and/or tab characters.

Assume non-nesting multi-line comments.

Source files are either given as command line parameters or are recognized by the file extension .cs while crawling a folder hierarchy.

Variations

Multi-line comments can be nested.

[1] Choose a programming languages as you like. In this document C# is assumed. Programming languages have different symbols for single line comments, starting/ending multi-line comments, and usually require specific source file extensions.