Is this a good programming practice

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: Is this a good programming practice

Post 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.
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
speewave
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 8
Joined: Thu Oct 30, 2008 9:08 pm

Re: Is this a good programming practice

Post 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
User avatar
Falco Girgis
Elysian Shadows Team
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

Post by Falco Girgis »

You don't know about classes in C, because they don't exist.
User avatar
M_D_K
Chaos Rift Demigod
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

Post by M_D_K »

GyroVorbis wrote:You don't know about classes in C, because they don't exist.
:lol: 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 :)
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.
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: Is this a good programming practice

Post by MarauderIIC »

yay global variables? =)
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: Is this a good programming practice

Post by avansc »

MarauderIIC wrote:yay global variables? =)
if im developing something seriously i never have global variables.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: Is this a good programming practice

Post 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.
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: Is this a good programming practice

Post 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
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: Is this a good programming practice

Post 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...?
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
User avatar
Falco Girgis
Elysian Shadows Team
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

Post 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.
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: Is this a good programming practice

Post 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();
}
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: Is this a good programming practice

Post 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 :)
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: Is this a good programming practice

Post by avansc »

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"
User avatar
Arce
Jealous Self-Righteous Prick
Jealous Self-Righteous Prick
Posts: 2153
Joined: Mon Jul 10, 2006 9:29 pm

Re: Is this a good programming practice

Post 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...
<qpHalcy0n> decided to paint the office, now i'm high and my hands hurt
User avatar
Falco Girgis
Elysian Shadows Team
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

Post by Falco Girgis »

Most intelligent thing that you have ever said on these forums.

Now go work on your goddamned level editor!
Post Reply