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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |