# Re: Non-declared variables

**URL:** <https://www.ruby-forum.com/t/re-non-declared-variables/72349>\
**Category:** Ruby\
**Created:** [October 16, 2006, 6:06pm UTC](https://www.ruby-forum.com/t/re-non-declared-variables/72349 "2006-10-16T18:06:39Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![phrogz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/2acd7d/32.png) [@phrogz](https://www.ruby-forum.com/u/phrogz)\
**Post date:** [October 16, 2006, 6:06pm UTC](https://www.ruby-forum.com/t/re-non-declared-variables/72349/1 "2006-10-16T18:06:39Z")

</div>

From: Martin C. [[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)]

> > Every language feature that reduces how much typing I have  
> > to do is a tangible benefit to me. It eases stress on my  
> > body machine, and speeds the delivery of my code.
> 
> That seems a ridiculous thing to say to me. Time taken to type out  
> code is not the limiting factor in your productivity.

You’re right, it’s too much of a blanket statement. I would not want to  
program in APL[1], for example, despite it’s terseness.

On the other hand, I wouldn’t want added to Ruby requirements to:

- Declare all my variables before using them.
- Write out Block.new( arg1, arg2 ){ body } for all blocks.
- Use ‘definition’ in place of ‘def’
- Put parentheses around all method calls
- Use .send( “foo=”, 12 ) to invoke a setter method
- Include types for every variable, argument, and method return  
(either as static types or ‘suggestions’)

etc.

The original post in this thread suggests that omitting declarations of  
variables leads to debugging problems for no real gain. My intended  
point was that (for me) a lot of Ruby’s elegance comes from its  
simplicity of expression. Ruby doesn’t do anything that I can’t do in  
Lua or JavaScript, except make my life as a programmer way easier.

[1] [APL (programming language) - Wikipedia](http://en.wikipedia.org/wiki/APL_programming_language)

---

<div class="post-metadata">

**Author:** ![phrogz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/2acd7d/32.png) [@phrogz](https://www.ruby-forum.com/u/phrogz)\
**Post date:** [October 17, 2006, 3:14am UTC](https://www.ruby-forum.com/t/re-non-declared-variables/72349/2 "2006-10-17T03:14:47Z")

</div>

“Gavin K.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

> The original post in this thread suggests that omitting declarations of  
> variables leads to debugging problems for no real gain. My intended  
> point was that (for me) a lot of Ruby’s elegance comes from its  
> simplicity of expression. Ruby doesn’t do anything that I can’t do in  
> Lua or JavaScript, except make my life as a programmer way easier.

I think what the OP was getting at is that assignment syntax is  
identical to declaration syntax, and there’s no clear distinction  
between the two. Therefore, when you want an assignment to be an  
assignment of an already declared variable, you have nothing checking  
you for typos.

This has nothing to do, by the way, with variable typing. As an  
example, perl with the “strict” flag on behaves this way, requiring  
variables to be declared before use and most perl programmers don’t  
find the extra checking cumbersome (in fact, if you go to perlmonks,  
the first thing they’ll ask you to do is to turn on “use strict”).  
Although variables can be declared in outher ways (it is, after all,  
perl) most perl programmers just put "my " in front of their first  
usage of a variable, and that’s that. You can declare and assign in  
one statement, or do them in two.

In ruby, I imagine that we’d evolve some syntax like this:

class Foo  
def initialize(name, thing)  
n @name = name  
n @thing = thing  
n @count = 0  
end

def inc  
# This line is an error at class definition parse time, because  
# I typoed a variable name  
@coutn += 1  
end

def compute  
# these four will be initialized to nil  
n a,b,c,d  
# I can now use a, b, c, and d as local variables without further  
# declaration.  
end  
end

Note that separating declaration from assignment also makes it dirt  
simple to decide the proper scope of a variable, instead of having to  
have the switch between ruby 1.9 and ruby 2.0 of various scoping  
details.

I’d welcome such a syntax to ruby - requiring, of course, a per-file  
strictness switch; no point in killing backwards compatibility - and I  
think that the extra typing would provide sufficient benefit to be  
worth it. Note that block parameters are already declared in the ||  
list at the start of the block, so nothing changes in:  
myArray.each {|a,b| alert(a,b)}  
although I may want to think carefully about what that block means in  
the presence of a declared variable called ‘b’ in the surrounding  
scope. I generally wouldn’t want the variable capture ruby does  
presently, but some might.

---

<div class="post-metadata">

**Author:** ![phrogz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/2acd7d/32.png) [@phrogz](https://www.ruby-forum.com/u/phrogz)\
**Post date:** [October 17, 2006, 8:11pm UTC](https://www.ruby-forum.com/t/re-non-declared-variables/72349/3 "2006-10-17T20:11:58Z")

</div>

Its probably best to have a strict option like in perl and [VB.net](http://VB.net). If  
I am understanding what Daniel is saying properly and ruby works in  
such a way that what should be a typo that generates an error is  
accepted by ruby as a legit variable in spite of the fact that the  
programmer really meant to say count it creates a set of situations  
that are very error prone and that is not a good thing.

---

<div class="post-metadata">

**Author:** ![phrogz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/2acd7d/32.png) [@phrogz](https://www.ruby-forum.com/u/phrogz)\
**Post date:** [October 17, 2006, 8:24pm UTC](https://www.ruby-forum.com/t/re-non-declared-variables/72349/4 "2006-10-17T20:24:00Z")

</div>

Kevin O. wrote:

> Its probably best to have a strict option like in perl and [VB.net](http://VB.net). If  
> I am understanding what Daniel is saying properly and ruby works in  
> such a way that what should be a typo that generates an error is  
> accepted by ruby as a legit variable in spite of the fact that the  
> programmer really meant to say count it creates a set of situations  
> that are very error prone and that is not a good thing.

I very much agree, being a longtime Perl programmer (10+ years). When  
“use strict” was first introduced, a very similar discussion ensued on  
the Perl lists about how it was going to be cumbersome/etc. As Kevin  
says, years later the first thing everyone says in Perl-land is to “use  
strict”, so much that many discussions have considered turning it on by  
default.

My one comment would be to use a common idiom in other languages, like  
“var”, rather than “n”.

BTW, howdy all (first post).

Cheers,  
Nate

---

<div class="post-metadata">

**Author:** ![phrogz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/2acd7d/32.png) [@phrogz](https://www.ruby-forum.com/u/phrogz)\
**Post date:** [October 17, 2006, 8:38pm UTC](https://www.ruby-forum.com/t/re-non-declared-variables/72349/5 "2006-10-17T20:38:09Z")

</div>

On Wed, 18 Oct 2006, Nate W. wrote:

> lists about how it was going to be cumbersome/etc. As Kevin says, years later  
> the first thing everyone says in Perl-land is to “use strict”, so much that  
> many discussions have considered turning it on by default.
> 
> My one comment would be to use a common idiom in other languages, like “var”,  
> rather than “n”.
> 
> BTW, howdy all (first post).

it’s a nice idea, but not as a default, consider Object#method\_missing,  
Module#const\_missing, etc. they rely on being fired when something is  
not  
defined…

-a

---

<div class="post-metadata">

**Author:** ![phrogz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/2acd7d/32.png) [@phrogz](https://www.ruby-forum.com/u/phrogz)\
**Post date:** [October 17, 2006, 8:26pm UTC](https://www.ruby-forum.com/t/re-non-declared-variables/72349/6 "2006-10-17T20:26:00Z")

</div>

On Wed, 18 Oct 2006, Kevin O. wrote:

> Its probably best to have a strict option like in perl and [VB.net](http://VB.net). If  
> I am understanding what Daniel is saying properly and ruby works in  
> such a way that what should be a typo that generates an error is  
> accepted by ruby as a legit variable in spite of the fact that the  
> programmer really meant to say count it creates a set of situations  
> that are very error prone and that is not a good thing.

harp:~ \> ruby -W -e’ @var=42; p @typo ’  
-e:1: warning: instance variable @typo not initialized  
nil

a: it’s hard to follow.

q: why is top posting bad?

-a

---

<div class="post-metadata">

**Author:** ![phrogz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/2acd7d/32.png) [@phrogz](https://www.ruby-forum.com/u/phrogz)\
**Post date:** [October 17, 2006, 10:28pm UTC](https://www.ruby-forum.com/t/re-non-declared-variables/72349/7 "2006-10-17T22:28:38Z")

</div>

On 10/17/06, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> it’s a nice idea, but not as a default, consider Object#method\_missing,  
> Module#const\_missing, etc. they rely on being fired when something is not  
> defined…

Not only that. Consider that the ‘my’ operator in perl is more than  
just a convenient way to get the interpreter to check your spelling  
with the strict pragma. It’s actually there as a scoping construct:  
if you don’t declare a variable with ‘my’, what you get is a package  
(global) variable rather than a lexical.

---

<div class="post-metadata">

**Author:** ![phrogz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/2acd7d/32.png) [@phrogz](https://www.ruby-forum.com/u/phrogz)\
**Post date:** [October 18, 2006, 7:49am UTC](https://www.ruby-forum.com/t/re-non-declared-variables/72349/8 "2006-10-18T07:49:33Z")

</div>

Louis J Scoras wrote:

> if you don’t declare a variable with ‘my’, what you get is a package  
> (global) variable rather than a lexical.

I don’t think it needs to be a default but it could be advisable to  
encourage its use especially among people new to Ruby and/or programming  
in general. I have not come to the point where the things I write in  
ruby are particularly long but I can easily see that as my programs get  
larger and larger I am more likely to run into this quirk.  
Autocompletion is great and all but it’s a bit of a crutch in this case;  
after all what happens if I have to end up coding in notepad or  
something like that? (I think I would just kill myself if I had to code  
anything in notepad…but it could happen.)
