See
Why are local variables bad?
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?
let Time.now in
puts “starting at #{$1}”
let a+b+c in
puts “sum = #{$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
language designers. Just my opinion, and I know well that quite a few
will
oppose me here…
…is as bad an issue as…
for (int i=0; i<5; ++i)
printf(“%d\n”, i);
fprintf(stderr, “%d\n”, i);
Basically, if you need to add an outer scope, you have to
renumber all of
the temporaries.
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
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