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

MakazeGamer
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 26
Joined: Wed Oct 29, 2008 10:48 pm

Is this a good programming practice

Post by MakazeGamer »

Ok so I keep running into LNK errors in VS. usually either 2001 or 2005. And usually with classes and header files.

But i wondered if it was a bad programming practice, for when you delcare classes that you declare them like this

Code: Select all

class ClassName
{
private:
public:
}ClassObject;
User avatar
ansatsusha_gouki
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 37
Joined: Fri Jul 04, 2008 1:19 am
Location: Cleveland
Contact:

Re: Is this a good programming practice

Post by ansatsusha_gouki »

you should get rid of ClassObject at the end



class Name
{

watever

};
MakazeGamer
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 26
Joined: Wed Oct 29, 2008 10:48 pm

Re: Is this a good programming practice

Post by MakazeGamer »

yea i thought so... But what is so bad about doing that though ? Like what problems could i have in the long run ?
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 »

I'm not aware of any "problems" from doing this. It's just like creating a global ClassObject of ClassName. Some people frown upon globals, but I have never really been much of a style whore. I'll leave it to somebody else to tell you whether its in good form or not.
User avatar
trufun202
Game Developer
Game Developer
Posts: 1105
Joined: Sun Sep 21, 2008 12:27 am
Location: Dallas, TX
Contact:

Re: Is this a good programming practice

Post by trufun202 »

In theory, classes should be in separate files, so you wouldn't need an instance of the object at that point...

With that said, we all use hacky globals from time to time. ;)
-Chris

YouTube | Twitter | Rad Raygun

“REAL ARTISTS SHIP” - Steve Jobs
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 »

...or glorified globals like singletons and pretend that we aren't breaking the theory of OO design.

;)
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 »

MakazeGamer wrote:Ok so I keep running into LNK errors in VS. usually either 2001 or 2005. And usually with classes and header files.

But i wondered if it was a bad programming practice, for when you delcare classes that you declare them like this

Code: Select all

class ClassName
{
private:
public:
}ClassObject;
its actually not bad,
but you are probably using it wrong.

classobject would usually be a alias for a pointer to that class/struct/union, thus in your program you wouldend have to write ClassName *Name, but only pClassName pName;
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
User avatar
trufun202
Game Developer
Game Developer
Posts: 1105
Joined: Sun Sep 21, 2008 12:27 am
Location: Dallas, TX
Contact:

Re: Is this a good programming practice

Post by trufun202 »

GyroVorbis wrote:...or glorified globals like singletons and pretend that we aren't breaking the theory of OO design.

;)
I'm curious as to why singletons are getting such a bad name these days. I've found them to be ideal for several projects. Having an object that can only instantiate itself ensures that you don't have multiple instancing floating around the server, chewing up more memory, and potentially stepping on one other.

At work, a buddy and I built a web-based chat / bidding application - kinda like a realtime eBay. They originally outsourced it to India and it ran like shit. It crashed the web server when more than 8 users got in a room - cuz it was chewing up so many resources. We rewrote the backend using singletons. Basically each room was only instantiated once on the server, regardless of the number of people in the room.

There was also a project where we needed a single instance of a runtime running on the webserver. Having more than one would screw things up because they would step on each other. Wrapping the runtime in a singleton did the trick.

I agree that singletons don't fit every case, but I'm surprised that so many people are against them.
-Chris

YouTube | Twitter | Rad Raygun

“REAL ARTISTS SHIP” - Steve Jobs
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 »

trufun202 wrote:
GyroVorbis wrote:...or glorified globals like singletons and pretend that we aren't breaking the theory of OO design.

;)
I'm curious as to why singletons are getting such a bad name these days. I've found them to be ideal for several projects. Having an object that can only instantiate itself ensures that you don't have multiple instancing floating around the server, chewing up more memory, and potentially stepping on one other.

At work, a buddy and I built a web-based chat / bidding application - kinda like a realtime eBay. They originally outsourced it to India and it ran like shit. It crashed the web server when more than 8 users got in a room - cuz it was chewing up so many resources. We rewrote the backend using singletons. Basically each room was only instantiated once on the server, regardless of the number of people in the room.

There was also a project where we needed a single instance of a runtime running on the webserver. Having more than one would screw things up because they would step on each other. Wrapping the runtime in a singleton did the trick.

I agree that singletons don't fit every case, but I'm surprised that so many people are against them.
the singleton design pattern is my fav
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
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 »

Yeah, I agree. I honestly hate this whole OO whore movement that lots of indie developers seem to be a part of. It's like they are strictly adhering to this abstract idea of what is good and bad when coding based solely on what they read--it's clear that they don't have enough experience to decide what does and does not work for themselves.

These are also the same guys who make a million topics "IS THIS OO DESIGN OKAY?!?!" What good does it do if you question your overly complicated idea for implementation every step of the way? They are seriously working backwards.

I learned not too long ago how to be smart about game development. I am my own boss. I do as I please how I please. I make decisions based on what works for me and the project. I choose what will help further the project, not what the programming elitists say.

This is also why you won't really see me in C++ design topics commenting on OO implementations much. It's all too subjective for my liking. I'm all about the actual game development implementations of things. I will always be involved in those topics. But seriously, when it comes to bitchy OO design, count me out.
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 »

^ totally agree.

having a singleton design pattern does not neccasarily mean its not OO. anyways.

i cant remember who when or where, but it has been mathematically proven that any problem can be
solved by a procedural programming method.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
XianForce
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 767
Joined: Wed Oct 29, 2008 8:36 pm

Re: Is this a good programming practice

Post by XianForce »

also a small note, if you start with private, you don't have to declare it private. Its private by default(right people?)
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 »

From a testing point of view (I'm taking a testing class), tracing dependencies is harder, which means that testing is harder, since most testing has to happen on the interfaces (read: interactions) between objects, since that's where things go wrong most of the time. You can't look at the class and say "this class uses this class", you have to go through the code itself, or attempt to automate such a procedure (and if it's harder for you to do it's harder to automate, right?)

Same problem with globals. And that's why globals/singletons are bad... apparently. Not that I care... yet :)
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:From a testing point of view (I'm taking a testing class), tracing dependencies is harder, which means that testing is harder, since most testing has to happen on the interfaces (read: interactions) between objects, since that's where things go wrong most of the time. You can't look at the class and say "this class uses this class", you have to go through the code itself, or attempt to automate such a procedure (and if it's harder for you to do it's harder to automate, right?)

Same problem with globals. And that's why globals/singletons are bad... apparently. Not that I care... yet :)
actually its alot easier to debug procedural code. there are no fancty things like JUnit or whatever they have these days.
Image
testing suites are nice. but they do not do the job better in anyway. i believe that testing units and stadards were invented to make untechincal people understand how far along/ how fucked up a project on a technical level is.

showing a manager a pictures saying a the XML package has a bug with nice red colors, but thats the only thing that not working and we are fixing it.
is better than telling him, oh yeah, we have a major memory leak thats causing faulty data in the XML parser, but we dont know if its the parser because we havent tested to see if the function that support the XML class is giving it proper variables.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
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 »

PS: if you wanted a decent testing suit for C++ i would use Unit++
its free.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
Post Reply