An Ethics Question (global variables)

Richard A. O'Keefe ok at quintus.UUCP
Fri Apr 14 12:11:07 AEST 1989


>Global variables or passed parameters?  I'm anxiously awaiting the pros
>and cons of this issue.  Thank you.

There are lots of good books which explain the reasons why global
variables are frowned on and what the alternatives are.  Ledgard and
Tauer, "Professional Software, Vol 2, Programming Practice" is one I
like.  One thing to note is that C hasn't _got_ most of the
alternatives (though 'static' variables are a help).

I try to arrange my programs so that any chunk which looks as though it
might be useful later on can be pulled out as a self-contained "module".
Even when writing filter programs, I write them as functions called
from main with stdout and (stdin or whatever the command line arguments
asked for) as arguments, so that if I ever have a use for that filtering
operation in an O/S which doesn't support popen() -- can you say CMS? --
I can just pick it up and use it.

What the big names in software engineering are really worried about is
not global variables as such, but the nasty messes you can get in when
part of your program has a _hidden_ communication channel with another
part.  Just by looking at a function call, you cannot tell what it might
be using, and worse, what it might be changing.  UNIX's "cxref" tool is
a help, but it could be a lot more helpful.  (Dear old B6700 Algol, sob.)



More information about the Comp.lang.c mailing list