26-01-2005, 11:32
|
|
|
חבר מתאריך: 20.12.01
הודעות: 20,962
|
|
Global Variables Are Bad
[התמונה הבאה מגיעה מקישור שלא מתחיל ב https ולכן לא הוטמעה בדף כדי לשמור על https תקין: http://c2.com/sig/wiki.gif] Global Variables Are Bad
This is something I have a hard time putting in words. I've been bitten by globals
in the past, so I 'know' they're 'bad', but for the life of me, I can't explain why. What
I'd like is to have some straightforward "Here's why globals are bad" document I
can point other people to, preferably with some concrete (if toy) examples. As with
all HeuristicRules, this is not a rule that applies 100% of the time. Code is generally
clearer and easier to maintain when it does not use globals, but there are exceptions.
It is similar in spirit to GotoConsideredHarmful, although use of global variables is
less likely to get you branded as an inveterate hacker.
Why Global Variables Should Be Avoided When Unnecessary
- Non-locality -- Source code is easiest to understand when the scope of its
individual elements are limited. Global variables can be read or modified by
any part of the program, making it difficult to remember or reason about every
possible use. - No Access Control or Constraint Checking -- A global variable can be get
or set by any part of the program, and any rules regarding its use can be
easily broken or forgotten. (In other words, get/set accessors are generally
preferable over direct data access, and this is even more so for global data.) - Implicit coupling -- A program with many global variables often has tight
couplings between some of those variables, and couplings between variables
and functions. Grouping coupled items into cohesive units usually leads to
better programs. - Concurrency issues -- if globals can be accessed by multiple threads of
execution, synchronization is necessary (and too-often neglected). - Namespace pollution -- Global names are available everywhere. You may
unknowingly end up using a global when you think you are using a local (by
misspelling or forgetting to declare the local) or vice versa. Also, if you ever
have to link together modules that have the same global variable names, if
you are lucky, you will get linking errors. If you are unlucky, the linker will
simply treat all uses of the same name as the same object. - Memory allocation issues -- Some environments have memory allocation
schemes that make allocation of globals tricky. Also, when dynamically linking
modules, it can be unclear whether different libraries have their own instances
of globals or whether the globals are shared.
Why the Convenience of Global Variables Sometimes Outweighs the Potential Problems- When global variables represent facilities that truly are available throughout
the program, their use simplifies the code. (Even in these cases, it's wise to
consider using the SingletonPattern to control access to this facility; if this
facility ever becomes non-global it will make such changes significantly
easier.) - Some programming languages provide no support or minimal support for
non-global variables. - In a very small program, using globals can be the simplest thing that works.
(But beware that small programs have a tendency to turn into large programs.) - Some people jump through very complicated hoops to avoid using globals.
Many uses of the SingletonPattern are just thinly veiled globals. If something
really should be a global, make it a global. Don't do something complicated
because you might need it someday. If a global variable exists, I would
assume that it is used. If it is used, there are methods associated with it.
Colocate those methods in a single class and one has created a singleton. It
really is better to specify all of the rules for use of a global variable in one
place where they can be reviewed for consistency. The veil may be thin, but
it is valuable. Really Bad Reasons to Use Global Variables - "What's a 'local variable'?"
- "What's a 'data member'?
- "I'm a slow typist. Globals save me keystrokes."
- "I'm not sure in what class this data belongs, so I'll make it global."
link: C2.com wiki: Global Variables Are Bad
אמנם אני לא מומחה גדול, אבל מה שהם כותבים נשמע לי הגיוני...
|