PyMyth: Global variables are evil... WRONG!
Steven D'Aprano
steve at pearwood.info
Thu Nov 14 00:50:40 EST 2013
On Wed, 13 Nov 2013 19:45:42 -0800, Rick Johnson wrote:
> On Wednesday, November 13, 2013 8:45:16 PM UTC-6, Steven D'Aprano wrote:
>> A fully-auto machine gun with a hair-trigger and no
>> safety is no different from a single-barrel shotgun with a safety and
>> a trigger lock! You can blow your foot off with both!
>
> Yes. But in the case of the shotgun you'd have to find the key, unlock
> the trigger, unset the safety, and aim REALLY well... because your only
> getting off one shot, and if you miss or only wound me, i've got that
> full auto machine gun with hair trigger and no safety -- i won't need to
> live very long to return the favor. >:)
I think you're missing the point that we're talking about the coder
shooting themselves in the foot, not Gunfight At The OK Corral. There's
no "favour" to return.
>> Yes, but in the first case you can do it by accident, while with the
>> second you have to work hard to blow your foot off.
>
> Uh huh.
>
> And why do i have the sneaking suspicion that the BIG SCARY ASSAULT
> WEAPON is referring to my argument and the "old shot-gun that Grandpa
> has in the closet under lock and key" is your argument.
That's the most insightful observation you've made in this thread so far.
Yes, the point is that process-wide global variables are demonstrated by
50+ years of programming experience to be best avoided (in general --
there are caveats and exceptions of course). We're talking about probably
millions of person-hours of experience leading to the conclusion that
"globals are harmful".
It isn't that global variables will leap out of the computer and attack
you while you sleep, of course, but that in general *its too damn hard*
for human programmers to write good, reliable, maintainable, correct
(i.e. bug-free) code using process-wide global variables. Global
variables are the spaghetti code of namespacing -- everything is mixed up
together in one big tangled mess.
The more global variables you have, the worse the tangle. One or two is
not too bad. With good conventions for encapsulation to limit the amount
of tangled, coupled code (e.g. naming conventions, or limiting globals to
a single module at a time by default) the amount of harm can be reduced
to manageable levels.
Global variables increases coupling between distant parts of the code. I
remember a nasty little bug in Windows where removing IE stopped copy-and-
paste from working everywhere. That's a sign of excess coupling between
code -- there's no logical reason why removing a web browser should cause
copying text in Notepad to fail. We want to avoid unnecessary coupling:
opening your fridge door shouldn't flush the toilet.
Since global variables increase coupling, and coupling is often harmful,
one way to avoid unnecessary coupling is to avoid unnecessary global
variables. Think of them as leaches. Even today, there are good uses for
medical leaches:
http://en.wikipedia.org/wiki/Hirudo_medicinalis
One or two leaches is hardly noticeable. Ten or twenty starts being a bit
of a problem. A few dozen and you'll certainly notice, and if you're
covered in multiple hundreds of them, you're at risk of dying from blood
loss, never mind the risk of infection.
--
Steven
More information about the Python-list
mailing list