To me, elegance is where you have encoded as much information per N characters as I can easily read, plus having used features that strike me as appropriate, especially if I wouldn't have done it that way. A little thinking to comprehend the algorithm is acceptable, so long as I don't have to break out pen and paper to rewrite it. (That is obfu, not elegant. Obfus have their own beauty, but I wouldn't call them elegant.)

Your title implies that elegance and clarity are orthogonal qualities. I would say that clarity is an essential part of elegance. Another integral part of elegance is simplicity. Think about what you would consider elegant in a person. Some items that have classically defined an elegant woman, for example, could be:

Elegance is about understated simplicity. So, to me, your first snippet isn't elegant. It's short, but I have to break out pen and paper to understand it. However, I would consider your second snippet elegant. It is also short, but it discusses one concept and does it in a simple fashion.

Your csv_split() ... It has elegant parts. Let me explain ...

sub csv_split { # This is very elegant. It handles one concept and does so in a si +mple and # clear fashion. # Two bugs: # 1) use "return;", not "return undef;". The latter creates a o +ne element # list in list context. # 2) I would use "shift // return;" (if you have the patch). Ot +herwise, # your function won't handle "0.0" correctly. If you don't h +ave the # patch, I would do a defined-check. local $_ = shift || return undef; my @array = (); # This is not elegant - it's cute and clever. You're using 0 in bo +th its # numeric and boolean aspects. Now, you may not have a choice in t +he matter, # as Perl may not give you the tools to be elegant. (It might ...) my $count = my $quoted = 0; # The while definition is elegant - simple, understated, but clear +. But, the # use of $1 throughout the loop is definitely not elegant. It's ki +nda ugly. # Again, I'm not sure Perl gives you the tools to do the elegant t +hing here. while ( s/(.)// ) { if ($1 eq ',' && !$quoted) { $count++; next; } # The use of q// here is more elegante than '"', but it might +have been # better to provide a variable that hides the ugliness. if ($1 eq q/"/) { # The "$x = 1 - $x" idiom is very elegant. I've always lik +ed it. unless ( $quoted && s/^\"// ) { $quoted = 1 - $quoted; nex +t; } } $array[$count] .= $1; } # Elegance might demand a wantarray solution here. It's unclear. @array; }

My version (with a bit more of what I consider elegant) would be (untested):

sub csv_split { local $_ = shift // return; my $is_quoted; my $count = my @array; while ( s/(.)// ) { if ($1 eq ',' and not $is_quoted) { $count++; } elsif ($1 eq q/"/ and not ( $is_quoted and s/^\"// )) { $is_quoted = !$is_quoted; } else { $array[$count] .= $1; } } return if $is_quoted; wantarray ? @array : \@array; }

I believe my version also better delineates the flow control.

Update: Added tilly's toggle. I was trying to think of it, but couldn't, so left the x=1-x flip. The toggle is definitely more elegant.

------
We are the carpenters and bricklayers of the Information Age.

Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.


In reply to Re: Code for elegance, code for clarity by dragonchild
in thread Code for elegance, code for clarity by delirium

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.