Non-declared Variables

Non-declared Variables

Post by Avata » Tue, 17 Oct 2006 20:41:28

I would like to hear people's opinions on the ability to use variables
without declaring them in dynamic languages like Ruby.

It would seem to me that an argument for non-declared local variables
that typically occupy a small scope could be made. But, class
variables? What is the benefit in allowing for runtime definition of
class variables?

Are there real tangible benefits that non-declared, dynamically typed
(at binding time) variables provide? Or do dynamic variables simply
create less compile time errors and more (harder to catch) runtime


Non-declared Variables

Post by Marco van » Tue, 17 Oct 2006 20:58:29

Yes? What would it really make possible? Or save?

E.g. in Delphi I press a key, and the variable of a for loop is auto
declared locally. I can check and change the type if I don't like it (e.g.
promote it to 64-bit integer), without much effort, but with an additional
safeguard that I can verify it is actually the type I mean it to be.


Non-declared Variables

Post by cr8819 » Tue, 17 Oct 2006 21:29:02

I am of mixed feelings here.

well, my lang lacks classes, so they are more technically object variables.
they can be created absent declaring them, but mostly this is more a
side-effect of the implementation than a design feature.

imo, ideally, declaration would be forced.

I more lean in favor of the latter.

however, it can be argued that there are cases where code can be so ad-hoc,
or relying on dynamic behavior, that it makes sense to allow implicit
variable creation (eg: using a mixin or similar that likes creating a few
variables for its own uses, ...).

actually, in this case the rule can be made that it be made at least vaguely

or more so:
float self.x=3;
float self.y=4;


as the former at least makes it clear that the variables belong to 'self',
wheras as far as the latter is concerned, the variables are mistyped lexical

however, for now, the vm is fairly lax about this (among other things...).

a problem is that prototype oo makes things like whether or not object
variables are declared before use particularly difficult to test at compile

this problem is at least a little easier with class/instance OO...


Non-declared Variables

Post by Robbert Ha » Tue, 17 Oct 2006 23:11:48

n Mon, Oct 16, 2006 at 04:41:28AM -0700, Avatar wrote:

The way I see it, there are various competing benefits:

1. Succinctness (how much code is necessary to accomplish something)
2. Flexibility (what does the language allow)
3. Intellegibility (how well can code be read and understood)
4. Reliability (how reliable are programs when run)

How having to declare variables before they are used affects these
depends on several other factors, e.g. what needs to be in the
declarations, how do the scoping rules for the language work, what form
do the declarations take, etc. Some examples:

Take Ruby. In Ruby, you don't need to declare variables; in fact, you
can't. Obviously, you can't get more succinct than that. So Ruby does
well there.

If you assign to a variable that isn't in scope, it will be created in
the current scope. This is confusing: if the variable _is_ in scope, it
will be overwritten. The scoping rules are different for blocks than for

def foo
x = 12
def bar
x = gets
puts x


def foo
x = 12
(proc { x = gets; puts x }).call

do different things. To me, this means that Ruby's way is bad for

Ruby is very flexible. You can introduce variables and methods anywhere
you want to, and this doesn't go just for the top level, but also for
nested scopes, objects, and classes. All of this is very simple to do.

The downside of the flexibility is that lots of things depend on the
control flow, and control flow cannot (generally) be checked at compile
time. As a result, there is absolutely no guarantee that a program that
passes the checker will actually work correctly.


For a completely different example, let's look at C. In C, variables and
functions must be declared before they are used, and declarations must
include explicit types. This is very verbose.

At least in older versions of C, variables would have to be declared at
the beginning of a block. There is no way to add variables on the fly.
The same restriction is present and more rigorous for global variables,
functions, and struct fields: you have what has been declared, and
nothing more. No flexibility.

The upshot of all this is that it's very clear: you know which scope
every variable belongs to, what type of value it holds, etc.
Intellegibility is good.

Requiring that everything be declared before it is used also aids
reliability. Unfortunately, C programs' reliability is twarted by the
fact that declaration doesn't imply setting variables to useful
values. You can happily say:

int i;
return i + 1;

and have idea what you're going to be returning.

Of course, the unsafety of C's type system (unchecked casts, no bounds
checks, pointer arithmetic, ...) makes reliability a pipe dream anyway,
but that's a separate story.


Now that we've analyzed two examples, let's see if we can come up with
something that combines the good parts of each. I think we can. What we
can distill from the above is:

1. Declaration and assignment should be different things, to avoid
confusion about which scope a variable belongs to. This obviously
doesn't apply to languages with only one type of scoping, but these
languages have other problems.

2. For reliability reasons, it should be mandatory to initialize
variables before they can be used. This initialization should be merged
with the declaration

Non-declared Variables

Post by carmen mir » Wed, 18 Oct 2006 11:53:24

The language VBScript has an option for declaring explicitly. The idea is
to avoid the possibility that your variable will get spelled incorrectly.
Unless it is explicitly checked by the de *** , then the de *** will not
find that error. But if you are sharing the variable among a few
functions it would be better to declare it once. It avoids reference to
data produced in functions that were not called. Its all about the de ***
in this messager opinion. Microsoft tutorials use dynamic class
assignment, which is from your point on it that it occurs to me. It works
well. The compilers are getting alot more sensitive in reports in research
i have been reading on SQL Server issues.

above is a site on objects, methods, etc. i think a search there might say
more. Watch the spelling or declare option explicit