Home > Uncategorized > Measure your code

Measure your code

I found a cool tool today to measure code: metrics. It measures SLOC, comments and cyclomatic complexity. It’s easy to install: pip install pygments metrics

Run this in your project’s root:

metrics -v `find . -type f \( -iname "*.css" -or -iname "*.py" -or -iname "*.js" -or -iname "*.html" -or -iname "*.txt" \) \! -path "*/migrations/*.py" -print`

I have a django project so I added \! -path "*/migrations/*.py" to skip any files that are in a migrations dir (I’d skip the automatically generated south migrations).

You probably bundle other libraries or apps in your source tree (eg: jquery or that nice django app the author didn’t bother to make a setup.py script for) so you want to measure only some specific paths. Eg, to collect stats only for files in src/foobar, lib/tools and src/otherbar:

metrics -v `find src/foobar lib/tools src/otherbar -type f \( -iname "*.css" -or -iname "*.py" -or -iname "*.js" -or -iname "*.html" -or -iname "*.txt" \) \! -path "*/migrations/*.py" -print`

If you work on multiple projects you can make a script or alias for this:

metrics -v `find \`cat METRICS\` -type f \( -iname "*.css" -or -iname "*.py" -or -iname "*.js" -or -iname "*.html" -or -iname "*.txt" \) \! -path "*/migrations/*.py" -print`

And in each project just save a METRICS file with the list of paths.

I get something like this for one random project:

Metrics Summary:
Files                       Language        SLOC Comment McCabe
----- ------------------------------ ----------- ------- ------
  129                         Python        4831     289    261
    2                      Text only           0       0      0
   49              HTML+Django/Jinja        1381      19    166
    7                     JavaScript        2204     231    352
   21                            CSS        1839     111      0
----- ------------------------------ ----------- ------- ------
  208                          Total       10255     650    779

Do McCabe (aka cyclomatic complexity) ratios (McCabe/(SLOC-Comment)) look odd ? (0.17 for javascript and and 0.05 for python).

Do you know other measurement tools adequate for Django projects ? Do tell me and, if possible, how to run it for specific files like above (I’m lazy :)

Advertisement
  1. July 3, 2011 at 2:03 am | #1

    uhm sloccount?

  2. July 3, 2011 at 5:42 am | #2

    Cool little utility! :) I’ll add this to my bag
    of tools!

    Metrics Summary:
    Files Language SLOC Comment McCabe
    —– —————————— ———– ——- ——
    70 Python 5975 2080 1128
    —– —————————— ———– ——- ——
    70 Total 5975 2080 1128

    ^^^ circuits :)

    @netblockalin: SLOCCOUNT is Source Lines Of Code Count

    –JamesMills / prologic

  3. July 3, 2011 at 9:43 am | #4

    Wonderful, keep it up thanks.

  4. July 3, 2011 at 11:51 am | #5

    Thank you..really informative!!

  5. July 3, 2011 at 6:46 pm | #6

    This looks really cool. Also, it reminds me that I keep wishing I knew a tool to draw diagrams of my modules interdependencies. Is there such a thing, or should I be thinking about whipping up my own, maybe my examining static imports and producing output for graphvis or somesuch?

  6. July 3, 2011 at 7:46 pm | #7

    I’m second to none in trumpeting all things Python, but cloc is awesome: http://cloc.sourceforge.net/. I humbly suggest that it’s the gold standard for this kind of utility.

  7. July 3, 2011 at 11:49 pm | #8

    For the command line, you can simplify and avoid quoting issues if you use a pipe and xargs. The -print0 option for find and the -0 option for xargs are useful. Equivalent to your first one:

    find . -type f \( -iname “*.css” -or -iname “*.py” -or -iname “*.js” -or -iname “*.html” -or -iname “*.txt” \) \! -path “*/migrations/*.py” -print0 | xargs -0 metrics -v

    This is much more unixy, and will also deal with ‘funny’ filenames like those with spaces in them.

  1. July 13, 2011 at 12:32 pm | #1

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 155 other followers