What Does Elegant Code Mean to Me?

I thought as my first official post on ElegantCode.com, it would be appropriate to talk about what I think defines elegant code.

Elegant is kind of an interesting word. Thinking about the word may evoke thoughts of black ties and French doilies, but it may also make you think about a modern building which looks simple and aesthetic on the outside, yet is built to withstand an earthquake.

It can be fairly difficult to describe elegance, but we know it when we see it.

But, if I had to describe it, how would I?

What is elegance?

Simply stated. Something that is simple yet effective, delivered with grace. Being simple is not enough. Simple lacking effectiveness is just a waste of time.

Simple - Not Effective

Simple, but not effective is a foam hammer. You don’t want your code to be like a foam hammer. If it can’t do what it supposed to do the simplicity just looks like stupidity instead.

Effective, but not simple is something that gets thrown away or lamented. It’s a Rube Goldberg machine.

Rube Goldberg Machine

You don’t want your code to be like a Rube Goldberg machine either. ?Where people look at your code because it is such a spectacle. ?You don’t want someone to look at your code and immediately think they want to kill you. ?Code that evokes murderous rage, tends to get blamed for anything that goes wrong in the system, even if it works perfectly.

If you can build something simple and its effective, it still has to be delivered in a graceful way. Consider, the elephant harness…

Simple and Effective - Not Graceful

Simple? ?Yes, it’s a box strapped on top of an Elephant, can’t get much more simple than that. ?Effective? ?Yes, looks to me like that elephant will get you where you need to go in that terrain. ?Delivered with grace? ?Hmm… ?Do you want to ride around on an elephant?

Even if you nail the first two, with your code, you should make sure it looks and reads good. ?No one wants to ride an elephant. ?I have seen more than one simple effective design get thrown away because the variables were named things like “cntr” and “banana”.

Elegant Code is James Bond

Women love him, men want to be like him and he kills people with a simple sophistication. When people read your code they should think James Bond, not Booger from Revenge of the Nerds.

James-Bond Vs. booger


Elegant code to me is code that reads well, does what is is supposed to do and is easy to maintain. Now that I have told you what I think elegant code is, in future posts I will talk more about how to actually write elegant code.

Side Note – Joining Elegant Code

First of all I wanted to publicly thank the other Elegant Coders for inviting me to join their community. I feel honored and esteemed to be joining such a great group of software craftsmen.

I also wanted to clear up confusion for anyone who has been following my blog at http://simpleprogrammer.com. ?I will still be blogging there, so don’t change your RSS feeds, but in addition I will be blogging here at ElegantCode.com. ?If you’ve come here form a link on my personal blog, I encourage you to check out the other excellent bloggers on this site and consider adding Elegant Code to your RSS feed reader. ??My personal blog will contain a wider array of topics, but my content here at ElegantCode.com will be centered around the theme of writing elegant code.

As always, feel free to email me at john *dot* sonmez *at* gmail.com or follow me on twitter.

One thought on “What Does Elegant Code Mean to Me?

  1. Had to smile when I read the blog and oh boy! I know what you mean.
    Even worse: doesn’t every one of us find some examples of this in code we once wrote ourselves? I’m not sure if James Bond is that elegant though. Right, James Bond would always do his job effectively and he looks great doing it, but he will almost certainly destroy one to thirty buildings to do his job. Calling James Bond to have him clean up your room means calling someone who would re-arrage your furniture with dynamite after shagging your wife, you don’t want that.
    Elegant code should be free of side effects, as much as possible.

Comments are closed.

Proudly powered by WordPress | Theme: Code Blog by Crimson Themes.