Skip to content

Instantly share code, notes, and snippets.

@andrewvc
Created July 23, 2012 04:19
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save andrewvc/3161953 to your computer and use it in GitHub Desktop.
Save andrewvc/3161953 to your computer and use it in GitHub Desktop.
Counting SLOC in clojure is pretty easy since the syntax is so simple.
# Count SLOC
export SLF=`mktemp -t cljsloc`; find src test -name "*.clj" | xargs egrep -v "(^[[:space:]]*$|^[[:space:]]*;)" | cut -d: -f1 > $SLF && echo "Files"; uniq -c $SLF; echo "Total" `cat $SLF | wc -l`; rm $SLF
@mdolidon
Copy link

Hello. This is elegant and quite an eye opener on the power of the Unix shell, and certainly should work for all languages that introduce every comment line with a special character, like Python with #.
Unfortunately in the case of Clojure this snippet also counts docstrings as code lines, and their amount is usually significant, as is their importance in code documentation quality. For instance, in Clojure's own source tree, over 2700 lines of docstring are more numerous than the 2470 blank lines that this expression filters out, and much more so than the 913 comment lines.

If a sloc measurement is actually needed to give an idea of code/comment ratio, I may suggest something more accurate though less gracious, and given that the lines have already been written, it shouldn't be a problem that there are many of them : https://github.com/mdolidon/clj-sloc
Regards.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment