473,410 Members | 1,952 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,410 software developers and data experts.

Avoidinig globals

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
Jul 22 '05 #1
4 1350
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
%>

--
--Mark Schupp
Head of Development
Integrity eLearning
www.ielearning.com

"Tim" <al@gahnstrom.se> wrote in message
news:MC*******************@newsb.telia.net...
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

Jul 22 '05 #2
Tim
Mark Schupp wrote:
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
Jul 22 '05 #3
"Tim" <al@gahnstrom.se> wrote in message
news:MC*******************@newsb.telia.net...
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.
Jul 22 '05 #4
Tim
Phill. W wrote:
"Tim" <al@gahnstrom.se> wrote in message
news:MC*******************@newsb.telia.net...
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.

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
Jul 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: lawrence | last post by:
I get the impression that most people who are using objects in their PHP projects are mixing them with procedural code. The design, I think, is one where the procedural code is the client code, and...
5
by: Frostillicus | last post by:
I'm trying to use array_multisort to sort by one of the dimensions of an array stored in $GLOBALS like this: array_multisort($GLOBALS, SORT_STRING, SORT_DESC); Each "row" in $GLOBALS contains...
7
by: John | last post by:
Hi, I'm looking for the best way to deal with globals in PHP. As a 'C' software developer, I would normally avoid all globals and not have any at all, but use structs and pass everything in...
3
by: Robert Dodier | last post by:
Hello, I'm interested in introducing new variables into the environment of a Python interpreter or program. In reading through old posts to this newsgroup, I see there is an often-repeating...
45
by: It's me | last post by:
I am new to the Python language. How do I do something like this: I know that a = 3 y = "a" print eval(y)
2
by: DFS | last post by:
Not sure whether it's bad programming practice or not, but I have a module of globals I declare in each system: Global ws As Workspace Global db As Database Global td As TableDef Global rs As...
2
by: xml0x1a | last post by:
How do I use exec? Python 2.4.3 ---- from math import * G = 1 def d(): L = 1 exec "def f(x): return L + log(G) " in globals(), locals() f(1)
5
by: Steven W. Orr | last post by:
I have two seperate modules doing factory stuff which each have the similar function2: In the ds101 module, def DS101CLASS(mname,data): cname = mname+'DS101' msg_class = globals() msg =...
1
by: cokofreedom | last post by:
if __name__ == '__main__': print "Globals (For Loop):" try: for i in globals(): print "\t%s" % i except RuntimeError: print "Only some globals() printed\n" else: print "All globals()...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.