Ways to avoid the needs and the musts of global variables??

Started by
86 comments, last by swiftcoder 13 years, 3 months ago
How do you do it? right now Im making a App class and put all program variables there. I know it might not be the best to do it, so I ask you here...
(anyway, might be good if one create a 'Useful C++ tips and tricks' thread here somewhere...just ideas though. Could help n00bs like me :p )
Advertisement
You can pass variables by reference for starters. That way you can change the values if need be. And redesigning some of the program could help eliminate the need for global variables. If lots of different pieces need the information, then you may (or may not depending) be doing something a bit "wrong".
If there are objects/class that a lot of code references, then your design could use improvement. More specifically your classes should be more loosely coupled.
Im sorry c_olin, but I dont understand the "loosely coupled" part. What do you mean by that?
@Dragonsoulj : what if for example, almost all part of my program need acces to a Logger object, is it wrong?Well I already made it a singleton, so it works out Okay.
The simple answer is it depends. No matter how much you may not accept that as an answer it is it. As you gain more experience you will become more familiar with the points at which you should employ certain techniques, and that's the bottom-line. The only broad generalization that can be made is that no broad generalizations can be made.
Denzel Morris (@drdizzy) :: Software Engineer :: SkyTech Enterprises, Inc.
"When men are most sure and arrogant they are commonly most mistaken, giving views to passion without that proper deliberation which alone can secure them from the grossest absurdities." - David Hume
Why not have a Logger object that is a part of another class? Just declare one either statically or as a private/public/protected variable and work with it that way? Or allow functions to accept Logger objects? Or reverse the thinking and have the other functions called from the Logger object?
Quote:Original post by Halifax2
The simple answer is it depends. No matter how much you may not accept that as an answer it is it. As you gain more experience you will become more familiar with the points at which you should employ certain techniques, and that's the bottom-line. The only broad generalization that can be made is that no broad generalizations can be made.


QFT.

Edit: And that probably applies for everything else in life.
[size="2"]I like the Walrus best.
@DragonSoulj : well I like the way singleton works and Im ok with it now
And thanks for the suggestion, I think wrapping an application in a class and putting all the needed variables there is the way to go. The guy from the Black and White told me that he did just the same. Now, Back to coding!
Great game. And glad my suggestion could at least help.
So far any response in gamedev helps, although sometimes sarcastic, nevertheless it's always constructive...

This topic is closed to new replies.

Advertisement