Friday, March 14, 2008

Favorite Code Today

I was most proud of this little gem today:

function addparams(query) {
return query.replace(/(&?)([^&=]+)=([^&]*)/g, function(m, amper, key, id) {
var val = document.getElementById(id).value;
return val.length > 0 ? amper + key + "=" + escape(val) : '';
});
}

It only ended up being six lines, but initially it was more and I like trimming code down more than making it bigger. The function itself is dead simple, nothing tricky or insightful here, but it's just downright pretty... even with the line noise regexp. Something about it just seems elegant and symmetrical. I don't even know how to describe how code can be pretty, but I would consider putting this on the wall in poster form.

So I applied for Startup School today. I figure I can waste some cash on a weekend in San Francisco to go meet some people and hear some hopefully inspiring speakers. Just thinking about it makes me look forward to this summer. I plan to be so nerdy.

2 comments:

Allan said...

Got a change for you. It'll be even more exciting!

Anonymous said...

no hable ingleis-- or java script either. i guess I'll stick with the blood, guts, and bodily functions.
But, i'm very impressed with the script, even if I'm totally illiterate.
Mum