Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Global Variable

5 views
Skip to first unread message

Nameir Ismeil

unread,
Jul 26, 1997, 3:00:00 AM7/26/97
to

Hello gus, is it good odea to use too many global variables if yes why,
and if not also why ??
Thanks a lot.

Kurt Watzka

unread,
Jul 27, 1997, 3:00:00 AM7/27/97
to

Nameir Ismeil <nis...@nortel.ca> writes:

>Hello gus, is it good odea to use too many global variables if yes why,
>and if not also why ??

Well, the term "too many" implies that there is something wrong with the
number of global variables, doesn't it. So, the if not part of that
question does not apply.

However, I will still try do say some words about why _I_ think that
the use of global variables should be restricted. Global variables can
be accessed from anywhere. So, if a global variable changes it's
value unexpectedly, it is hard to tell what the reason for that
change might have been. If you _pass_ a reference to a variable to
a function, you know that that function may change it. This makes
code both easier to understand and easier to verify.

Arguments _for_ using global variables are numberous, too. Esp. if they
are well documented and access is restricted to a closely related
set of functions, those arguments may be a strong indication that the
use of global variables may be appropriate in a certain situation,
and even the use of many global variables may be justified.

However, there never is a justification for using too many global
variables, and such questions are merely a figure of style. This
is like asking whether it is justified to take too much of a drug.

Kurt

--
| Kurt Watzka Phone : +49-89-2180-6254
| wat...@stat.uni-muenchen.de

Larry Weiss

unread,
Jul 27, 1997, 3:00:00 AM7/27/97
to

Nameir Ismeil wrote:
> Hello gus, is it good odea to use too many global variables if yes
> why, and if not also why ??


Nameir, what do you think about this issue?

- Larry Weiss

Alicia Carla Longstreet

unread,
Jul 27, 1997, 3:00:00 AM7/27/97
to

Kurt Watzka wrote:

>
> Nameir Ismeil <nis...@nortel.ca> writes:
>
> >Hello gus, is it good odea to use too many global variables if yes why,
> >and if not also why ??
>
> Well, the term "too many" implies that there is something wrong with the
> number of global variables, doesn't it. So, the if not part of that
> question does not apply.
>
> However, I will still try do say some words about why _I_ think that
> the use of global variables should be restricted. Global variables can
> be accessed from anywhere. So, if a global variable changes it's
> value unexpectedly, it is hard to tell what the reason for that
> change might have been. If you _pass_ a reference to a variable to
> a function, you know that that function may change it. This makes
> code both easier to understand and easier to verify.
>
> Arguments _for_ using global variables are numberous, too. Esp. if they
> are well documented and access is restricted to a closely related
> set of functions, those arguments may be a strong indication that the
> use of global variables may be appropriate in a certain situation,
> and even the use of many global variables may be justified.

I try to limit the number of global variables to 10 or less. My feeling
is, if I have more than 20 Global variables I consider redesigning the
system, more than 30 and I definately sit down and rethink my design.

Question: what is the genreal feeling about just how many Global
variables are too many?

--
********************************************
* Alicia Carla Longstreet ca...@ici.net
********************************************
It used to be:
Spare the rod and spoil the child.

Today it's:
Spare the rod to stay out of jail.

Craig Franck

unread,
Jul 28, 1997, 3:00:00 AM7/28/97
to

Alicia Carla Longstreet <ca...@ici.net> wrote:
>Kurt Watzka wrote:

>> Arguments _for_ using global variables are numberous, too. Esp. if they
>> are well documented and access is restricted to a closely related
>> set of functions, those arguments may be a strong indication that the
>> use of global variables may be appropriate in a certain situation,
>> and even the use of many global variables may be justified.
>
>I try to limit the number of global variables to 10 or less. My feeling
>is, if I have more than 20 Global variables I consider redesigning the
>system, more than 30 and I definately sit down and rethink my design.
>
>Question: what is the genreal feeling about just how many Global
>variables are too many?

I don't think you can come up with a number. Real global data (not
just file scope) must be justified with each use.

There is the basic principle that the scope of a variable should be
as minimal as possible. Global and file scope variables seem to
violate this.

There is also the principle that any interface should be as clean as
possible. This is why we pass variables to functions rather than let
them be pulled out of thin air when they are needed. If you were to
create a funtion to draw boxes, you would probably pass it coordinates,
size, style, ect. You should be able to place it in its own .c file;
this certainly helps with software reuse. It shouldn't depend on
existing in another file or having access to global data. (That is a
trivial example, but it demonstrates what I meant by a "clean
interface".) Certain functions that needed to be wedded together could
use file scope varibles. rand and srand come to mind, so there are no
absolutes.

There is also the point that access routines -- functions you call
to manipulate data -- can give you the all the benefits of global
data without as many drawbacks. You get data abstraction and the
ability to build a firewall around the data. You also created a
form of protected data; it is shared with those who need to know,
but not the entire project.

In a nontrivial example, say a flight simulator, you could have a
global state struct which kept track of the plane in its virutal
world (worst). You could also create functions that got passed a
pointer to it and manipuled the state struct (somewhat better).
Or, you could hide the whole thing behind access functions that
updated and querried the state information, completely hidden away
(best). You could change the representation of the information much
more easily with the abstract state information. You could also
control which routines have access to the update and querry
function. You could build sanity checks on exactly what state the
plane was in much more easily. As the flight simulator grew in
size, the complexity curve of the code would stay much flatter
with the third example, than the first two.

--
Craig
clfr...@worldnet.att.net
Manchester, NH
Most rock journalism is people who can't write interviewing
people who can't talk for people who can't read. -- Frank Zappa


Steve Jones - JON

unread,
Jul 28, 1997, 3:00:00 AM7/28/97
to

Nameir Ismeil <nis...@nortel.ca> writes:

>
> Hello gus, is it good odea to use too many global variables if yes why,
> and if not also why ??

> Thanks a lot.

Okay here is an actual real life (tm) examples of globals in C going horribly
wrong.

I had a piece of code that handled input from a pipe, there was a table
of message ids to functions, pass in the message id to get the function,
normal form of thing allowing people to register functions at start up.
Now this was global so as to allow people to register their functions.
The problem came when someone else copied the code letter for letter changing
only the function names but leaving the variable names the same. The two
objects were then linked into on HUGE executable and run in seperate threads.
The problem arose because of the common name for the variable, which resolved
to one run-time variable. Hence functions in one tasks were being activated
from within another task, with predictable results.

The solution was to provide function interfaces to an internal store that
was local to the given unit and thus provide an extra layer of security.


--
|Un Loup en France | Wolverhampton Wanderers, out of darkness cometh Bully|
|---------------- C++ is to OO what C is to structured --------------------|
|----The above opinions rarely reflect my own and never my employers'------|
|Do not add me to mailing lists violations will be billed for time. |

David Starr

unread,
Jul 28, 1997, 3:00:00 AM7/28/97
to

Steve Jones - JON wrote:
>
>
> The problem came when someone else copied the code letter for letter changing
> only the function names but leaving the variable names the same.

When we talk about code reuse, we are talking about copying the code letter for
letter, or even better, linking in a compiled object module. Steve is describing what
happens when you attempt to re use code with global variables. Code that doesn't use
global variables is a lot easier to re use on the next project.
If the code takes all inputs as calling arguments it is easier to debug.
You just put a break point at the entrance to the routine and check the calling
arguments. If they are wrong, it's the caller's fault. If they are OK, it's the
module's fault. On the other hand, if a global variable is directing the code to do
soemthing foolish, its a lot harder to find and fix it. You have to grep thru every
module in the program looking for the one or ones that access the global and then you
have to debug each one to see who trashed the global.

0 new messages