From: Ronald Fischer Date: 2007-05-29T22:53:26+09:00 Subject: Re: Introducing the "it" keyword > The OP brought up an interesting issue, but what made it > interesting is not > the implicit binding but the need for a scoped temporary > value. There are > three important optimization goals to it: > > 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) > > See > 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. > First, it reduces > readability. Second, even if naming the variable explicitly > is optional > rather than required, you get the same sorts of maintenance > problems caused > by the optionally braceless block notation for single > statements in C-derived > syntaxes, e.g. > > 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