Author Topic: Global Variables Bad?  (Read 1290 times)

0 Members and 1 Guest are viewing this topic.

Offline S3THST4

  • Contributor
  • Jr. Member
  • *
  • Posts: 81
  • LOL, Women's Rights!
  • Field: Software Development
  • Real Name: Seth Jacoby
  • Favorite OS: Linux
  • Programming Language: C#
Global Variables Bad?
« on: September 30, 2009, 03:28:51 AM »
So, I've heard from people that using global variables is bad technique/practice. If this is true, why are they bad? I find them quite useful.
Eric Clapton is God.


Offline AJ32

  • TCC Webmaster
  • Administrator
  • Cube Head
  • *
  • Posts: 1,403
  • "TCC Fuhrer"
    • Fox Software
  • Field: Software & Web Development
  • Real Name: Andrew Warner
  • Favorite OS: Windows
  • Programming Language: PHP
Re: Global Variables Bad?
« Reply #1 on: September 30, 2009, 03:44:40 AM »
Well, I never really thought they were so bad, in fact in php I use them all the time. But if I had to guess:

1. They take up a lot of memory
2. They can be changed from anywhere, from any class or file, so they can't be depended on as much as static variables.

Just my guess at it, if you want to change data from anywhere that the whole program relies on (like I do with PHP) I see no problem other than the memory issue.  :-\

"Data is eternal, immortal, immutable. Even when the engineers and intelligence
behind its creation are gone, information, facts, and knowledge never die."

Offline gbertoli3

  • Expert
  • Sr. Member
  • *
  • Posts: 1,169
  • My Code Is In The Cube
    • Bertoli Family
  • Field: Software & Web Development
  • Real Name: Guido Bertoli
  • Favorite OS: Windows
  • Programming Language: C#
Re: Global Variables Bad?
« Reply #2 on: September 30, 2009, 05:49:36 AM »
I had no idea that they were considered a 'bad' practice, but then again I haven't really used global variables that much, maybe just once or twice.

Offline KYA

  • Charter Member
  • Full Member
  • *
  • Posts: 596
  • #include <nerd.h>
  • Field: Software Development
  • Real Name: Knowles Atchison, Jr.
  • Favorite OS: Windows
  • Programming Language: C++
Re: Global Variables Bad?
« Reply #3 on: September 30, 2009, 07:37:31 PM »
Inherently, they aren't bad, but generally you'll be working on a project with multiple people and everyone doesn't know your thought process like you. Another rule of thumb is to keep scope as small as possible. This allows for more efficient bug hunting. If you get an error, wrong result, etc... you can quickly narrow it down to a module or function to step through to figure out what the issue is. With a global variable, it could be modified anywhere, so the hunting takes longer.

Offline Rayne

  • Contributor
  • New TCC Member
  • *
  • Posts: 17
  • Resident Witch (Male, stop assuming otherwise)
    • The Wiccan Secks Hut
  • Field: Software Development
  • Real Name: Anthony Simpson
  • Favorite OS: Linux
  • Programming Language: Other
Re: Global Variables Bad?
« Reply #4 on: October 13, 2009, 01:43:24 PM »
They introduce dependencies in your code that are hard to untangle. If you have a global variable that's modified in about 10 different modules, how will (you in the future) anyone else know what's modifying it at whatever time? It becomes a big mess. This is why I love the way Clojure works. It discourages mutable state, and provides ways around it most of the time. It allows you to use mutable state when you really need it, but you have to be explicit about it with refs, atoms, and agents.
Acidrayne - Acidrayne - My website and blog.
Clojure - The Lisp that makes the JVM dynamic.
Haskell - An advanced purely functional programming language that puts the funk in funktion.

Offline S3THST4

  • Contributor
  • Jr. Member
  • *
  • Posts: 81
  • LOL, Women's Rights!
  • Field: Software Development
  • Real Name: Seth Jacoby
  • Favorite OS: Linux
  • Programming Language: C#
Re: Global Variables Bad?
« Reply #5 on: October 19, 2009, 03:05:33 AM »
Thanks for the responses, guys  B)
Eric Clapton is God.