[AccessD] Global Variable

DWUTKA at marlow.com DWUTKA at marlow.com
Mon May 16 13:49:43 CDT 2005


You know, I think this whole debate could be avoided, again, if it was just
stated that using global variables when a 'lesser' scope should be used, is
bad practice.

I know I get into these discussions, because I am self taught, and it takes
a LOT more energy to overcome incumbent thought obstacles then it does to
learn new things.

Drew

-----Original Message-----
From: John W. Colby [mailto:jwcolby at colbyconsulting.com]
Sent: Monday, May 16, 2005 12:31 PM
To: 'Access Developers discussion and problem solving'
Subject: RE: [AccessD] Global Variable


Gustav,

There are many things generally considered poor programming practice in the
programming industry.  Excessive use of Gotos and Global variables are a
couple of examples.  In general they ARE CONSIDERED (by myself and many many
many other people) to be bad programming practices.  

Globals, like Gotos are handed down from the days of old when there was no
such thing as functions, data types declarations, scope and the many other
tools that make modern programming worlds above the code written 40 years
ago.  All of these things exist for a reason.  Globals are considered poor
programming practice because they expose variables to update and even
overwriting by code that has no business updating that variable.  

It is generally accepted practice that variables should have the minimum
exposure absolutely necessary to perform their function.  A variable used
ONLY in a specific function should NOT EVER be declared as a global
variable.  A variable used ONLY in a module should NOT EVER be declared a
global.  Microsoft and other compiler writers provide tools like variable
scope for a good reason, they help minimize, and track down bugs.  If a
variable is private to a module then if that variable is being updated
incorrectly, you at least know it is because of some code in that module.
If it can be updated anywhere in the entire program then you must search the
entire program when something goes wrong.  Function level variables
(declared inside a function) are local to the function and cannot be
declared otherwise.  

Thus the "excessive use" of globals would include making a variable,
intended for use in a module, global "just in case".  

I personally make it a practice to always explicitly declare module level
variables private 

Private MyVar as SomeVarType

unless there is an over-riding reason to expose the variable on a global
basis.  Even then I make it a practice to build a function that reads a
private variable in those cases where code in other modules must be able to
read the variable.  Only in cases where code in multiple modules must both
read and write a global variable will I expose the variable itself as a
global.  And even in those cases it is sometime useful to cause the write to
go through a function, particularly if the variable is a computed value.

Global variables is one of those issues that strikes a sensitive note with
many programmers, but it is indeed widely accepted to be poor programming
practice to just use them willy-nilly and for no good reason.  Every tool
exists for a reason and variable scope is a very powerful tool to assist the
programmer in writing better code.

John W. Colby
www.ColbyConsulting.com 

Contribute your unused CPU cycles to a good cause:
http://folding.stanford.edu/

-----Original Message-----
From: accessd-bounces at databaseadvisors.com
[mailto:accessd-bounces at databaseadvisors.com] On Behalf Of Gustav Brock
Sent: Monday, May 16, 2005 12:34 PM
To: accessd at databaseadvisors.com
Subject: RE: [AccessD] Global Variable


Hi John

You may like or prefer or need, or not, a global variable - that's up to
everyone to decide for him/herself - but they are not "bad practice", not
even in general. You may write clumsy code using globals, but not using them
gives no guarantee for nice code.

/gustav


-- 
AccessD mailing list
AccessD at databaseadvisors.com
http://databaseadvisors.com/mailman/listinfo/accessd
Website: http://www.databaseadvisors.com



More information about the AccessD mailing list