I received a couple of offline responses to my last blog entry with the general question of “What is elegant and inelegant code?”
Elegant code to me is beautiful/logical/intuitive/smooth code, inelegant code is “ugly” or rough code. Elegant code isn’t necessarily “pretty” but is a combination of several things that make it “right” and beautiful in its own right. I don’t believe beauty is dependent on something being pretty but it can contribute to it. Certainly something pretty can also be quite ugly to me, say the code looks gorgeous and fancy on the screen but then you start to trace through it and realize it has horrible flow and is very difficult to understand and completely counter-intuitive; it may be pretty but it certainly isn’t beautiful/elegant. To give an analogy more people would understand, say you meet an absolutely stunning looking person (guy or girl), but that person is vapid/dense or kicks kittens and puppies or something like that… the looks become the least of what you see. Beauty in code is just like it is for other things in real life, completely in the eye of the beholder. What I consider beautiful or elegant code may not strike another coder the same way.
So what are some features I think of in elegant code?
1. A clean smooth flow which makes the code easy to follow and understand.
2. Minimal efficient code to accomplish the goal but still adhering to #1
3. Flexible such that if there is a change in what is needed, the code requires minimal modifications to account for the change.
4. Something that I can’t describe but know when I see or feel it. A certain je ne sais quoi. It just feels right, you know what I mean? 🙂
Â
Most code starts out as ugly code unless you were inspired when you created it. It is like any other art where the first thing that hits the paper often isn’t amazing, it is a framework that you flesh out and strive to make elegant. I will usually write the initial ugly code, run tests through it for a while to make sure the logic stands up and then toss it out and start over again to see if the second pass finds me more inspired. Sometimes even that doesn’t do it and I get stuck with ugly code until maybe one day I suddenly see the David in the stone and get inspiration and tear out and replace it or rewrite to get even more flexibility or handle a case I didn’t previously think of.
  joe
Â
An elegant piece of code is something like a beautiful proof in mathematics. It leaves the reader saying “of course – how could you do it any other way?” [This was my problem in college when submitting coding assignments – you had to document approaches you had tried and discarded, and for most of the assignments, there was a really obvious ‘right way’, and so there were no wrong ways to discard and document.]
In some cases, it takes more to document an elegant piece of code than it does to write it – because people will look at the length of the code, and assume it can’t be complete, so you have to re-assure them that it does indeed cover all cases.
In most cases, however, elegant code is elegant because it is clear and obvious, and it works _with_ the APIs and OS, rather than fighting to get stuff done.