in reply to IYO, what is elegant code?

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

Replies are listed 'Best First'.
Re: Re: IYO, what is elegant code?
by stonecolddevin (Parson) on Jul 13, 2003 at 04:34 UTC
    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....