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

Started by
86 comments, last by swiftcoder 13 years, 3 months ago
so drop it for your logger, and your kernel. there's a high chance that one day, you need more than one kernel.

and your logger just doesn't need it.
If that's not the help you're after then you're going to have to explain the problem better than what you have. - joanusdmentia

My Page davepermen.net | My Music on Bandcamp and on Soundcloud

Advertisement
Even having your Kernel as a singleton may give you a bit of hassle down the track. I export my engine class in a DLL, and that's kind of difficult (or probably impossible) to do with a singleton.
[size="1"]
Well okay, okay if you insist. Still, that sounds 'weird'. I need all my subsystem to log their activity, yet if I dont singletonize them, they'll have no way to log up. I can go around with storing global pointer though, which will almost be the same though. The kernel works out ok without it tho.
//log.cppvoid log(string log) {   std::fopen file("log.txt", std::ios::writeappendwhateverflag);   file<<log<<std::endl;   file.close();}



why exactly do you need a global log thingens? is your log that complicated?
If that's not the help you're after then you're going to have to explain the problem better than what you have. - joanusdmentia

My Page davepermen.net | My Music on Bandcamp and on Soundcloud

Hmm sounds good. The thing Im making a logger class is only to make it 'elegant'. It creates the log file in constructor and close it in destructor. Okay..I will recode it. Wish it get better. Anyway, if singleton's bad, why it exists?
if wars/violence/death/insertwhateveryouwant are bad, why it exists?

in short, singleton is a tool, and can be used where appropriate. problem is, it's not a good tool, and nearly never appropriate.


there can be more elegant loggers, but only if needed. and then, passing them around is best (so every subsystem like graphics, sound, network can use its own instance, and f.e. log to other files, have different logging behaviour, etc)
If that's not the help you're after then you're going to have to explain the problem better than what you have. - joanusdmentia

My Page davepermen.net | My Music on Bandcamp and on Soundcloud

Hmm logging to different files? Sounds a little messy. I'll think about it later....Thank you for the great responses. My colleagues always say singleton's every coders enemy, But I never listened. Please accept my repentance O Lord of The Code :p
I think if a thread gets this long about whether to make a logger global or not then it's being severely over thought and the easiest way to solve the problem should be adopted.

Make it a global pointer to your logger class and be done with it.
if there is a problem and one can learn more about it to grow and get better in the future, then drop all hope and just run back to the problem and use it as is, always the best.

sorry, dave, but you're completely wrong. even with that cool name you have.

people who do think globals are simple and a good quick solution think wrong, they have to rewrite that part of their brain. this is like "my gf annoys me, i kill her, then she's quiet". wrong thinking. (now don't tell that analogy to my gf.. i guess she'll kill me otherwise..).

we're here to learn, not to duck and cover if there's a problem. your solution doesn't help him, it isn't even more easy than the simple solution i provided, and it isn't more flexible.
If that's not the help you're after then you're going to have to explain the problem better than what you have. - joanusdmentia

My Page davepermen.net | My Music on Bandcamp and on Soundcloud

Sure he is here to learn. You have taught loads and I am offering an alternative perspective. Having been through this entire learning curve myself I am offering the opinion that in fact avoiding some of C++ idioms is a good thing. Worrying about singletons etc is not something to get caught up in when developing all software.

I would like to see the OP decide for himself whether he needs to worry about such things as singletons. You can carry on writing OO if you like, hell he can as well, i really don't care. What i don't like to see however is the obfuscation of far simpler approaches (such as global data) which in fact arn't as bad as alot of people seem to think they are.

Thanks,

This topic is closed to new replies.

Advertisement