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.
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.
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)
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.
Possibly. But it does work.