Is This A Bad Habit?

Started by
53 comments, last by superpig 17 years, 11 months ago
Hello, I was wondering if using global variables (C++) is a bad habit to get into? Eample:

//Global Variable
int o;

void diaper()
{
    std::cout << "My Fav Number is 731";
    std::cout << "I LOve Peanut Butter!";
}

void poop()
{
   std::cout << "Whats Your fav Number? ";
     std::cin >> o;
}


If it is a bad habit, or there is a better way of keeping variables so i can use them Anywhere in my functions/program, How can i do this? -- Thanks.
Advertisement
Yes. You do know you can pass arguments to functions right? Those parentheses aren't just there to look pretty.
Quote:Original post by NUCLEAR RABBIT
Hello, I was wondering if using global variables (C++) is a bad habit to get into?

it's a very bad habit. globals are bad. encapsulation is good.

This space for rent.
Well... Let me be the contrarian voice and point out that there is such a thing as a pointer that really does need to be seen by the whole program, and even have its state changed occasionally. It also depends a bit on what you are doing. If you are working on, say, a snippet to analyse some data and make a few histograms, then globals are fine, because the effort of making properly encapsulated code is greater than the effort of maintaining a small non-encapsulated program. Particularly one that will only be needed for a few weeks anyway. Let me re-emphasise that 'small'; more than a thousand lines of code, and you definitely want encapsulation.

So, with the small caveats above, yes, globals are a bad habit for games programming. Don't use them for anything bigger than Tetris, at least not as a general thing.
To win one hundred victories in one hundred battles is not the acme of skill. To subdue the enemy without fighting is the acme of skill.
It's not a good habit, but it's not as bad as some people make it. Using singletons to pretend that you're not using globals is a significantly worse disease.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Quote:Original post by King of Men
So, with the small caveats above, yes, globals are a bad habit for games programming. Don't use them for anything bigger than Tetris Pong, at least not as a general thing.


Basicly, try to keep your variables as local as possible. That way, you wouldnt have to worry about different variables with the same name. For example, if you were making an FPS, you should have your player speed variable, and your bullet speed variable. Now, big projects can be confusing, and you might have the variable name "speed" for both. That could be a disaster! Either you would go super fast, or your bullet would go super slow. But by keeping your variables local, you can use Player.Speed, and Bullet.Speed. Alot better, isn't it?

Hope I helped,
Martin
When using global variables, it can be hard for yourself or another programmer to look at your code and find out where some given global variable is being significantly changed at some given time, simply because it's available to all of them. The difficulty lies in tracking just where the variable is declared, initialized, and all the places in which it's used. (Sound easy? Have a look at the Quake source code.) Throw in some multithreading, and you can have a lovely spaghetti dinner.

You just wish the guy who wrote it (perhaps you?) had simply kept the code clean and organized from the start in the first place, and had not been so lazy as to shove all these variables into the global namespace.

Keep it simple, but be smart about it.
It's one of those dangerous but efficient things, like statically allocated arrays that you hope don't overflow.

Whenever I look at C source code to a program I get the uncontrollable urge to try and crash the program with buffer overflows.
Quote:Original post by Boder
It's one of those dangerous but efficient things, like statically allocated arrays that you hope don't overflow.

Globals actually aren't necessarily more efficient than better-written equivalent code. In particular, having lots of globals can significantly increase the number of page faults.

This topic is closed to new replies.

Advertisement