Quickly count your code base

Often, the size of a code base is measured in terms of “source lines of code” (SLoC). If you’re interested in the size of your code base - or your client is - this metric provides a way to express that size. Of course, comments and the like are not considered to be code, so how to determine this metric? Using grep is tempting, but it quickly results in a very complex and hard-to-understand approach.

There is a simpler approach. First of all, install cloc for the platform of your choice. Start the tool and supply the directory that contains your source files, like so: $ cloc .. It will output something like

http://cloc.sourceforge.net v 1.65  T=0.57 s (166.5 files/s, 10491.0 lines/s)
-------------------------------------------------------------------------------
Language                     files          blank        comment           code
-------------------------------------------------------------------------------
XML                             85              1            275           5094
Java                             7             71            106            317
Maven                            1              5              0             93
YAML                             1              0              1             13
Bourne Shell                     1              2              0              8
-------------------------------------------------------------------------------
SUM:                            95             79            382           5525
-------------------------------------------------------------------------------

Nice! It evens splits code per language, including scripting and configuration files.

Combining reports

Sloc also has a nice option to combine multiple reports. Instead of printing the results to console, as we just saw, we use the --out paramter and run sloc for each directory we want to assess. This will result in a few files, which we can then combine using cloc --sum-reports --out=combined input1.txt input2.txt <etc>. Now, sloc will create two reports:

  1. combined-sloc.file which contains a report per folders
  2. combined-sloc.lang which reports over the total code base.

Pitfall using NPM or Bower

If you’re using dependecy managers like NPM or Bower, which install dependencies inside your working directory, sloc will count that code as well. So before starting sloc, make sure to clean the node_modules resp. bower_components folders. It will save you a slight heart attack.