[Web4lib] global variables versus local variables

Brandon Dennis bdennis at shreve-lib.org
Mon May 23 15:33:57 EDT 2005


Depending on the programming language you're using, there's a wide
variety of reasons to use local variables as opposed to global
variables.
 
Not only do you allow for the potential of outside routines to modify
your variable (which is one of the least problematic scenario).
Whenever you use a global variable, you tell the program to track that
variable for the entire runtime and have to coordinate it against every
other global variable out there.   In addition, there's plenty of
language-specific keywords you cannot name your variable without causing
problems.
 
Not to mention trying to debug a program with a global variable floating
around somewhere.  Is it named the same as another global variable,
somewhere hidden deep in the code, that's changing it?   Et cetra.
 
Finally, most structured languages and scripting languages separate
processes out.  So even though the local variables have to be created
each time a sub-routine or loop is created or run-through, it's created
in a small enclosed spaced that is then cleared when the variable is
run.  This requires far less memory to store than adding yet another
global variable to track.  Especially if it's unnecessary.
 
In fact, the best practice is to use local variables plentifully and
global variables sparingly.  In fact, if you find a need to track large
amounts of data across the entire program, it would be better to use
sub-routines with local variables to create a larger variable for the
globals (linked list, arrays, or structs sometimes).
 
I'm certainly no expert, but the less global variables, the less likely
you are to run into increasingly difficult debug problems or resource
issues.  For while it's true that local variables are created and
examined each time the sub-routines are called, global variables are
examined and called each time the main process is called.
 
This is one of the reasons I never liked working with PERL programming,
because it tends to become a mish-mash of spaghetti code that is very
difficult to debug or change.   It may just be the stuff I've looked at,
and I'm sure that quality Perl programmers can do amazing things with
structure.  
 
I don't know much about object-oriented programming, however, and how
global variables relate in those rules.
 
Brandon

	-----Original Message----- 
	From: Karen Coyle [mailto:kcoyle at kcoyle.net] 
	Sent: Mon 5/23/2005 9:38 AM 
	To: John Fitzgibbon 
	Cc: web4lib at webjunction.org 
	Subject: Re: [Web4lib] global variables versus local variables
	
	

	When I was learning programming that had local and global
variables, the
	local variables were billed as being more "safe" because other
routines
	could not inadvertently modify or delete them. You chose local
or global
	depending on the role of the data element in relation to the
	sub-routines, not based on efficiency. And with the speed of
today's
	computers, the difference in efficiency between a global and a
local
	variable may not be significant for most applications.
	




More information about the Web4lib mailing list