# from Sergei Steshenko
# on Sunday 18 March 2007 04:08 pm:
>> ...
>> funky_array => do {my p = (1,2);
>> [1,2,map({push( p, my $v = shift( p)+$p[0]); $v} 3..100)]},
>> ...
>
>Why should I load my brain trying to understand what $p
means ?
>And what is $v if I may ?
Well, one-letter variables that only appear within a closed
block on two
lines of code never bothered me. I prefer to think about
the algorithm
and not whether the code *appears* to be readable. It's
encapsulated
by virtue of its seeming opacity.
>I prefer readable code, not a short one.
I prefer correct code. If my subtlety eludes you, go run
yours and see
what answer you get. It is just a long-winded [map({2**$_}
0..99)]
(i.e. a doubling sequence) which is at-a-glance very
readably wrong.
I really don't understand why so many people seem to think
that longer
code is somehow better and more readable. Have they had a
readability
study? Would you call a 5000-page novel
"readable"? Is a 20-hour
movie "watchable"? If you're writing 10 times as
much Perl as you
should, why not just use C? You'll have the same number of
bugs, but
they'll run faster :-D
A local news station once had (maybe still does have) the
slogan "Clear,
accurate, and to-the-point." I always thought it
ironic that they
would use so many words to claim that they are
"concise".
I'm not claiming that code should be all one-character
variable names in
a single chain of compile-time map() statements with no
carriage
returns (for that, you need lisp.) Rather, code should not
be any
longer than it needs to be. A large part of Perl's
expressiveness
comes from context, implied information, and concision.
There's more
than one way to do it, but there's a whole lot of ways to do
it wrong.
Concise and correct code beats long-winded incorrect code.
Further,
short and incorrect code is easier to fix. In fact, the
longer code is
obfuscated by its shear size. Ever heard the phrase
"needle in a
haystack"? For code to be readable, you have to
understand what it is
doing. I could use utf8 with greek characters for variable
names and
it would make as much sense and still be correct. We have
two things
p
which get rotated as the new one $v arrives. How is that
more
complicated than your longhand variable names? (If you
haven't found
the bug, I think you need yet another variable to hold the
new value.
Then it will give 1,1,1,1,1 instead of 1,2,4,8,16. But to
get
1,2,3,5,8,13 you have to deal with the seeding.)
So you want better variable names and comments? Fine. The
algorithm is
still more clearly (and correctly) expressed in less code.
funky_array => do {
my pair = (1,2); # previous pair
[1,2, # seed first
map({ # roll-over the pair while calculating the next
val
push( pair, my $new_value = shift( pair)+$pair[0]);
$new_value
} 3..100)]
},
Or,
funky_array => do {
my out = my pair = (1,2); # seed/previous pair
for(3..100) {
# roll-over the pair while calculating the next value
# I call it '$new_value' because it is new, and a
value.
push( pair, my $new_value = shift( pair)+$pair[0]);
push( out, $new_value);
}
out
},
Well, that's getting long-winded. But, to make sure the
dead horse is
thoroughly beaten, let me just say that you (hopefully)
won't be
searching your codebase for bugs and skimming this e-mail in
Foo::Bar.pm. Code is going to be read/skimmed over and over
and over
and over and over again. You pay for the extra lines of
code *all the
time*. You only pay for the extra density when you're
working on that
particular chunk.
--Eric
--
"Because understanding simplicity is
complicated."
--Eric Raymond
---------------------------------------------------
http://scratchcomputing.c
om
---------------------------------------------------
------------------------------------------------------------
-------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the
chance to share your
opinions on IT & business topics through brief
surveys-and earn cash
http://www.techsay.com/default.
php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
wxperl-users mailing list
wxperl-users lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/wxperl-use
rs
|