The most famous written words are quotations; the author communicates in a sentence an idea that would take most of us an entire book, consequently quotations are memorised, regularly repeated, and generally held up as a pinnacle of composition. An equivalent in the programming world might be a hash table - undoubtedly a great deal of work is done behind the scenes, but a hash table exposes profound functionality with a trivial interface. Does the analogy run deeper though - the adjective beautiful is often applied to code, but does it make sense? is there an artistic merit in code beyond the efficiency and functionality of the resulting executable. Are hash tables beautiful?
The converse is clearly true; we've all seen ugly code. Poorly structured, poorly laid out, or worse spaghetti code is an eyesore as well as a nightmare for its maintainer. Well structured and elegantly formatted code is pleasing on the eye, but that doesn't qualify it to be beautifully written in the same sense as Steinbeck's novels are beautifully written. What does?
Not the programming language used, in fact the choice of programming language has no bearing on whether or not code is beautiful. Python's ordered whitespace appeals to my bauhaus sensibilities, and PERL source looks like a high level programming language disguising itself as machine code, but code is not beautiful because it is written in Haskell, and not all software written in Java is ugly. When a programmer talks about beautiful code, they are discussing the algorithm, not the syntax used to implement it.
Consideration of algorithms brings us to mathematics, another non-artistic pursuit in which elegance and beauty are frequent considerations. In A Mathematician's Apology Hardy observes that "Beauty is the first test: there is no permanent place in the world for ugly mathematics". He goes on to state that whereas a painter makes patterns with shapes, a mathematician does so with ideas. A coder must add an engineer's sensibilities to those ideas to transform them into software, but the link between the technical world and the artistic world is valid.
In Hackers and Painters, Paul Graham points out that both painting and hacking are about refinement. No piece of artwork is born in a day. X-Rays explicitly show how great painters often changed their minds during composition, and Kubrick once retook a shot 148 times before he was happy. I don't know how an artist refers to this process of refinement, but in the coding world we call it optimization.
A piece of artwork and a section of sourcecode are both built with a purpose. The hacker's goal is so concrete it is encapsulated in a unit test before work begins, whereas the artist sets out with the more ephemeral objective of capturing emotion. Nevertheless both parties proceed similarly, attacking their work as creatively as possible followed by a period of revision and refinement. Noone argues that the resulting artwork can possess beauty, so why not consider the code beautiful too?
Most likely the answer is that artists set out to inspire emotions in us, and we like emotions. The programmer who perfected an algorithm to calculate taxes had a much less glamorous task. However for me, as much a hacker's obsession with precision and performance may contrast with the artist's less definable goals, the artist doesn't have a monopoly on beauty. Hash tables are pretty too.
Posted on 02 May 2012
Slide to code blog is licensed under a Creative Commons Attribution 3.0 Unported License.
Based on a work at http://slidetocode.com/blog/.