How to count Lines of Code (LOC) using grep and wc

Contrary to the CLOC tool (http://cloc.sourceforge.net) I argue that comments in the code are useful and should be counted in the “real” lines of code. I think that this gives a nice incentive to developers for commenting their code. Of course that holds assuming that you are not trying to fool anyone by artificially increasing the lines of your code. If your are trying to fool people, you can still do it with comments being removed (but I’m not going to tell you how).

So, the command I use on Linux to count code lines in C is: grep -v "^$" *.[ch] | grep -v "^[ ]*$" | wc -l (NOTE: the two characters in [] are Space and Tab - you may need to press Ctrl-V to write Tab on Linux). For other programming languages just change the file extension (e.g. *.cc, *.h, or *.cpp for C++, *.py for Python).

It’s often convenient to write a makefile target for measuring code lines, so that you can get the numbers by a simple “make loc” command. You can do that by adding the following lines to your makefile(s):

loc: [Tab]@echo -n "Code lines (excl. blank lines): " [Tab]@grep -v "^$$" *.[ch] | grep -v "^[ ]*$$" | wc -l NOTE: the characters before @echo and @grep are Tabs, and there’s a Space and a Tab inside [].

Enjoy!

[This post has been mirrored on my posterous blog Gnosis]