Help me find more examples of Code as Poetry
April 5, 2005 4:28 AM   Subscribe

Inspired by this blog post about a particularly elegant single line of code, and thinking back to the golden-age brilliance of Duff's Device, does anyone else have good examples of code beauty? Are there any blogs out there that are essentially a reverse DailyWTF?
posted by Plutor to Computers & Internet (16 answers total) 1 user marked this as a favorite
sweetcode seems to be dead. I've thought quite a bit about making such a web site as my first venture into web stuff. One of the problems I see with picking out small bits of code for elegance is that truly elegant code (to my eye at least) seems to occur more often in languages that are out of the mainstream. There's currently a thread on lambda the ultimate titled of all things "Expressivity of "idiomatic C++" that is the type of discussion about code fragments that I'd like to see more of..

When I was thinking about this a few months ago I came up with a lot of questions. What is elegance? How can small bits of code be pulled from their context and still be beautiful since code generally needs lots of context to have meaning? How do I tell the difference between code that makes me think from code that is elegant?

You might want to check HACMEM for clever code or TeX for an example of a beautiful system built in a ugly language.
posted by rdr at 5:00 AM on April 5, 2005

this is quicksort in haskell:
  sort :: (Ord a)   => [a] -> [a]
sort [] = []
sort (pivot:rest) = sort [y | y <- rest, y < pivot]
++ [pivot] ++
sort [y | y <- rest, y >= pivot]
the first link (to wikipedia) will show you a variety of ugly versions in other languages.
however, all is not perfect - this implementation is less efficient than most because the array is not updated in-place (in fact, this constructs a new list from the contents of an old one).
note that this code will sort any type that can be ordered (any member of the Ord class) and the compiler will give an error otherwise. in other words, the code is generic, but correctly (strong/static) typed.
(and, obviously, you could just call a library sort function instead, but this is a classic algorithm that is generally not that easy to code elegantly).

i don't know of any good general sites. for cross-language comparison, there's the 99 bottles of beer pages;sweetcode was more about "whole programs" [on preview - i guess i better post this before people list my other sites too!]; and ntk once had a spin-off that listed useful code fragments (but that seems to have gone completely).
posted by andrew cooke at 5:02 AM on April 5, 2005

Lots of good stuff here. For example, Lambda expressions in C++.
posted by Succa at 8:02 AM on April 5, 2005

Hell damn and blast!
Blood, pus, boils, and pox upon programmers who think they're elegant, when they're really being clever.

If you want to iterate from n-1 to 0, there's an easy way to do that:
for (x = n-1; x >= 0; x--)

The code cited as "elegant" is bad bad BAD for at least two distinct reasons
1) It does something easy and standard in a non-easy and non-standard way
2) It contains bits that look like they're doing something, but they're not. Namely the third, unused term. The post claims that this "serves as more documentation" in fact this is anti-documentation, it serves to distract the reader from what's actually going on, not illuminate it

Code is written for people to read. If you're doing something easy and normal like iterating from n-1 to 0, and you decide to show the world how heaven-blessed-clever you are by doing it in a stupid way like this, and I have to maintain this code after you, I'm going to come to your desk and punch you.
posted by Capn at 8:44 AM on April 5, 2005

i (heart) python
def solve(eq,var='x'): 
    c = eval(eq.replace("=","-(")+")",{var:1j}) 
    return -c.real/c.imag 
>>> solve("x - 2*x + 5*x - 46*(235-24) = x + 2") 

posted by Loser at 9:01 AM on April 5, 2005

Oleg's site is incredible. If you like that, you'd probably like Henry Baker, too, although you'll have to read his papers to find the gems.

In the perl source, in toke.c, function scan_str, there's this gorgeous bit that floored me the first time I saw it:

/* find corresponding closing delimiter */
if (term && (tmps = strchr("([{< )]}> )]}>",term)))
term = tmps[5];

There's a nice restructuring of some poor code here.
posted by sonofsamiam at 9:03 AM on April 5, 2005

Oh Capn, my Capn, I do agree with thee!
Elegance is something
anyone can see

posted by yerfatma at 9:35 AM on April 5, 2005

I remain blown away by this bit of Perl to print all strings of n pairs of parentheses that are validly matched (which is isomorphic to a very large number of problems.)

#!/usr/bin/perl -l

print $_ = "()" x shift;
print while s{^ ( \(+ ) ( \)+ ) \( }
{"()" x (length($2) - 1)
. "(" x (length($1) - length($2) + 2)
. ")"

(Yes, it's hard to read, and it's not intuitive what it does... but so were all the much longer solutions to this problem -- if you can do better in any language, I'd love to see it.)
posted by Zed_Lopez at 9:37 AM on April 5, 2005

Perl one-liners: elegant or just clever? Here's how I fold a file so that every set of 10 lines becomes one tab-separated line:

perl -pl -i.old -e '$\ = $. % 10 ? "\t" : "\n"' inputfile.txt
posted by nicwolff at 9:45 AM on April 5, 2005

I don't think there's anything elegant in that blog post. Quite the contrary. If you want more of the same, then start reading Perl newsgroups.
posted by grouse at 10:09 AM on April 5, 2005

Note well, too, that that guy's solution could have easily been (maybe was) done by accidentally inverting the latter two premises to a for construct, and it worked anyway.
posted by abcde at 10:32 AM on April 5, 2005

There's a lot of cool perl stuff over on perlmonks, but not a DailyGoodWtf.
posted by callmejay at 12:34 PM on April 5, 2005

self link, but you may be interested, since your title is "code as poetry" - i once wondered about code as file reviews. that link summarises people's responses, which included some examples of "good code".
posted by andrew cooke at 12:54 PM on April 5, 2005

I have to agree with Capn: that code sucks.
posted by delmoi at 1:01 PM on April 5, 2005

The problem with code that looks nice is that it's often hideously inefficient. Recursion looks nice, but consumes RAM like it's infinite. (It's also a bastard to debug.)

Truely eligant code is code that can achieve what it needs to with the least amount of resources.
posted by krisjohn at 5:41 PM on April 5, 2005

no. what you aren't used to, and what isn't suitable for the language you're using, is bad. recursion is just fine if you know what you're doing and the language supports tail calls.

truly elegant code does what it says on the can and doesn't slow down the main loop appreciably. if it's called once every other month i don't give a damn how efficient it is - but i do care that it works, because it's unlikely to be as well tested as more popular code.

context is everything. people complaining about recursion becuase it's outside their experience are nothing.
posted by andrew cooke at 6:03 PM on April 5, 2005

« Older How can I see incoming connections on my Mac?   |   How to put in a starting bid on a house Newer »
This thread is closed to new comments.