In my mind, there are two main differences between those chunks of code (ignoring the differences in functionality). First, the algorithm is different. Second, the top one chains expressions. I'll rewrite the top one without the chaining:
sub YetAnotherMakePtag { my $fixme = shift; my @lines = split "\r\n", $fixme; my @nonblank_lines = grep { /\S/ } @lines; my @wrapped_lines = map { "<p>$_</p>" } @nonblank_lines; return join("\r\n", @wrapped_lines; }
I would argue that this version is clearer than either original, with or without comments. (Probably the slowest, too.) While the chaining made things more confusing, the algorithm change actually made things clearer. And that's because the concepts in the new version, however many there may be, are a direct mapping to a natural conception of how to perform the task. In MakePtag(), there is an intuitive leap required to see that you can wrap delimiters around a string by replacing separators with close-delimiter open-delimiter pairs, and then wrapping the whole thing with open-close.

I try to avoid that construct in my code. For example, I always do print "$_\n" foreach (@list) instead of print join("\n", @list), "\n" simply because the first is a more natural translation the actions I wish to perform. (It also reduces repetition of the terminator, and is much more open to modification if you want to do something a little different: print "-->$_<--\n" foreach...).

On the other hand, for pedalogical purposes I think Ovid's is best. It answers your question, it gives a more complete solution, and it introduces a useful technique (chaining) that doesn't particularly obscure the clarity of the example because it's so easy to see the transformation I did above.


In reply to Re: What is clear code ? by sfink
in thread What is clear code ? by mandog

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.