News for hackers. Stuff that matters.
Re:Ask HN: The most fun and beautiful code you know of? by Xichekolas on Saturday May 23, 2009 @01:47AM (#623186)
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.

Re:Ask HN: The most fun and beautiful code you know of? by Xichekolas on Saturday May 23, 2009 @11:22AM (#623508)
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.

  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:

  <input type="text" id="first" value="defaultfirst"/>
  <input type="text" id="last" value="defaultlast"/>
Then we used it when doing some kind of ajax update. Assuming jQuery, (although that wasn't the case) something like this:

  $.ajax({
    url: "/path/to/page",
    data: addparams("fn=first&ln=last")
  });
Which would make an xhr GET request to:

  /path/to/page?fn=defaultfirst&ln=defaultlast
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.
Re:Ask HN: The most fun and beautiful code you know of? by RiderOfGiraffes on Saturday May 23, 2009 @04:50AM (#623277)
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/

Re:Ask HN: The most fun and beautiful code you know of? by voisine on Saturday May 23, 2009 @03:13AM (#623237)
UTF-8 encoding in 6 lines, from ezxml:

    if (c < 0x80) *(s++) = c; // US-ASCII subset
    else { // multi-byte UTF-8 sequence
        for (b = 0, d = c; d; d /= 2) b++; // number of bits in c
        b = (b - 2) / 5; // number of bytes in payload
        *(s++) = (0xFF << (7 - b)) | (c >> (6 * b)); // head
        while (b) *(s++) = 0x80 | ((c >> (6 * --b)) & 0x3F); // payload
    }
Re:Ask HN: The most fun and beautiful code you know of? by kmavm on Saturday May 23, 2009 @02:05AM (#623198)
The UNIX v6 kernel (Lions book). Every page brims with the excellent, hard tradeoffs required to shoehorn what we'd recognize as a modern system onto a 128K 16-bit machine.
Re:Ask HN: The most fun and beautiful code you know of? by Jeremysr on Saturday May 23, 2009 @01:23AM (#623173)
My own code...

(Not because it's perfect and beautiful, but just because I wrote it and understand it.)

Re:Ask HN: The most fun and beautiful code you know of? by edw519 on Saturday May 23, 2009 @09:44AM (#623431)
My own code that's common throughout all of my software.

Report writers, data base update objects, data formatting routines, form processors, string & array handlers, special logic algorithms, and especially, code generators.

The more I read these, the better I know them.

The better I know them, the easier to improve and extend them.

The more I extend them, the better my software gets and the less of it I have to write to get anything done. Leverage.

I literally have a folder of all this stuff on my nightstand or in my backpack all the time.

Re:Ask HN: The most fun and beautiful code you know of? by sauce71 on Saturday May 23, 2009 @03:33AM (#623244)
Duff's device. It opened up my eyes for the beauty of C and insanity of compilers: http://en.wikipedia.org/wiki/Duffs_device
Re:Ask HN: The most fun and beautiful code you know of? by dfranke on Saturday May 23, 2009 @01:46AM (#623184)
Usually I find API documentation more inspiring than actual code. It's higher-bandwidth. That said:

- Some bits of On Lisp

- Haskell's SYB and Parsec libraries

- MINIX (2; have yet to play with 3)

Re:Ask HN: The most fun and beautiful code you know of? by vivekamn on Saturday May 23, 2009 @02:53AM (#623227)
Solutions in Programming Pearls. Each solution is iteratively improved with the proper reasoning.

http://www.amazon.com/Programming-Pearls-2nd-ACM-Press/dp/02...

Re:Ask HN: The most fun and beautiful code you know of? by nailer on Saturday May 23, 2009 @05:36AM (#623293)
Anything that:

* Doesn't repeat itself. This includes languages which involve marking out blocks of code once for the interpreter and the second time for humans.

* Is short, because it doesn't reinvent common modules, and modularizes out general logic.

* Uses descriptive names, including using dictionary keys rather than vague list element numbers. A good test is if you can show the code to someone who doesn't work in computing and have them understand it. ZS had a talk on this where he showed lawyers SOx code and received feedback on the rules, changes to the order, etc.

* Uses tree structures for tree structured data (eg, etrees and xpath) rather than treating such data as strings and using RegExs.

* Has docstrings.

Re:Ask HN: The most fun and beautiful code you know of? by jmonegro on Saturday May 23, 2009 @01:58AM (#623193)
Shakespeare. It exists. Seriously. It's as beautiful as Shakespeare, but not very fun, sadly.

http://en.wikipedia.org/wiki/Shakespeare_(programming_langua...

Re:Ask HN: The most fun and beautiful code you know of? by spitfire on Saturday May 23, 2009 @01:30AM (#623177)
The art of computer programming.

Not because it's direct computer code, but because when I'm reading it past code i've written starts swirling in my head. Sort of one of those "ahhh fantastic" moments.

Re:Ask HN: The most fun and beautiful code you know of? by progLiker on Monday May 25, 2009 @10:15AM (#625464)
/* fun only / #const HellIsAHotAndFieryPlace=1 while (HellIsAHotAndFieryPlace) printf("The end is near\n") ;

/ yes it did make me laugh when I read it in Atari St user */

Re:Ask HN: The most fun and beautiful code you know of? by aristus on Saturday May 23, 2009 @01:44AM (#623182)
Very few people will get this reference, but when I had access I used to read the genweb3 code. It's like a whodunnit because there are no comments. :)
Re:Ask HN: The most fun and beautiful code you know of? by keefe on Saturday May 23, 2009 @02:16AM (#623206)
pseudocode all throughout cormen... I appreciate industrial code the same way I appreciate a ferrari - without looking at the engine
Re:Ask HN: The most fun and beautiful code you know of? by jmonegro on Saturday May 23, 2009 @02:00AM (#623194)
Shakespeare. It exists. Seriously. It's as beautiful as Shakespeare, but not very fun, sadly.

http://en.wikipedia.org/wiki/Shakespeare_(programming_langua...

Re:Ask HN: The most fun and beautiful code you know of? by known on Saturday May 23, 2009 @06:08AM (#623311)
Re:Ask HN: The most fun and beautiful code you know of? by abyssknight on Tuesday May 26, 2009 @04:19PM (#627433)
c99shell. It's a PHP trojan horse of sorts written by Russian hackers. Beautiful, rare, dangerous and awesome.
Re:Ask HN: The most fun and beautiful code you know of? by maurycy on Saturday May 23, 2009 @09:12AM (#623413)
OpenBSD, Django and code written by Niels Provos.
Re:Ask HN: The most fun and beautiful code you know of? by nostrademons on Saturday May 23, 2009 @01:40AM (#623181)
Google.