Code is beautiful

I guess you'd call me a programmer. Personally, I'm not particularly comfortable with that title, because while I do know how to program I am convinced I'm not good at it. No matter how hard I try, there will always be someone who is better, more clever and faster than me. I also rarely program as a hobby, which tends to be the singular mark of a true programmer.

The thing is, I love code. I suspect I love code more than most of my peers. Code is poetry. It's creativity. Code tells a story, and a very complex one at that. Coding is a creative problem solving exercise. A finished program both describes and solves a problem: not only does it describe an algorithm - a recipe for solving a puzzle - but it writes it in a way that a machine built entirely out of principles of physics can actually execute. Good code can even make such a machine flexible and adaptable. Clever or interesting code brings me a thrill akin to that felt by people who enjoy poetry. It is accompanied by the rush of having learned or discovered something new - and appreciation of the design skills and intelligence of the author. In a world of complete imperfection, we have in our hands the ability to create things of near perfection. If one only cares enough, one can create error-free, beautiful programs...

Visually, code is beautiful to me. There is a wonderful symmetry in well formatted code, and when you add pretty syntax highlighting and a nice monospace font, things become... well, I would print and frame code if I had any space to hang it. The uniqueness of the programming design itself is further emphasized by the uniqueness of formatting. I once had a professor tell us that he can recognize when a student submits someone else's code, because the physical formatting of the code changes from previous submissions.

Many people barely format their code - "if it works," they say, "that's all that matters. Formatting is wasting time." Not so, I respond. Code is never eternal - it will need additions and bug fixes and updates. The people that come after you will need to follow and understand exactly what you've done - and clear formatting and comments will aid them in that task. Beauty in code does not exist for beauty's sake: pretty code is easier to understand, maintain and often is faster and more accurate than ugly code.


Formatting code shouldn't take long - it should become an ingrained habit of any programmer worth their salt, and it is certainly made much easier by the use of IDEs that handle things like indentation, comment blocks and even method headers for you. There really is no excuse to produce badly formatted code.

As poetry, code is like a haiku, or iambic pentameter: it has very heavy restrictions, and bad programmers fight these restrictions instead of flourishing within them. Good programmers dance elegantly through the keywords, libraries, paradigms and quirks of the language they're writing in. Good programmers explore every inch of their language, seeking to find gems of functionality. Tracing thoughtful code is an absolute pleasure - it is an intellectual exercise, filled with many "aha!" moments.

Just like with creative writing, each programmer has their own characteristic style. The lens through which they view the world becomes an integral part of their approach to problem solving. Some programmers are clever, others are clear. Some are dirty, others are arbiters of standards. Some programmers seek to squeeze every byte of efficiency out of a machine, while others are relaxed about resource use. A professor once told us that after years of experience tracing other's code, it becomes rather like reading a book: you stop focusing on the words and structure of the sentences, and instead focus on what they are saying. Good programmers get their message across clearly and effectively, just like good writers do. Beautiful code will have delightful concepts and clever solutions laying beneath the literal text of a program.

In the hands of a good programmer, any language can become beautiful. There are thousands of computer programming languages out there - those of broad use, some of specific use, some that seem to exist solely for the amusement of the language author. That's right, some programs and languages contain fair amounts of humour. A great example is LOLCODE. The following example program, in LOLCODE, opens a file and prints the contents, and includes rudimentary error handing:
HAI
 CAN HAS STDIO?
 PLZ OPEN FILE "LOLCATS.TXT"?
     AWSUM THX
         VISIBLE FILE
     O NOES
         INVISIBLE "ERROR!"
 KTHXBYE



Another rather amusing example of the... flavour of the programmer is the count of swear words present in the Linux kernel source as the system develops over time. I'm not sure which I like more: that there are over 200 instances of "shit" in the source code, [or almost a hundred of "penguin"], or that someone decided to count them all. Code can be fun, in addition to being beautiful - just like with any art.


Knowledge and beauty is all we have. Intelligence is beautiful. Thoughtful design is beautiful. Simple and elegant solutions to complex problems are beautiful. Code that doesn't lie - that presents itself clearly and truthfully is beautiful. Code is like poetry - and good code is as thrilling and stimulating as good poetry. It is one of my loves...

10 things about

Code is beautiful
  1. @implementation Comment

    - (void)commentView:(UIPCommentView *)commentView {

    NSString *commentText;

    commentText=[[NSString alloc] initWithFormat:@"LOVED this post. I'm kind of new to programming; I've only done it for a year. I do Objective-C by the way, just in case you couldn't tell :) I do enjoy how you can create your own software through code. Right now I have this iPhone app that I'm still developing but until it's complete it's something that's unique to me and only I can use. That automatically makes it awesome. The fact that I made this myself, makes it uber awesome (well to me anyway). I hope you enjoyed this rather quirky comment!"];

    [commentText release];

    //By the way I've awarded you in my blog :) Check out my latest post and experience it's amazingness!

    @end

    ReplyDelete
  2. Bio: There's some other great esoteric languages. There's one called "Chef" where the code is built like a recipe. ("Put cinnamon into 2nd mixing bowl") There's "Shakespeare", where it's all written like a Shakespeare play. There's "Whitespace" which has no text characters other than spaces, tabs and newlines... and many more languages, some as proof-of-concepts, some just for fun. It's a great field.

    Tom: Haha, when I first saw it in my email, I was like, "Oh no, Blogger shit itself." I like what you've done.

    I'm not familiar with objective C; I generally try to stay away from Apple products, haha. I've heard Obj-C is... an experience. :P

    ReplyDelete
  3. This was written with such passion...I feel as though I am missing out. Perhaps I should rectify that and learn me some programming :)

    ReplyDelete
  4. Lol my comment does look like Blogger turd :P

    I can't compare Objective C to anything as I haven't coded in any language, but I have tons of books to help me out. Apple makes it easier with drag and drop interface builders which means less coding on the design and more on the functionality which in my opinion is good. But I've read a lot of stories about people who have programmed for Java, moved onto Obj-C and found it really easy once they get around the concept of a slightly different language!

    So yeah, maybe everyone's a little different. Even my little comment would probably result in a crash somewhere if it was real :P

    ReplyDelete
  5. Haha sorry I just had to share this :) (You might think I'm totally off my trolley)

    If code is beautiful, algorithms are porn.

    I know how much you love por- algorithms :)

    ReplyDelete
  6. I don't know coding, but I can draw a lot of parallels between your feelings for it and my feelings towards architecture. There are definitely careers that I could have where I would be closer to the "best," but I just get such a creative-problem-solving high off of architecture that I have to set my pride aside and stick with it. MAN do I love combining creativity & logic.

    I also smiled at the similarities w/ architecture when you mentioned working with restrictions rather than fighting them, simple & elegant solutions, and [code/design] that presents itself clearly & truthfully.

    This was a fun read, amiga!

    ReplyDelete
  7. Diego: People who are passionate about something can make is seem so amazing... I've started considering moving to Newfoundland because someone I've been talking to the psst day is absolutely in love with the place, haha. I get excited about other people's hobbies and interests if they're passionate enough.

    Tom: the language you learn first has a big impact on how you look at other languages. I cut my teeth on Visual Basic and then Java, so I look at everything in terms of them [and now, C, because I love C so much]. That and, some people just don't have a mind for programming, no matter how hard they try - it's like, knowing the technicalities of how an instrument works doesn't mean you can play it well! I don't know where I'm going with this. Maybe... people with a programming mindset can program in any language?

    I like "algorithms are porn". I often confuse people by calling snippets of code, or hardware "Sexy", "hot", "cute" and "pretty". But seriously, a nice piece of hardware [like a macbook air] is... well, it's sexy to me! Beautiful code is erotic. :P

    April: I figured this would ring true with other professions. I think the people that are passionate about their field all feel the same way, albeit about different subjects. Appreciation for good design, a concept of technical "beauty"... we all see it!

    ReplyDelete
  8. The most visually pleasing code I've ever written is a septuply nested loop that constructs an advection matrix for a continuous Galerkin formulation of a 2-D wave equation. Beautiful, but sadly, all those loops run slow in Matlab.

    My favorite overall was also in Matlab and it was a vectorized version of nearly the same thing. No loops; the code itself is practically incomprehensible - even to me after spending some time away from it. God help me when I need to adapt those methods to another problem. But it is FAST and is perfectly amenable to use with parallel libraries that make it very very fast. I tried hard to format the code so it looked as pretty as the nested loop version but I'm resigned to the fact that it's aesthetic qualities can only be appreciated at run time.

    You're right: reading good code is like appreciating a fine piece of artwork, reading a beautiful paragraph, well-crafted sentence with all the perfectly chosen words. Except it's better because it is stripped of everything extra and unnecessary. It's like a sculpture.

    Messy, sloppy and wasteful code is like walking into a friend's house and seeing the bed unmade with dirty laundry and dishes strewn across the furniture. It gives me a headache and I get frustrated when someone asks for help debugging such code; as if they asked me to search through a pile of their dirty underwear to look for the half-eaten cake that is attracting all the insects.

    Great post.

    ReplyDelete
  9. Thank you, Stu!

    I noticed you use MATLAB frequently; I've only dabbled with it but everything I've seen so far has me quite impressed. It has some very sexy syntax.

    I'm totally with you on not recognizing your own code... sometimes you just don't have the time to clarify particularly obfuscated code, since it relies on clever tricks to work. There are some exceptions to the "beautiful code always runs better" idea - though let's keep it a secret... if you let people know it, they'll stop making an effort to write pretty code. ;)

    So far I've been fortunate that the only code I deal with on a regular basis is my own! But, one of my favourite things to do with code is to fix and polish it, so I think I'll be alright when I start maintaining others' work. I'm one of the few, it seems :P

    ReplyDelete

Copyright 2012 Phile not Found. See About
Powered by Blogger

"Whenever you find that you are on the side of the majority, it is time to pause and reflect."