Jump to content
  • Advertisement
Sign in to follow this  
NUCLEAR RABBIT

Is This A Bad Habit?

This topic is 4443 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
Advertisement
Yes. You do know you can pass arguments to functions right? Those parentheses aren't just there to look pretty.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.


Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!