views:

429

answers:

17

The question of 'what is elegance?' has been asked before, and rather than arguing about that, I think we could probably all agree we know it when we see it.

But I'd like to know is elegance a goal of programming (an essential aim), or merely a desirable side-effect? (or technique)

If we create code which isn't elegant, have we failed?

+7  A: 

For me, the goal of programming is to produce maintainable code that works.

Now, maintainable means that the code should be easy to change over time (hence should be easily understandable) and should be easily testable.

If this goal is achieved, i believe that the code will be elegant and beautiful.

Jeff Atwood thinks that

"Ideas are beautiful. Algorithms are beautiful. Well executed ideas and algorithms are even more beautiful. But the code itself is not beautiful."

Anirudh
I agree ... and I would have agreed even if you hadn't invoked Jeff Atwood's name. +1
pavium
BTW, thanks Anirudh, for the linked quote from Mr Atwood. Having said 'code itself is not beautiful, he went on to say: "The beauty of code lies in the architecture, the ideas, the grander algorithms and strategies that code represents."
pavium
+4  A: 

Perhaps those who have answered before me work in the arena of academia, but in my opinion the purpose of code is to provide your users with useful software that makes their lives better or easier in some way. Beyond that, yes, elegant code is great, and it is something to strive for, but it is not the end goal and you have not failed if you wrote a tangle mess of code that solves a problem in its domain and provides its users with a good experience doing so.

Obviously code quality is important for may reasons that others have touched on. You can't claim competence in your field if no one can understand or modify the fragile code that you have glued together. However, it is not the single most important aspect of our jobs, and anyone who thinks that it is the end-game in the real world is mistaken.

Ed Swangren
Ironically, I actually DO find myself in the situation where no-one else can understand or modify the fagile code that I have glued together ... not because its elegance defeats comprehension, but because no-one else in my organisation is qualified or inclined to understand it. So long as it appears to work, management is satisfied. As for user experience, so long as a result takes only a few seconds, everyone's happy.
pavium
+2  A: 

If we create code which isn't elegant, have we failed?

Mostly yes, and partly no.

Most of the times, code becomes ugly once we start peeking into the internals of the framework/language (which is occasionally necessary to build additional functionalities into a language or to build libraries that provide such low level functions). Hence, code that is not entirely elegant does not constitute a failure, when compared to its importance.

Alan Haggai Alavi
Yes, I agree. I have seen some buggy 'elegant' code. After adding a few 'if' statements, a workaround, and possibly even a cludge to fix 10 bugs it is no longer elegant, but it works.
Ed Swangren
I disagree, it might still work with ugly patches. But for very new ugly patch you add the next bug-fix or feature will be even harder to make. Never allow your code to deteriorate in the first place.
PeyloW
Right, but keep the original question in mind. If you release software that includes some ugly patches of code that also solves the needs of your clients, you have succeeded.
Ed Swangren
Put it this way; do you really think that the code which powers your computer's hardware, as well as the underlying OS, is all "elegant"? I doubt it, but would you say that these developers have "failed"? I doubt that as well.
Ed Swangren
+15  A: 
Traveling Tech Guy
A: 

If it does the job, and has no bugs, then imo, screw elegance...

baeltazor
Have you ever tried to maintain code made by someone else? You would appreciate some elegance...
Joonas Pulakka
I have had to maintain code created by predecessors (in fact, I asked a question about it a few days ago). The conclusion I reached with some excellent advice from others, was that "If it ain't broke, don't fix it." Baeltazor is right ... about maintaining other's code.
pavium
@baeltazor - I would never hire / recommend hiring someone with that attitude.
Stephen C
@Stephen C. Why's that? I'm interested in hearing your opinion. The way I see it is that Elegance is just a personal preference...
baeltazor
+7  A: 

If the code is not elegant but works, have you failed: No

The answer is No.

Its just a question that will start an argument. Like if you paint a picture, if its not a work of art, have you failed?

Mark Redman
I do belive in writing elegant code. However its not a "failure" if its isnt and the code works, just answering the question.
Mark Redman
Thanks Mark, I agree. +1
pavium
+4  A: 

Elegant code is a "nice to have".

It shouldn't be a goal in of itself. The most elegant code on the planet isn't really worth much if the application never gets released because you went out of business building it. Or if you created a product that looks amazing under the covers but doesn't provide anything that anyone wants to use. Or if your clients walk out on you because you keep refactoring instead of giving them the features they want/need.

Obviously, we'd all rather have elegant code. But success is measured by totally different metrics in my opinion.

TM
I disagree. The minute you loose tidying up your code now, is hours gained when you later on try to bug-fix or add features to it. Elegant code is a must if you want to stay as productive as the first day of the project, and deliver on time. Anything else is fooling yourself.
PeyloW
@PeyloW Code doesn't have to be elegant to be maintainable. Often times, simple, "dumb", code is a LOT easier to change and maintain than something that is slick and elegant.
TM
+2  A: 

In answer to your question "No, but ..."

If we leave the non-elegance as done, I think we have failed.

If each time we visit the code after it is "in" in its non-elegant manner, we make it a little better (Refactoring whilst backed up by a good suite of unit tests) then the elegance will come.

Kindness,

Dan

Daniel Elliott
I DO tend to fine tune old code when I recognise ugliness, but I'm trying to give it up. I work in industry: both customers and QA get a little 'iffy' about code which doesn't have the same version number as last time it was used.
pavium
+2  A: 

From my (limited) experience, the two primary and easy-to-remember goals should be (Borrowing copious amounts of knowledge from Alistair Cockburn):

  • produce business value (within the given constraints of time-scope-cost-quality)
  • to set the stage for the next team that will work with your code. (clean code, tests, docs, etc.)

Elegance is a desired attribute of code not a mandatory one. e.g. if TeamA produced an app that met the desired market need and made millions while TeamB labored to produce an "elegant" app that missed the bus and came out 6months to a year after TeamA, Guess who won?

Gishu
Elegance can be taken to the extreme as well. But at the end of the day the team that do not allow themselves to commit ugly cod to the repository will be able to sustain a higher level of productivity in the long run. Those who run fast and dirty from the start will son run with a heavy weight of technical depth.
PeyloW
+2  A: 

But I'd like to know is elegance a goal of programming (an essential aim), or merely a desirable side-effect? (or technique)

I think that striving for elegancy is a trick to produce tolerable code. So, even if the end result isn't quite as elegant as you tried to make it, you haven't failed; instead, you've succeeded in creating good enough code.

The end result will usually be somewhat worse than what you planned, so your best bet to get something good is to strive for excellency.

Joonas Pulakka
You seem to be saying 'elegance equates with efficiency' or at least 'elegant code is probably more acceptable because it works better' I can't disagree with that.
pavium
Elegant code is always better than barely passing code. Code that already good can easily be made great, bad code is freaking hard to turn into great. So never allow yourself to write bad code to begin with.
PeyloW
Just because a piece of code is not elegant does not necessarily mean that the code is "bad".
Ed Swangren
@Ed: I think it does. Even if it may work perfectly, if it's a pain to comprehend so that it's virtually unmaintainable, then yes, it indeed is "bad".
Joonas Pulakka
+2  A: 

If it works, but isn't elegant, it probably just means you are not finished yet. Refactoring is your friend now. In my experience, elegance is hardly ever reached on the first try.

ammoQ
Ah but, as I commented to agileguy, in industry changing the code is considered undesirable. I once said to my QA manager 'I only make improvements'. He was skeptical.
pavium
pavium: That's why you should improve the code _before_ you officially declare it "finished".
ammoQ
+1  A: 

The aim should always be to write elegant code. Any deviation from this aim is a failure, even if the code works, a failure that can be remedied but a failure no less.

  • Never write any code that you can not debug tomorrow. A clever trick today, can be hours spend scratching your own head another day.
  • Never ever allow a bug-fix to be a ugly work-around. Fix the problem, do not work around it, ten bug-fixes later your code will be nothing but patches otherwise.
  • Never let the complexity of the internals leak to the interface. If yo as the developer of a service can not manage it, how can you even dare ask your clients manage it for you?
PeyloW
An interesting perspective. My own perspective comes from creating software to test semiconductor devices. Failures are failures. They can't be fixed, but we can learn from our mistakes... But, apart from the peculiarities of my situation, I tend to agree with you. Maybe my next question will be 'Where's the dividing line between elegance and obfuscation?'
pavium
Ed Swangren
Ed, focusing on quality means you can sustain a steady progress for a year. If you allow yourself to get sloppy, you will appear to go faster the first weeks, but before that year ends you will be hit hard by reality and progress is nearing a halt. All you do is cleaning up the ness you made. In th long run the tortoise wins over the hare.
PeyloW
Yes, I understand that and I was careful to not sound as if quality does not matter, but when all is said and done it is not a goal unto itself. The goal is to create useful software for your clients. That is the judge of success or failure, not pure code quality
Ed Swangren
The question is not "should I strive to write elegant code", the question is "have I failed as a developer if I do not". Those are two very different questions.
Ed Swangren
I believe you have. Deliberately write code you know could be better, be for short term wins or pure laziness, is failure. I could go as far as saying betraying your customer, if someone is paying for your work. It's like the center forward not even trying to score a goal, settling for a 0-0 draw is not what you are paid for.
PeyloW
@PeyloW - you are way too dogmatic. Sometimes the overriding short term and even long term goals **demand** that you write inelegant code; see my answer.
Stephen C
@Stephen C: I think I am not, the difference between my answer and yours is that my take on programming still works even if the requirements are changed in the future. And so far I have never worked on a project where requirements has not changes. Nor a "prototype" that did not end up in production.
PeyloW
+1  A: 

And of course It depends on what you mean by saying elegant. It depends on how you define elegant code in your dictionary.

Braveyard
But I wanted to avoid a discussion of 'what is elegance'. It's been done before. I'm quite happy to accept that elegance is what everyone thinks it is.
pavium
@pavium : Okay, as long as the common definition of elegance is what almost everybody thinks, then there will be no problem :)
Braveyard
But everyone thinks it's something different. See here http://stackoverflow.com/questions/563036/what-is-elegant-code/563354#563354
MarkJ
+1  A: 

We should always remember that the process of writing code that solves a problem is nothing else than a kind of business value generation. The code you write then becomes one of the assets your company has, similarly to how an assembly line person puts together a computer that becomes the property of their employer and which gets sold afterward.

If you do not write your code in a maintainable way, then you are providing your company with a lower business value. Invariably this code will need to be tweaked, fixed, etc, and your company will have to spend more resources on this than if you had written it nicely from the beginning. In some cases (notably when the initial codebase originated in South Asia), the investment to perform this maintenance hugely outweighs the savings gained early on from producing quick and dirty code.

This is of course only one part of the equation, it fits in next to many other sources of business value. You may sometimes need to sacrifice on the value of your code to be able to reach out and grab a lot more value in other ways (e.g. if your video game hits stores on December 27th, the value of your clean code is practically null next to the amount of money you lost).

Unfortunately, many managers are not well equipped to properly weigh the value of code vs. the more traditional business value sources like customer acquisition. They view their codebase as some kind of ephemeral magic they had to pay for in order to satisfy their customers' whims, instead of true business value they own. With managers like this you will often be pushed to produce code faster while ignoring it's elegance. The other edge of this sword is that they also won't understand why you need to take so much time to fix something in last month's release. Hopefully you'll be able to educate them on the value of code elegance :)

Tekahera
+1  A: 

I think elegance is overrated.

Code should:

  • work (first and formost, I've seen elegant solutions that don't actually accomplish the task)
  • be efficient
  • be maintainable

None of those things require elegance. In the database world, elegance is often the enemy of efficient. Efficiency should trump elegance, the users care about efficiency, they don't see or care about elegance.

HLGEM
I disagree. Elegance goes hand-in-hand (most of the time) with readability, and you want to program for readability *first* then efficiency (it goes without saying that you're coding to solve a problem).
RCIX
Efficiency is FAR more important that readability!
HLGEM
+1  A: 

Like so many other things in life, (drumroll please)........ It Depends.

The first thing you should be writing code for is to solve the problem. After that, you should write elegant, maintainable code, then finally, specialize for the situation. Sometimes there's no other choice but to write some ugly hunk of trickery to speed up your code execution, and that's not a failure.

RCIX
+2  A: 

Unless elegance per se is rarely a requirement. Maintainability ... which is often related to elegance ... is often a requirement. However, unless maintainability is overriding requirement, then code that is inelegant/unmaintainable is definitely not a failure.

So this boils down to the question as to whether elegance / maintainability is an overriding requirement. And that depends on various factors:

  • Maintainability is a requirement for code that has to last a long time, even if this is not explicitly stated ... or even recognized by management.

  • Maintainability is generally not a requirement for a prototype / proof of concept.

  • Maintainability may be overridden by a requirement for performance.

  • Maintainability may be overridden by a requirement to get something implemented / fixed quickly.

Elegance and maintainability are not always the same thing. Consider for example, that the most elegant solution to a program may be to code the application in (say) Haskell. However, you are working in a C or Java shop, and nobody but you understands Haskell. Your elegant solution won't be maintainable by the rest of the team if you are hit by the proverbial green bus.

Finally, the (hypothetical) notion that elegance per se is an overriding requirement is IMO, just plain daft. Programming is an engineering discipline, not a fine arts discipline.

Stephen C