From: Just Another Victim of the Ambient Morality Date: 2007-12-11T04:50:04+09:00 Subject: Re: How much would variable declarations in Ruby make you wince? "Eivind Eklund" wrote in message news:dc9b20bb0712100945m1dd2ab73p9ab3cf104d5be858@mail.gmail.com... > On Dec 10, 2007 5:34 PM, Just Another Victim of the Ambient Morality > wrote: >> You're really not thinking this through. >> How on God's green Earth are you going to accidentally prepend an >> extraneous "var " (or whatever keyword is decided upon) to a variable's >> assignment. Because this is highly unlikely, the described problem will >> only happen if you accidentally want to declare a new variable. This is >> rather distinct from wanting to use a variable and so the bug should >> occur >> far less frequently... > > It isn't so distinct that the error don't happen, as I've done the > error in Perl. I suspect this has to do with the way either of us > write code; I write a lot of small methods, and I often combine > initialization and variable creation. So, to my mind, first > initialization of a variable is mentally equivalent to declaring the > variable - so I'll just write "my" when I write the first > initialization. When this happens to be inside a code block (e.g, > inside an if statement), I need to expand the context of the variable, > so I'll throw a declaration before the code block. I'm not exactly sure what you're describing, here. Are you describing behaviour in PERL? Is PERL like C/C++, where variables declared in "if" blocks (or any code blocks, for that matter!) are local to that block? In Ruby, variables leak right out of if and loop code blocks, so your coding style wouldn't be any worse off by this change. If I haven't been misinformed, variables in Ruby blocks (the things that are closures) are going to leak too, so there will be no situation where your habit will trip you up. Variable declarations will help prevent some bugs and will not encourage any others (that I've thought of)! > With Ruby, this is just an initialization - "variable = nil". With > Perl it's "my variable" - alas, that variable is the shadowed by the > other my declaration I wrote later in the code and earlier in time. > > I had this bug just last week. > > In my opinion, the noise of repeated "var "-declarations (or > "my " declarations) makes the code less readable, with the > corresponding increase in correctness checking being a benefit of less > worth than the cost. This is the same as my view of (other) type > declarations in most cases: Their cost in terms of being in the way of > code readability/transformability exceeds their benefits. As I've mentioned, above, there should be no cost except that of actually having to type it out. My guess is that declarations are far less common than variable use, so I don't think you'd have to type it out all that often, either. It will even have the strange side effect of encouraging funcitonal programming, which is funny...