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, 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.
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? ?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.
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.