EmbodiedDarkness
EmbodiedDarkness

Reputation: 135

Does instance variable throttles performance? Should one class serves two purposes?

I have a class that have multiple instance variables. I want to achieve two purposes with the class. It's possible that I may only use some variables for one purpose and sometime use both.

Here's a more concrete example. I want to create a class that every time the user tap the screen, a dog sprite and cat sprite appear with an animation. If tapped again, they continue to perform different animation. However, sometime I only want the dog sprite to appear and update. And some other rare times, I want the cat sprite to appear after a couple of taps after the dog sprite appeared.

The question is: does instance variable allocate too much memory? I'm highly concerned with performance, because I'm planning to make a memory-intensive game. Since it's hard to predict when I actually use all the instance variable, should I divide them into two classes? Let's divide the possible scenarios to get a better idea.

  1. Only the Dog Sprite is used and the cat sprite never appears : The cat's instance variable is left untouched if left in one class.

  2. The dog sprite appear first, then the cat sprite appear later : Both sprite will eventually appear. It's possible to divide it into two classes, but some methods are duplicated since methods such as the touch advance logic and animation are similar. But if we leave it in once class, scenario 1 could occur, which could possibly be solve without a lot of duplicate code being reproduced.

Other things could occur, but the problems is already discussed above. These are the pro and con from my point of view:

  1. One Class Approach

    • Pro
    • Avoid some duplicate logic
    • No need to import multiple header that leads to some similar instance variable

    • Con

    • Possibly leave half of instance variables unused (including NSString, CCSprite, a lot of integers and floats, CCAnimation, CCLabelBMFont)
  2. Two Class Approach

    • Pro
    • Less instance variables
    • Possibly inherit from the class without inheriting some unnecessary variables in the future

    • Con

    • Some logic are reproduced

It's difficult to decide which option I should use. Any suggestions would be great! Thank you in advance!

if (didHelp) 
   for (int x = 0; x < 100; x++)
      NSLog(@"Thanks!");

Upvotes: 0

Views: 189

Answers (3)

EmbodiedDarkness
EmbodiedDarkness

Reputation: 135

This is a somewhat open-ended question, and there are indefinitely many ways to approach this question. Therefore, this is my approach and may or may not fit in every scenarios.

There are cases when an instance variable could be replace -- however this should not affect your decision if necessarily needed. Instance variable should be used when needed. Do not perform endless calculation just to substitute a single instance variable. Do try to limit your instance variables into variables when it is not needed outside a certain scope. Thanks to the informative users that posted on here, instance variable left unused impact performance at such a microscopic scale that you should not worry.

From my point of view, a class should only have one focus -- on function and and should pass on any other information to other class that need it. Information should remain encapsulated -- with one function to maintain reusability in other projects.

One should focus on the relationship of the function. IT-IS is the relationship that say one object should inherit another. In reality, it's like a Sienna-IS a car. A boat-IS a vehicle. Therefore, these objects should inherit any information from it's superclass. On contrast, IT-HAS say that these class contain something, usually of a quality or component, that cannot be inherited. A sienna-IS a car, but a tire-IS-NOT a sienna. Rather, a sienna-HAS a tire.

Another important relationship is delegation. The fancy definition say it perform a task on behalf of another, much like how delegates in the US represent the people of their states. Basically, it pass a certain information saying to the other class, who should in good practice, not affect the other former class. The class should not know exactly who it pass on to, but know enough to pass on certain information. This process of not knowing the exact identity of the delegate is called coupling.

In my case, of cats and dogs, delegation along with IT-IS is subjectively the best answer. Your opinion may differ. A base class should contain all the information that the Cat and Dog share. And any other information that is needed, such as the sprite's position, should be passed on as a delegate to the other class. And based on what I wrote, a class should not, in normal circumstances, programmed to do two function; for a class do one function and pass on all other dutiful needs to another.

Upvotes: 0

CodeSmile
CodeSmile

Reputation: 64477

I'm highly concerned with performance

You and thousands of other inexperienced developers. Seriously, there are two things you're most likely going to experience:

  1. your idea is way out of proportion and no amount of performance optimization will make it work -> change your idea
  2. performance won't matter the least bit and you simply wasted time

Performance is among the least important things a game developer needs to consider at the start of a project.

Why?

Case #2 is self evident.

Assessing case #1 with reasonable accuracy before you even get started requires experience. Even then it's difficult. Simply have a backup plan if feature X proves to be too technically challenging (or impossible). If you can't assess performance, and your idea won't work with any backup plan, again you have two options:

  1. implement a different idea
  2. create a quick prototype to find out the peak performance parameters: memory usage, CPU & GPU utilization, loading times, and whatever other fitness tests seem appropriate to find out if your idea is feasible within a few days, if not hours.

does instance variable allocate too much memory?

No, and allocated memory has very little to do with performance.

You can use class_getInstanceSize to see how much memory a class instance uses. Rarely ever will a class instance use more than 500 Bytes. However, this only counts memory allocated for instance variables - not the memory the instance variables may point to. In a cocos2d app it's fair to say that 95% of your memory usage will come from textures.

It's difficult to decide which option I should use

Always strive to:

  • write readable code
  • write maintainable code
  • write less code
  • write safer code
  • write code only once (avoid duplication)

Upvotes: 6

mike628
mike628

Reputation: 49331

EmbodiedD, You are certainly worried about too much here. The heap is going to get quite large in most applications. One simple class will be irrelevant. When you have 1000 instances of a data intensive class then you might have to start thinking about profiling. If you are worried about organization, that's another thing altogether. If you are loading classA with var1 and var2 or loading classA with var1 and class2 with var2, its more a matter of how you were taught to do abstraction.

Upvotes: 0

Related Questions