April 2, 2007

Scrapheap challenge at SPA 2007

Nat Pryce and I ran scrapheap challenge again (our Postmodern programming workshop), this time at at SPA 2007.

Nat and I joined in with the three challenges - I hope Nat will write up the other two as he pretty much did them by himself while I drank tea - here's my write up of the one of them where I actually did something.

Slopography part 2

This challenge was to produce a tool that would show you whether you are making code sloppier as you work (compared to how the code was when you checked it out). This was the last challenge of the workshop and we had overrun slightly, so we had only around 30 minutes for it. As a result, we allowed a very simple interpretation of the measure of slop - a simple count of the number of occurences of the string "todo" was enough.

I've called this "part 2" because we did something a little bit similar at a previous scrapheap challenge - to produce a graph showing the sloppiness of code over time - e.g. by measuring the number of "todo" comments in code, by checking out code from a subversion repository one revision at a time. This was enough different so there was very little overlap - having a solution to that challenge wasn't much help for this one.

A solution (in 30 minutes)

Here's the code (in python) for it as typed in on the day - not cleaned up - it could be done more neatly and shorter:

import os, time

def slop(filename):
    f=open(filename)
    return f.read().lower().count('todo')

import sys
rootDir = sys.argv[1]

while(True):
    checkedInSlop = 0
    mySlop = 0

    for root, dirs, files in os.walk(rootDir):
        for fileName in files:
            fullFileName = os.path.join(root, fileName)
            slopcount = slop(fullFileName)
            if fileName.endswith('.svn-base'):
                checkedInSlop = checkedInSlop + slopcount
            else:
                mySlop = mySlop + slopcount

    f=open('slop.html','w')
    s = int(100 * float(mySlop) / float(checkedInSlop))
    if s <= 100:
        color = "#33ff33"
    else:
        color = "#ff3333"
    f.write("""<html>
    <HEAD>
    <META HTTP-EQUIV="Refresh" CONTENT="10">
    </HEAD><body>
    
    You are this sloppy: PERCENT%</body></html>""".replace("PERCENT",str(s)).
            replace("COLOR",color))
    f.close()

    time.sleep(10)

A couple of things about this solution

We just counted the number of occurrences of "todo" - Nat wanted to do a cleverer implementation, but we were rushing to complete the challenge, which included a big friendly display of whether the slop was increasing or not. We put completion of the whole solution, however poor the individual parts, ahead of doing any of the parts of the solution really well. We could return to the worst of the parts of the solution later if we had time, but it turned out that we only just completed this in the time.

For the display, we used a really simple and quick-to-implement solution. Our solution was to write an html file with a meta-refresh, and set the background red or green depending on whether the level of slop has gone up or down. This borrowed from how build-o-matic used to do its build results web page.

Yuk - that's quick and dirty

Possibly. But it does work.

Posted by ivan at April 2, 2007 6:30 AM
Copyright (c) 2004-2007 Ivan Moore
Comments

Yeah, that was dirty, but not enough. =)
How about this one for Windows. Bonus -- all todo locations. (No HTML report, but doable):

:over
@dir /s /b *.h *.cpp >files.lst
@dir /s /b *.svn-base >svn.lst
@findstr /n /f:files.lst /i /c:"todo" >myTodo.lines
@findstr /n /f:svn.lst /i /c:"todo" >svnTodo.lines
@perl -e "open(MY,$ARGV[0]);1 while<MY>; $my=$.;open(SVN,$ARGV[1]);1 while<SVN>;if($.){$s=100*$my/$.;print qq(You are this sloppy: ${s}%%\n)} else {print qq(just $my new todos\n);}" myTodo.lines svnTodo.lines
@perl -e "sleep(10)"
@goto over

Posted by: alexandroid at April 11, 2007 3:22 AM

It's a good measure of sloppiness and it could be improved by looking at which subversion user checked in the TODO and emailing them a message along the lines of:

I know you are enjoying working on that new feature that no-one asked for but if you wouldn't mind awfully coming back to File:Line 54 where you wrote TODO: (blah blah) and fixing that code that would be terribly nice of you. Thankyou.

Posted by: Chris Clarke at April 14, 2007 2:22 AM
Post a comment









Remember personal info?