Avoidinig globals

T

Tim

Is there a way to force a sub to only use global variables when
explicitly told to?

I mean that if I forget to dim a a local variable but there is already
such a variable in the global scope then my function will use the global
variable without me intending it too.

Here is an example:

<html>
<% Option Explicit
sub corect()
Dim var
var=5
end sub

sub wrong()
var=5
end sub

Dim var
var=2
response.write(var) 'prints rightly 2
call corect()
response.write(var) 'Still prints rightly 2
call wrong()
response.write(var) 'Now it prints 5 because I forgott to Dim var
%>
<html>

I would have liked to be able to declare a Globals Explicit this should
force me to write


sub wrong()
global var=5
end sub

If I wanted wrong to change the behavior of the globaly defined variable
var.

Is there a way to acomplish this in ASP?

Thanks

Tim
 
M

Mark Schupp

Either don't use globals at all (as in following example) or use a naming
convention for global variables like "g_var" so that you only use them
intentionally in subroutines.

<%@ Language=VBScript %>
<%
option explicit

call main

Sub Main()
Dim var
var=2
response.write(var) 'prints rightly 2
call corect()
response.write(var) 'Still prints rightly 2
call wrong()
response.write(var) 'Now it prints 5 because I forgott to Dim var
End Sub

sub corect()
Dim var
var=5
end sub

sub wrong()
var=5
end sub
%>
 
T

Tim

Mark said:
Either don't use globals at all (as in following example) or use a naming
convention for global variables like "g_var" so that you only use them
intentionally in subroutines.

Both solutions are workable but none are really good I think.

But if there is not globals explicit kind of directive I guess they are
as good as they will be.

Thanks

Tim
 
P

Phill. W

Tim said:
I mean that if I forget to dim a a local variable but there is already
such a variable in the global scope then my function will use the
global variable without me intending it too.

Welcome to the World of Naming Conventions.

There are those who sneer at prefixes of /any/ kind on variable
names, because they know (or, rather, their mega-expensive
IDE tells them) what DataType a variable is and whether it's
Private, Public, Global or whatever.

Those of us in the Real World, who still have to fight the Good
Fight [occasionally] with the likes of Notepad prefer to be able
to read this kind of thing for ourselves.

At the /very/ least, include a "Scope" prefix on your variable names,
as in

iCount ' [local] Integer variable
smCount ' String Variable [at *M*odule (or class) level]
lgCount ' Long variable [*G*lobal]

That way, you /can't/ get your variables mixed up.

HTH,
Phill W.
 
T

Tim

Phill. W said:
Welcome to the World of Naming Conventions.
At the /very/ least, include a "Scope" prefix on your variable names,
as in

iCount ' [local] Integer variable
smCount ' String Variable [at *M*odule (or class) level]
lgCount ' Long variable [*G*lobal]

That way, you /can't/ get your variables mixed up.

It sure helps but it is not fool proof, atleast not when I am the fool
in question.

I start the project out as a single page and use global variables
(reasonable).

When the project grows I realize I should move some stuf to functions
and / or include files. Then I copy and past code and all the names come
a long.

Of course, then I should go through my copied code and change the
prefixes but if I miss one variable there is a rather hard to find error
in my code.

A globals explicit would help prevent this kind of errors (or at least
make them easy to spot). To me it is such an obviously good feature I
assumed it should be in there somewhere but I realize I cannot get what
is not there :(


Tim
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top