List Info

Thread: Re: declarative syntax




Re: declarative syntax
country flaguser name
Satellite Provider
2007-03-18 18:08:34
--- Eric Wilhelm <scratchcomputinggmail.com> wrote:

> # from Sergei Steshenko
> # on Sunday 18 March 2007 02:15 pm:
> 
> >funky_array => [1, 2, 3, 5, 8 ...],
> ># element is the some of previous two,
> ># and this way 100 times; 
> 
> >So, this is Perl, and there is more than one to do
it, and I would do
> > it this way:
> >
> >{
> >...
> >funky_array => do{
> >                 my tmp_array;  # no name clash
danger, the scope
> >                                 #  protects from
name contention
> >
> >                 my $number_of_times = 100; # I
said 100 times, dodn't
> > I ?
> >
> >                 my $previous_element = 0;
> >                 my $element = 1;
> >
> >                 while($number_of_times-- > 0)
> >                   {
> >                   $element += $previous_element;
> >                   push tmp_array, $element;
> >                   $previous_element = $element;
> >                   }
> >
> >                 tmp_array; # the array
reference to be returned
> >                 }
> >};
> >
> >So, the point of this example is that there should
be
> > canonical/normalized data structure and there can
be infinite number
> > of ways to produce it.
> 
> Fine.  But, you should be able to skim over the code in
much less space.  
> When you want to build something based on a rolling
pair of values, why 
> not use a rolling pair of values?
> 
>   ...
>   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 why should help those claiming Perl is unreadable ?

And what is $v if I may ?

I prefer readable code, not a short one.

I remember a few days ago a colleague of mine was saying
"'i' is current and 'j' is
previous". And the colleague had a real difficulty
debugging her own code.
And I wanted to ask her "Why did you give your
variables meaningless names in the
first place ?".

I wouldn't have written such a code in the first place - in
my code previous is
previous and current is current.

Sorry.

--Sergei.

Applications From Scratch: http://appsfromscr
atch.berlios.de/



 
____________________________________________________________
________________________
Need Mail bonding?
Go to the Yahoo! Mail Q&A for great tips from Yahoo!
Answers users.
http://answers.yahoo.com/dir/?link=list&sid=3965460
91

------------------------------------------------------------
-------------
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-userslists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/wxperl-use
rs

Re: declarative syntax (please, "longer" ne "better"!)
country flaguser name
United States
2007-03-18 19:35:02
# 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-userslists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/wxperl-use
rs

[1-2]

about | contact  Other archives ( Real Estate discussion Medical topics )