Re: IYO, what is elegant code?
by dws (Chancellor) on Jul 13, 2003 at 02:57 UTC
|
I was wondering how one might "define" elegant code?
Elegance is a funny thing. You know when it's not there, but sometimes it's hard to articulate why it is there when you're seeing it.
To my eye, an elegant piece of code does what it needs to in a way that's easy to follow, without any extra bells-and-whistles (including needless commentary). "Easy to follow" implies well formatted, with just enough horizontal and vertical whitespace. It also implies carefully chosen names, and control-flow structures that match the problem.
It implies a well-factoredness, with a certain coherence to each subroutine/method.
A truly elegant piece of code teaches you something good about how to reason about a problem.
An elegant piece of code is written first to inform the reader, and second to be executed by a machine. Sometime, when speed is needed, elegance can be undone.
| [reply] |
Re: IYO, what is elegant code?
by nysus (Parson) on Jul 13, 2003 at 04:31 UTC
|
Because I truly am the Perlmonks' most clueless Pontiff, I started to write a module that has already been done. You can view it here. A couple of days later, I stumbled upon this code by Lincoln Stein which you can view here. Now Stein's code doesn't do exactly what my code does but all you probably have to do to get it there is modify a half dozen lines to so. I learned a lot comparing the two. I plan to spend a good hour or two more dissecting Stein's code some more to learn how I can improve my programming skills. But here's what I think makes Stein's code far more elegant than mine:
First, Stein's code is more efficient. While Stein names that tune in 2 notes, I named it in 10. That's an obvious one.
Second, elegant code makes use of existing modules. Not only that, but it makes efficient use of those modules. Stein obviously has much more familiarty with the methods in the modules than I do. What I wrote a whole subroutine for, he wrote in one line.
My last observation was that elegant code is fluent with powerful programming tools and how to apply them. Even though the problem I was trying to solve was ripe for a recursive function, I didn't see it. Even if I did see it, I probably wouldn't have figured out how to write it without pulling some hair out of my skull. Stein, however, obviously tossed off his recursive function as an afterthought. To me, this is probably the most important mark of elegant code. It really shows the programmer knows his/her stuff.
Update:That last comment reminded of something else I wanted to add about comments. My comments are way too wordy and get in the way. I obviously think way too my hard about my code. Stein's comments are much more concise and to the point.
Well, that's what I learned. Hope it helps you gain some insight.
$PM = "Perl Monk's";
$MCF = "Most Clueless Friar Abbot Bishop Pontiff";
$nysus = $PM . $MCF;
Click here if you love Perl Monks
| [reply] |
|
|
Wow, that's some good stuff, nysus. Thanks a bunch, I'll definitely take it in. I've made it a habit as well, reading better perl programmer's code, and it seems to really help.
And if you're feeling lucky... come and take me home
And if you feel loved
If you feel lucky, if you feel loved
If you feel lucky, if you feel loved
You've crossed the walls - Excelled
Further along through their hell
All for my heart, I watch you kill
You always have, you always will
No
w spread your wings and sail out to me....
| [reply] |
Re: IYO, what is elegant code?
by Albannach (Monsignor) on Jul 13, 2003 at 03:02 UTC
|
This is of course subject to personal preference, but the things that come to my mind first are along the lines of:
- as simple as possible but not simpler
- smartly done (far from brute force)
- clean, clear, unadulterated
- distilled, with a hint of smokey peat... (perhaps I'm getting off track)
I certainly am a fan of readable code, but there should also be as few comments as possible, just enough to fill in the odd question that might arise in a reader's head as they are following the flow. Extra comments are fine if one is writing code to teach, but in practice I don't think it helps to write code on the assumption that later readers will be newbies. To be clear about this, I consider comments different from documentation which should be at the start of a routine, explaining the input, output, and general algorithm. I may be pushing it here, but I sometimes feel like every comment in-line is a blight, moving the work further from elegant with each line.
--
I'd like to be able to assign to an luser | [reply] |
Re: IYO, what is elegant code?
by Zaxo (Archbishop) on Jul 13, 2003 at 02:56 UTC
|
Following your definition, elegant code is well-written code with panache.
Well-chosen algorithms help, too.
After Compline, Zaxo
| [reply] |
Re: IYO, what is elegant code?
by Juerd (Abbot) on Jul 13, 2003 at 15:37 UTC
|
IMO, code is elegant if all of these are true:
- The code does what it is supposed to do
- The code looks like it does what it does
- The code is readable and follows a style that I like
- The code is efficient
- The code is pure (re-usable)
If code is efficient but unreadable, it is no longer elegant. Likewise, code that does what it should do, but looks like it would do something else, is inelegant. I like code styled like this.
Please note that code does not have to be elegant in order to be useful or entertaining.
Juerd
# { site => 'juerd.nl', plp_site => 'plp.juerd.nl', do_not_use => 'spamtrap' }
| [reply] |
|
|
That makes TOTAL sense. Thank you for the link, I'll make use of it for sure. :-)
And if you're feeling lucky... come and take me home
And if you feel loved
If you feel lucky, if you feel loved
If you feel lucky, if you feel loved
You've crossed the walls - Excelled
Further along through their hell
All for my heart, I watch you kill
You always have, you always will
Now spread your wings and sail out to me....
| [reply] |
Re: IYO, what is elegant code?
by sauoq (Abbot) on Jul 13, 2003 at 20:43 UTC
|
"A designer knows he has achieved perfection not
when there is nothing left to add, but when there is nothing
left to take away." — Antoine de Saint-Exupéry
-sauoq
"My two cents aren't worth a dime.";
| [reply] |
Re: IYO, what is elegant code?
by hsmyers (Canon) on Jul 13, 2003 at 14:06 UTC
|
Elegant code is identified the same way as obscenity---we may not know what it is, but we know it when we see it! My own filters are typically set off more by code that does something familiar in a new way than by the unfamiliar done any way at all. The flagging reaction is something along the lines of 'Cool' or 'I didn't know you could do that, that way' or shuch like. BTW, this is not to say that new code can't be elegant, just to say that I don't have any useful metric to identify it with.
--hsm
"Never try to teach a pig to sing...it wastes your time and it annoys the pig." | [reply] |
Re: IYO, what is elegant code?
by halley (Prior) on Jul 14, 2003 at 03:41 UTC
|
Atop all the nice things people said above, I think there's one other trait I can only express this way:
It looks like the language was designed just to solve this problem in exactly this way.
Good melodies also share this trait. Once the public hears it, the public can't remember a time when they didn't know it. Henry Mancini and John Williams and Johann Sebastian Bach. They plucked the notes from mankind's subconscious and we all know them by heart.
Examples of elegant Perl code? As famous as it is, I wouldn't say the Schwartzian Transform quite meets the bar (in my opinion); it's memorable but it looks like it overcomes something obviously missing in Perl syntax itself. The sublime and subliminal grep { not $seen{$_}++ } trick to get unique sets is where I see it.
The problem with thinking up some one-liner examples is that Perl promotes the good stuff into one canonical token and everyone forgets the amazing work that it does behind the scenes. What other language allows such effortless expressions? Think of the dizzying work behind doing a
sort, a
grep, a
m//g?
-- [ e d @ h a l l e y . c c ]
| [reply] [d/l] [select] |
Re: IYO, what is elegant code?
by hardburn (Abbot) on Jul 13, 2003 at 14:25 UTC
|
The mathmatical definition of "elegant" is a theorum that is simple, yet powerful. That sounds like it is a very subjective term, and I guess in the strictess sense, it is. However, both math and CS majors tend to agree about wheather a particular therum or code is elegant.
---- I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
-- Schemer
Note: All code is untested, unless otherwise stated
| [reply] |
Re: IYO, what is elegant code?
by jplindstrom (Monsignor) on Jul 13, 2003 at 18:06 UTC
|
Elegant code can be code that is easy to work with.
Say you want to implement a new feature, and it turns out you do it by thinking a lot and coding a little. You think for half an hour and then edit two lines of code and write three new ones and then you're done. That's elegant.
I've only ever experienced that with code that was very well factored. | [reply] |
Re: IYO, what is elegant code?
by Abigail-II (Bishop) on Jul 13, 2003 at 11:29 UTC
|
That's easy. My code is elegant, and all elegant
code is mine. ;-)
Abigail | [reply] |
|
|
Sure, you're joking, but what you wrote fits in sooo well with my theory about elegant code being a full meshing of the author's and reader's expectations and culture. Since for this statement $author eq $reader, it only stands to reason that you would have that opinion. :P
--
Allolex
| [reply] |
Re: IYO, what is elegant code?
by allolex (Curate) on Jul 13, 2003 at 18:53 UTC
|
Oh, that's easy. Elegant code is the opposite of clunky code ;) But seriously, elegant code is like good writing: there are many different styles and many more different ways those styles are understood. So much depends on whether the author and the reader share enough culture to understand each other---that's when the code is considered elegant.
--
Allolex
| [reply] |
Re: IYO, what is elegant code?
by ajdelore (Pilgrim) on Jul 14, 2003 at 21:00 UTC
|
Borrowing an idea from Kurt Vonnegut's Bluebeard, the best way to determine elegant code is to look at a million lines of code, and then you can never be wrong.
The $1 million dollar epistomological question: Is there a universal definition, or is it purely subjective? I suspect some of both...
</ajdelore>
| [reply] |
|
|
Hmmm, that's a good reply. Definitely has me thinking :-)
And if you're feeling lucky... come and take me home
And if you feel loved
If you feel lucky, if you feel loved
If you feel lucky, if you feel loved
You've crossed the walls - Excelled
Further along through their hell
All for my heart, I watch you kill
You always have, you always will
Now spread your wings and sail out to me....
| [reply] |
Re: IYO, what is elegant code?
by johndageek (Hermit) on Jul 16, 2003 at 20:39 UTC
|
Elegant:
Does what it is supposed to do
Is clear as to what it is supposed to do
Simple as possible
Consistant with itself and within it's framework
Suggests other possibilities to the beholder based upon it's attributes
Beutiful in the eye of the beholder
Lover, Machinery, Art, Technology - elegance is the same
John
| [reply] |