Page 2 of 3
Re: Is this a good programming practice
Posted: Tue Dec 02, 2008 10:16 pm
by MarauderIIC
avansc wrote:actually its alot easier to debug procedural code. there are no fancty things like JUnit or whatever they have these days.
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.
Re: Is this a good programming practice
Posted: Wed Dec 03, 2008 1:38 am
by speewave
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
Re: Is this a good programming practice
Posted: Wed Dec 03, 2008 4:07 am
by Falco Girgis
You don't know about classes in C, because they don't exist.
Re: Is this a good programming practice
Posted: Wed Dec 03, 2008 8:52 am
by M_D_K
GyroVorbis wrote:You don't know about classes in C, because they don't exist.
It still makes me laugh that people think classes exist in C.
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");
And at exit the texture manager makes sure all texture resources are freed. Singletons rule
Re: Is this a good programming practice
Posted: Wed Dec 03, 2008 7:08 pm
by MarauderIIC
yay global variables? =)
Re: Is this a good programming practice
Posted: Wed Dec 03, 2008 7:19 pm
by avansc
MarauderIIC wrote:yay global variables? =)
if im developing something seriously i never have global variables.
Re: Is this a good programming practice
Posted: Thu Dec 04, 2008 10:08 am
by MarauderIIC
I was just pointing out that singletons are pretty much global variables, just you can't make another variable of the same type.
Re: Is this a good programming practice
Posted: Thu Dec 04, 2008 10:13 am
by avansc
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.
umm.. maybe we have different definitions of that a singleton is.
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.
Wikipedia 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.
http://en.wikipedia.org/wiki/Singleton_pattern
Re: Is this a good programming practice
Posted: Thu Dec 04, 2008 11:09 am
by MarauderIIC
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...?
Re: Is this a good programming practice
Posted: Thu Dec 04, 2008 11:13 am
by Falco Girgis
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.
Re: Is this a good programming practice
Posted: Thu Dec 04, 2008 11:37 am
by avansc
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
Code: Select all
int a = 5;
int main(void)
{
bla...
return 1;
}
that is a global variable.
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));
}
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.
Code: Select all
int main(void)
{
Game game = new Game("config.cfg");
return game.run();
}
Re: Is this a good programming practice
Posted: Thu Dec 04, 2008 10:39 pm
by MarauderIIC
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 :)
Re: Is this a good programming practice
Posted: Thu Dec 04, 2008 11:03 pm
by avansc
no, singletons cant be changed. they are not stored in memory the same as global variables.
Re: Is this a good programming practice
Posted: Thu Dec 04, 2008 11:07 pm
by Arce
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:
[]...therefore has an unlimited potential for creating mutual dependencies, and adding mutual dependencies increases complexity."
and I think that's simply a matter of a good design, not so much a problem with singletons themselves...
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...
Re: Is this a good programming practice
Posted: Thu Dec 04, 2008 11:21 pm
by Falco Girgis
Most intelligent thing that you have ever said on these forums.
Now go work on your goddamned level editor!