SitePoint Sponsor

User Tag List

Results 1 to 2 of 2

Thread: Local VS Global

  1. #1
    Under Construction Poop_Shoot's Avatar
    Join Date
    Jul 2003
    Location
    Sacramento, CA
    Posts
    330
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Local VS Global

    I was just wondering what the pro's and con's of Local/Global Variables? I am rather confused as to when I should use what? I would just declare everything as a Global Variable but I know that would and has caused me problems! So if I could some insight Please?

  2. #2
    I'll take mine raw silver trophy MikeFoster's Avatar
    Join Date
    Dec 2002
    Location
    Alabama, USA
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Poop_Shoot,

    Here's a few quick thoughts...

    Code Reuse: If you design your program so that the functions don't use globals, then you are much closer to having reusable functions. The functions are a step closer to being loosely coupled. They rely less on each other, and use less shared data, so they are more generic.

    Maintenance: It's much easier to maintain a program written with no (or less) global variables. When a program has many global variables it is much easier to cause unintentional side-effects because many different functions may access those globals.

    Development Efficiency: Designing programs/functions properly will save you time in the long run - you'll be a lot less likely to have to re-design.

    Ahhhh... Let's face it, nothing beats looking at your code and thinking... now that's elegant, baby!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •