Right; OO stuff is harder to test in general and there aren't any easy techniques quite yet, and the mutation operators are still rather new.avansc wrote:actually its alot easier to debug procedural code. there are no fancty things like JUnit or whatever they have these days.
Is this a good programming practice
Moderator: Coders of Rage
- MarauderIIC
- Respected Programmer
- Posts: 3406
- Joined: Sat Jul 10, 2004 3:05 pm
- Location: Maryland, USA
Re: Is this a good programming practice
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
Re: Is this a good programming practice
Now i don't know alot about C++ (nothing about classes in C...) Look at Lazy Foo Productions... Lazyfoo.net , he does great tutorials on SDL, Something about classes should appear in there
- Falco Girgis
- Elysian Shadows Team
- Posts: 10294
- Joined: Thu May 20, 2004 2:04 pm
- Current Project: Elysian Shadows
- Favorite Gaming Platforms: Dreamcast, SNES, NES
- Programming Language of Choice: C/++
- Location: Studio Vorbis, AL
- Contact:
Re: Is this a good programming practice
You don't know about classes in C, because they don't exist.
- M_D_K
- Chaos Rift Demigod
- Posts: 1087
- Joined: Tue Oct 28, 2008 10:33 am
- Favorite Gaming Platforms: PC
- Programming Language of Choice: C/++
- Location: UK
Re: Is this a good programming practice
It still makes me laugh that people think classes exist in C.GyroVorbis wrote:You don't know about classes in C, because they don't exist.
I just have to say I love singletons. I use them for my log file output and various managers (textures, sound, etc). They're just really handy for that stuff.
Its alot easier to just do this:
Code: Select all
Texture2D *tex = TextureManager::GetSingleton().Load("some_texture.png");
Gyro Sheen wrote:you pour their inventory onto my life
IRC wrote: <sparda> The routine had a stack overflow, sorry.
<sparda> Apparently the stack was full of shit.
- MarauderIIC
- Respected Programmer
- Posts: 3406
- Joined: Sat Jul 10, 2004 3:05 pm
- Location: Maryland, USA
Re: Is this a good programming practice
yay global variables? =)
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
Re: Is this a good programming practice
if im developing something seriously i never have global variables.MarauderIIC wrote:yay global variables? =)
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
Dad, "Yea well I have a fan belt in street fighting"
- MarauderIIC
- Respected Programmer
- Posts: 3406
- Joined: Sat Jul 10, 2004 3:05 pm
- Location: Maryland, USA
Re: Is this a good programming practice
I was just pointing out that singletons are pretty much global variables, just you can't make another variable of the same type.
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
Re: Is this a good programming practice
umm.. maybe we have different definitions of that a singleton is.MarauderIIC wrote:I was just pointing out that singletons are pretty much global variables, just you can't make another variable of the same type.
singletons dont really have anything to do with global variables, its more of a design pattern.
i mean i guess you can declare the object that is a class desinged to be a singleton outside of main to make it global, but there really is no reason to do that.
if you look at my phyzillz code most of my objects are singletons but non are global variables.
http://en.wikipedia.org/wiki/Singleton_patternWikipedia wrote: In software engineering, the singleton pattern is a design pattern that is used to restrict instantiation of a class to one object. (This concept is also sometimes generalized to restrict the instance to a specific number of objects - for example, we can restrict the number of instances to five objects.) This is useful when exactly one object is needed to coordinate actions across the system. Sometimes it is generalized to systems that operate more efficiently when only one or a few objects exist. It is also considered an anti-pattern by some people, who feel that it is overused, introducing unnecessary limitations in situations where a sole instance of a class is not actually required.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
Dad, "Yea well I have a fan belt in street fighting"
- MarauderIIC
- Respected Programmer
- Posts: 3406
- Joined: Sat Jul 10, 2004 3:05 pm
- Location: Maryland, USA
Re: Is this a good programming practice
Right, but you can still access a singleton from anywhere via getinstance (don't have to pass it, hence my analogy to global), unless I'm missing something...?
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
- Falco Girgis
- Elysian Shadows Team
- Posts: 10294
- Joined: Thu May 20, 2004 2:04 pm
- Current Project: Elysian Shadows
- Favorite Gaming Platforms: Dreamcast, SNES, NES
- Programming Language of Choice: C/++
- Location: Studio Vorbis, AL
- Contact:
Re: Is this a good programming practice
That's right.
He is arguing that it is not a global, because the instance is contained within the class and must be retrieved through a getInstance() method.
You are arguing that it is a global, because you can retrieve the instance from anywhere in the program rather than having to pass it around.
He is arguing that it is not a global, because the instance is contained within the class and must be retrieved through a getInstance() method.
You are arguing that it is a global, because you can retrieve the instance from anywhere in the program rather than having to pass it around.
Re: Is this a good programming practice
well its globally accessible, if you choose to make it so, but thats not what makes it a singleton,
you can make any object available globally if you have a function that returns it location in memory.
and globally is a dangerous term. if you are talking about something like this
that is a global variable.
if you havesomething like this.
these mayinfact be accesible anywhere in the program as long as you provide that functionality, but are not global variables.
ps: i understand what you are trying to say and i guess in lose term you are right, im just a stickler for things like this.
singleton in its essence just means that there is only once instance of that object in a program.
think of it this way. in game programming ideally you want your input, graphics, contentloading, physics, etc. all to be singleton designs.
then i would also make a gameclass that encompasses them all. which would also be a singleton class. then when your main for your game would only be something like this.
you can make any object available globally if you have a function that returns it location in memory.
and globally is a dangerous term. if you are talking about something like this
Code: Select all
int a = 5;
int main(void)
{
bla...
return 1;
}
if you havesomething like this.
Code: Select all
int a = 5;
int main(void)
{
SingletonClass temp = new SingletonClass();
// or how i like doing things.
SingletonClass *pTemp = (SingletonClass*)malloc(sizeof(SingletonClass));
}
ps: i understand what you are trying to say and i guess in lose term you are right, im just a stickler for things like this.
singleton in its essence just means that there is only once instance of that object in a program.
think of it this way. in game programming ideally you want your input, graphics, contentloading, physics, etc. all to be singleton designs.
then i would also make a gameclass that encompasses them all. which would also be a singleton class. then when your main for your game would only be something like this.
Code: Select all
int main(void)
{
Game game = new Game("config.cfg");
return game.run();
}
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
Dad, "Yea well I have a fan belt in street fighting"
- MarauderIIC
- Respected Programmer
- Posts: 3406
- Joined: Sat Jul 10, 2004 3:05 pm
- Location: Maryland, USA
Re: Is this a good programming practice
I'm familiar with how singletons work and what the purpose is; it's just that, well, globals are "bad" because, quoting wikipedia here, "They are usually considered bad practice precisely because of their nonlocality: a global variable can potentially be modified from anywhere, (unless they reside in protected memory) and any part of the program may depend on it. A global variable therefore has an unlimited potential for creating mutual dependencies, and adding mutual dependencies increases complexity."
So isn't a singleton 'bad' for the same reason?
I will admit that globals have their uses, and I have used them, as well. Quoting wikipedia again, "However, in a few cases, global variables can be suitable for use. For example, they can be used to avoid having to pass frequently-used variables continuously throughout several functions."
Personally, due to the similarities, I would have to say that a singleton is more of a special case of a global variable than a "not a global variable." Of course, I'm arguing opinion here, but you see my point a bit more in-depth now, I hope, and if you got it that in-depth before, well, then 10 years from now somebody can pick it up and go oh that's what he meant :)
So isn't a singleton 'bad' for the same reason?
I will admit that globals have their uses, and I have used them, as well. Quoting wikipedia again, "However, in a few cases, global variables can be suitable for use. For example, they can be used to avoid having to pass frequently-used variables continuously throughout several functions."
Personally, due to the similarities, I would have to say that a singleton is more of a special case of a global variable than a "not a global variable." Of course, I'm arguing opinion here, but you see my point a bit more in-depth now, I hope, and if you got it that in-depth before, well, then 10 years from now somebody can pick it up and go oh that's what he meant :)
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
Re: Is this a good programming practice
no, singletons cant be changed. they are not stored in memory the same as global variables.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
Dad, "Yea well I have a fan belt in street fighting"
Re: Is this a good programming practice
Singletons don't violate OO design...They actually enhance it.
If there were no singletons, the equivalent would be a class with all static members. However, singletons do exist purely for the sake of good OO design...They allow private members and data hiding...
And the argument that singletons are 'global' is bleak at best. That's the same as claiming that all static members are global, thus evil...But before we can really finish debating weather or not singletons are bad because they're 'globally accessible', lets take a look at WHY globals are bad: they violate encapsulated OO design, and they can lead to messy code as a result of reliance of other parts of the program on said variable...
Singletons sure as heck don't violate encapsulated design--they allow for hidden and private data types just as any other object would. Though they can be accessed anywhere, true, that doesn't negate data hiding and proofing capabilities that should prevent one from screwing something serious up.
The only real argument I see as to why they can be 'bad' is this:
edit: ...In order to access a singleton, one must use the scope operator. Would that in and of itself negate arguments that it's equivalent to globals...? Eh, maybe I'm pushing my luck here, haha...
If there were no singletons, the equivalent would be a class with all static members. However, singletons do exist purely for the sake of good OO design...They allow private members and data hiding...
And the argument that singletons are 'global' is bleak at best. That's the same as claiming that all static members are global, thus evil...But before we can really finish debating weather or not singletons are bad because they're 'globally accessible', lets take a look at WHY globals are bad: they violate encapsulated OO design, and they can lead to messy code as a result of reliance of other parts of the program on said variable...
Singletons sure as heck don't violate encapsulated design--they allow for hidden and private data types just as any other object would. Though they can be accessed anywhere, true, that doesn't negate data hiding and proofing capabilities that should prevent one from screwing something serious up.
The only real argument I see as to why they can be 'bad' is this:
and I think that's simply a matter of a good design, not so much a problem with singletons themselves...[]...therefore has an unlimited potential for creating mutual dependencies, and adding mutual dependencies increases complexity."
edit: ...In order to access a singleton, one must use the scope operator. Would that in and of itself negate arguments that it's equivalent to globals...? Eh, maybe I'm pushing my luck here, haha...
<qpHalcy0n> decided to paint the office, now i'm high and my hands hurt
- Falco Girgis
- Elysian Shadows Team
- Posts: 10294
- Joined: Thu May 20, 2004 2:04 pm
- Current Project: Elysian Shadows
- Favorite Gaming Platforms: Dreamcast, SNES, NES
- Programming Language of Choice: C/++
- Location: Studio Vorbis, AL
- Contact:
Re: Is this a good programming practice
Most intelligent thing that you have ever said on these forums.
Now go work on your goddamned level editor!
Now go work on your goddamned level editor!