What Did That Mean? March 18, 2006Posted by gordonwatts in computers, physics.
Further evidence of exactly how much of a nerd I am… Pyracantha writes:
I don’t get it. Please explain for us fans?
in response to my super nerdy utterance “Using global variables is like checking baggage for a plane flight.”
Well. This is going to be one of those things that isn’t as good after the explanation is done. But next time you hear it…
Checking baggage is bad. If you are a frequent flier it is inevitable that they will loose your luggage. For some reason it is your luggage, not the person’s luggage that flies once a year. Thus, as a frequent flier, you learn to never check. It can be a bit of a religion. And there is nothing worse than fling with someone that has checked their bag when you’ve not. Never mind they had less to carry during that rushed plane change in Chicago — you can’t help but think about all the time you could be spending driving home, or on the way to the rental car… instead you are waiting for this persons luggage to arrive. And if you arrive at the airport early and there is an earlier flight — well, if you’ve checked you can pretty much forget trying to make that flight.
Ok. Don’t check baggage. It is bad.
Now — the global variable thing. Writing programs is just like any other type of problem solving. You start with an overall project: Calculate the efficiency of finding a jet of particles. But you can’t solve it: it is too big. So you split it into smaller problems: First, find a set of jets. Second, decide how many are clean. Third, divide the second number by the first: voila, an efficiency! Each of those steps might have a sub-steps. If it is a really tough problem it could have 100’s of steps and you may have different people working on different steps. And you hope that when you put all the steps together… bam! it works the first time and you get an answer. Ok. That doesn’t happen.
Now, lets say John is working on step 5, and Jane is working on step 22. Think of how much more complex the task would be if John changed a small behavior in his step and that affected Jane as well? Jane would never be able to work on her step until John was finished with his step. That would suck.
A global variable is a perfect way for this to happen. It is global — that is, visible to all steps in the problem. Anyone can modify it. The only way to keep sanity is to have everyone swear to use the global variable in a certain way: it is a verbal contract. The contract isn’t written in code. Anyone can violate it. Perhaps someone new comes along, changes it, and suddenly all the results go pear shaped. And it may seem like what is messed up is far away from any changes. Yuck.
So. Don’t do global variables.
That said, sometimes global variables are the right way to go. It is just too difficult to program around them. The programming language you use can also affect how often you use them. FORTRAN: a lot. C++ less. 🙂 So, like everything else, you have to use them wisely. Kevin also commented on that post:
Yeah, but how do you keep application-wide parameters in event-driven programs? I’m thinking of a DSP I’m programming right now…
That said, 95% of my showstoppers come from globals. They are evil.
[DSP == Digital Signal Processor — your MP3 player has one that helps it translate digital bits into sound] And he is totally dealing with the problems I’m talking about above.
Kevin – if you are using C then you are kind-a stuck. If you are using C++ and your call backs can figure out what object they are attached to…
Ok. I hope that was sort-of clear!