Efficiency of Inheritance

Started by
6 comments, last by AfroFire 21 years, 7 months ago
Forgive any kind of spelling mishaps, I am still a little 'blehed' out from OzzFest. Anyway, I was thinking, lets say I have 2 Sprite Classes. A Normal Sprite class with all the basic sprite goodies, and then an Animated Sprite class which inherits the normal sprite classes goodies, and allows animation on the sprites. Is it more efficient to have 2 seperate classes like this? Or one Jumbo Sprite class with all the functions packed in to one? -AfroFire [edited by - AfroFire on September 1, 2002 7:15:10 PM]
AfroFire | Brin"The only thing that interferes with my learning is my education."-Albert Einstein
Advertisement
It all depend on your implementation of the functions/code. If you code it well, then there''s no reason why a jumbo, all-in-one class can''t do the job of two classes at the same speed.
------------------------------------------[New Delta Games] | [Sliders]
Ask yourself this question:
Is anybody other than you ever going to use your code to
write games.
The answer is most likely "nope".
Spending time trying to create easy to use classes is wasted time
unless you need other to understand them.
Figure out which ever way will be easiest for you...

I think, therfore I am.
I think?

I think, therefore I am. I think? - "George Carlin"
My Website: Indie Game Programming

My Twitter: https://twitter.com/indieprogram

My Book: http://amzn.com/1305076532

Thanks guys.


-AfroFire
AfroFire | Brin"The only thing that interferes with my learning is my education."-Albert Einstein
quote:Original post by Glass_Knife
Spending time trying to create easy to use classes is wasted time
unless you need other to understand them.

So you say you should only write well-designed code if other people are going to see it or use it?

"When you know the LORD you have no need for masturbation!"
--AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.[Project site] [IRC channel] [Blog]
quote:Original post by Glass_Knife
Spending time trying to create easy to use classes is wasted time
unless you need other to understand them.


You''ll talk differently in 3 months time when you realise that old code has a subtle bug in it


It's only funny 'till someone gets hurt.And then it's just hilarious.Unless it's you.
I would disagree here.

Just because no one else will use your code doesn''t mean that you shouldn''t attempt to code it properly. When you have problems writing easy to use classes (especially in a simple / well defined case like this) it ususally means you don''t have a complete understanding of the situation.

Usually modularization (having two classes) will be more efficient than a non-modularized approach (1 huge class) simply because its easier to see the optimzations. As well, if there are a number of differences between the two classes the inheritance appraoch will most likely be more effcient because you''ll probably have a bunch of ''if'' statments running around in the code depending on whether or not your drawing an animated sprite, or a normal sprite. On the other hand if the animated sprite is only slightly different (maybe only a slight extension to the loading function or whatever), than the implicit pointer derefence will add a slight overhead and it would probably be better to go with one large class (though trust me when I say that this is an extreme case, cause even 1 extra ''if'' statement will take longer time than the pointer derefence).

Now one thing to consider is that in either case the actual drawing code will probably be of a significant number of magnitudes slower than either case, so code it in whatever way is easier for you to understand (I would choose the two - class approach as it allows you alot more flexibility in the future).
Anonymous Poster, I will have to go with your idea, I decided that even IF no one is going to see my code, I should still practice writing descent code >

I may someday send this in for a job, one never knows.

-AfroFire
AfroFire | Brin"The only thing that interferes with my learning is my education."-Albert Einstein

This topic is closed to new replies.

Advertisement