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


In reply to Re: IYO, what is elegant code? by nysus
in thread IYO, what is elegant code? by stonecolddevin

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.