Let ME try for a new topic... (externals)
Henry Spencer
henry at utzoo.UUCP
Sun Jun 10 07:54:55 AEST 1984
Dick Dunn comments:
>Sorry, Danny, but I can't resist: my favorite way of dealing with
>the problem is to avoid external variables. Passing information as
>parameters and returned values is a much cleaner way of communicating
>between different modules...
Unfortunately, this has nothing to do with the issue. Parameters and
global variables solve two different problems; it's comparatively seldom
that you have a real choice as to which is appropriate. Please remember
that parameters have limited scopes and extents (lifetimes).
He's made the same mistake as several people that sent mail to me
privately: he has confused the words "external" and "global". I did
not say "global variables are bad", I said "external variables are bad".
I never said that variables with longer lifetimes than a single function
invocation are a poor idea, because often they are both necessary and
appropriate. What I said was that variables that are visible all over
the place, i.e. are used for "communicating between different modules",
are bad. Such communication is much simpler and cleaner if it goes by
a more restricted interface, i.e. function calls.
I use global variables a lot. But practically all of them have the
magic word "static" in their declarations, so they are visible only
within the source file (the handiest definition of "module" in C) in
which they are defined. The number of external variables I use, even
in large many-module programs, can be counted on one's thumbs. I have
experienced no trouble in working this way, and I highly recommend it.
Such programs are a lot easier to understand than almost anything that
needs an "externs.h" file.
--
Henry Spencer @ U of Toronto Zoology
{allegra,ihnp4,linus,decvax}!utzoo!henry
More information about the Comp.unix.wizards
mailing list