[Tutor] global is bad but ...

jim stockford jim at well.com
Wed Nov 14 01:21:32 CET 2007


On Nov 13, 2007, at 3:11 PM, Alan Gauld wrote:

> Rather than answer your question directly can I ask, do
> you know *why* wiser heads say global is bad? What
> problems does using global introduce? What problems
> does it solve?


i'll try:

    globals are good because they provide common data to
one's entire program without issues of scope.

    globals are bad because one can design (or just allow to
happen) software in which globals are changed by
different entities within the program without coordination.
    the classic:
globflag = True
proc_1 checks globflag and starts to perform accordingly
proc_2 changes globflag to False for some good reason
before proc_1 has finished, and enough before so that
there's trouble.

    how to get the good without the bad? in a small program,
be a disciplined coder. in a large program, wrap the globals
in some function wrapper that doesn't easily allow changes
to the global data. in the above case, write some kind of
not_yet code to keep proc_2 from changing globflag until
after proc_1 is finished.

    okay, i tried. so why are globals bad and what problems
do they solve?



More information about the Tutor mailing list