Funny seeing this, because I was at my local Borders earlier and decided to give that Beautiful Code book a shot. I've seen it on the shelf for quite a while, but those books don't tend to turn my crank, so to speak, so I had never before really considered it.
I only read the first and the third essay, but I must say, wow!
The third essay, for instance, takes a simple 12 line implementation of quicksort in C (which is already quite beautiful), and through a series of well explained transformations, arrives at a four line proof (as in, it's C code that is equivalent to the recurrence relation, and eventually the summation, we all solved in our CS classes) of the average case complexity (~1.4nlgn comparisons) of the quicksort he started with.
That is what I would consider "nightstand" code, or at least a nightstand essay about code. It was actually entertaining, to the point that you don't realize you are solving a recurrence relation in C form until the end, when the parallel becomes obvious.
I only had a chance to skim the other topics, but I'm actually considering buying it, because it really was a blast to read.
Definitely not _the_ most beautiful code I've written, but I still enjoy this little function for some reason. It's just pretty to me.
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) : '';
});
}
All it does is take a query string template of sorts, grab the values to plug in, and then return an actual query string. For instance, if you had this on your page somewhere:
Nothing remotely complicated or amazing like the regex that finds prime numbers or the 12 line quicksort, but I just think the function is aesthetically pleasing.
Comments
Funny seeing this, because I was at my local Borders earlier and decided to give that Beautiful Code book a shot. I've seen it on the shelf for quite a while, but those books don't tend to turn my crank, so to speak, so I had never before really considered it.
I only read the first and the third essay, but I must say, wow!
The third essay, for instance, takes a simple 12 line implementation of quicksort in C (which is already quite beautiful), and through a series of well explained transformations, arrives at a four line proof (as in, it's C code that is equivalent to the recurrence relation, and eventually the summation, we all solved in our CS classes) of the average case complexity (~1.4nlgn comparisons) of the quicksort he started with.
That is what I would consider "nightstand" code, or at least a nightstand essay about code. It was actually entertaining, to the point that you don't realize you are solving a recurrence relation in C form until the end, when the parallel becomes obvious.
I only had a chance to skim the other topics, but I'm actually considering buying it, because it really was a blast to read.
Oh and to post some actual code...
Definitely not _the_ most beautiful code I've written, but I still enjoy this little function for some reason. It's just pretty to me.
All it does is take a query string template of sorts, grab the values to plug in, and then return an actual query string. For instance, if you had this on your page somewhere: Then we used it when doing some kind of ajax update. Assuming jQuery, (although that wasn't the case) something like this: Which would make an xhr GET request to: Nothing remotely complicated or amazing like the regex that finds prime numbers or the 12 line quicksort, but I just think the function is aesthetically pleasing.I think it's been posted before, but it's relevant. You can see Jon Bentley give a talk on that here:
http://www.catonmat.net/blog/three-beautiful-quicksorts/