לוגו אתר Fresh          
 
 
  אפשרות תפריט  ראשי     אפשרות תפריט  צ'אט     אפשרות תפריט  מבזקים     אפשרות תפריט  צור קשר     חץ שמאלה ‎print ‎"Hello World!"; if‎ ‎not rules.‎know ‎then rules.‎read(); חץ ימינה  

לך אחורה   לובי הפורומים > מחשבים > תכנות ובניית אתרים
שמור לעצמך קישור לדף זה באתרי שמירת קישורים חברתיים
תגובה
 
כלי אשכול חפש באשכול זה



  #1  
ישן 26-01-2005, 11:32
  AlexKarpman AlexKarpman אינו מחובר  
 
חבר מתאריך: 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




אמנם אני לא מומחה גדול, אבל מה שהם כותבים נשמע לי הגיוני...

תגובה ללא ציטוט תגובה עם ציטוט חזרה לפורום
תגובה

כלי אשכול חפש באשכול זה
חפש באשכול זה:

חיפוש מתקדם
מצבי תצוגה דרג אשכול זה
דרג אשכול זה:

מזער את תיבת המידע אפשרויות משלוח הודעות
אתה לא יכול לפתוח אשכולות חדשים
אתה לא יכול להגיב לאשכולות
אתה לא יכול לצרף קבצים
אתה לא יכול לערוך את ההודעות שלך

קוד vB פעיל
קוד [IMG] פעיל
קוד HTML כבוי
מעבר לפורום



כל הזמנים המוצגים בדף זה הם לפי איזור זמן GMT +2. השעה כעת היא 04:56

הדף נוצר ב 0.04 שניות עם 10 שאילתות

הפורום מבוסס על vBulletin, גירסא 3.0.6
כל הזכויות לתוכנת הפורומים שמורות © 2024 - 2000 לחברת Jelsoft Enterprises.
כל הזכויות שמורות ל Fresh.co.il ©

צור קשר | תקנון האתר