The OP brought up an interesting issue, but what made it=20
interesting is not
the implicit binding but the need for a scoped temporary=20
value. There are
three important optimization goals to it:
=20
1) avoid holding onto memory any longer than necessary (memory)
2) avoid clumsy use of temporary values (readability)
3) avoid recomputing the same value more than once (speed)
=20
See=20
http://weblog.raganwald.com/2006/10/why-are-local-variables-bad.html
for a discussion of local variables and better ways of doing things
(including the functional let).
Well, that article raises the issue that mutal local variables are
evil. I have sympathy with this statement, though I would like to
add that mutal global variable are evil as well, and your hint of
using functional let (which, BTW, was also the inspiration to my
usage of let blocks) just emphasizes that a bit more functional style
could make the world of programming less evil. But while I certainly
would cheer any new element of Functional Programming in Ruby, certainly
Ruby is not Haskell or Scheme. If we go into *that* direction, maybe we
should
start the discussion from a completely different end? How much
additional
FP would enhance Ruby?
Importantly, an implicit variable name has two problems.=20
First, it reduces
readability. Second, even if naming the variable explicitly=20
is optional
rather than required, you get the same sorts of maintenance=20
problems caused
by the optionally braceless block notation for single=20
statements in C-derived
syntaxes, e.g.
=20
let Time.now in
puts "starting at #{$1}"
let a+b+c in
puts "sum =3D #{$1}" if $1 > 3
end
end
Though I for sure can imagine examples, where implicit variables make it
hard to read, your example is, IMO, very easy to follow: You can clearly
see that the first $1 is bound to the time, and the second one is bound
to the sum. But I am well aware that the issue of how/to what extent
variables should be named, is a controversial one, and I wouldn't enjoy
opening that bag of worms here.
Maybe I'm now bringing too much Perl-spirit to the Ruby discussion, but
IMO if a sufficiently large number of users would find some language
feature
useful (and I mean this in a general sense, since I don't know how many
users the "it"/"let"/... stuff would really appreciate), readibility
issues
should be more on the users side (project management), not on the side
of the=20
language designers. Just my opinion, and I know well that quite a few
will
oppose me here...
...is as bad an issue as...
=20
for (int i=3D0; i<5; ++i)
printf("%d\n", i);
fprintf(stderr, "%d\n", i);
=20
Basically, if you need to add an outer scope, you have to=20
renumber all of
the temporaries.=20
Not if you count them from inside out, as I did it. Of course
you would have to renumber if you add an inner scope, such as=20
when changing
let foo() in
x1($1)
x2($1)
x3($1)
x4($1)
x5($1)
into
let foo() in do
x1($1)
x2($1)
let bar() in do
y0($1)
x3($2) # <--- renumbering necessary
y1($1)
end
x4($1)
x5($1)
end
but as I said, I would use anonymous variables only in a *very* local
way,
not for a longer piece of code. In the example above, I would likely
have
assigned a name foo(), but perhaps used anonymous $1 for bar(). Sure,
code
*is* undergoing changes, and I can imagine cases where I would have to
renumber (or, more likely, replace my anonymous variable by a name later
on).
It's just that I don't feel this as painful, if it happens only here and
then.
Ronald