Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top


Using direct access while breaking the law of demeter looks like this:

res.sprite.drawQuad(res.sprite.xScreen,res.sprite.yScreen, mMVPMatrix);

Using proxy methods / getters / setter looks like this:

res.drawSprite(res.sprite, res.getScreenX(res.sprite), res.getScreenY(res.sprite), mMVPMatrix);

I'm finding it really difficult to justify using the (apparently better) 2nd method. It means more to type, more confusing looking code and potentially slower excecution.

I've been told the 2nd way of doing things is the way to go but could someone please explain why?

Original Question

I've read a few questions on this subject but my question relates specifically to game development (and even more specifically, use of these methods within a game-loop).

I have the following classes (See below for pseudo code):

Logic - this is where all my game updates happen

Resources - this is where I load all my bitmaps and create my sprite objects

MyGLRenderer - this holds my game loop and rendering code

So, in my Logic class you can see that I'm accessing my sprite by 'reaching' through my Resource class to get to my Quad class ('sprite' is an instance of Quad).

I know that generally speaking, this isn't good practice. And I can easily overcome it by adding a 'passthrough' or 'proxy' method in my Resources class like so:

public incremementPos (Quad quad, int amount){



And then just setting it like so:

res.incrementPos(sprite, r.SpriteXOffset);

But I've been reading that methods like these can be aprox. 1/3 slower than just accessing variables 'directly'. This apparently isn't much of a problem until you start using stuff like this within a performance-critical piece of code.......i.e.,the game-loop.

I would be making literally hundreds of these method calls within my loop. I've spent the last 2 hours writing about 20 of these passthrough methods but I'm still not convinced.

Would you do the same? Or would you just access/update the variables directly?

Bearing in mind that in my case, I'm not just accessing variables from neighbouring classes, but non-neighbouring ones too (Thereby breaking the 'law of Demeter'). Is there a point where good practice has to be somewhat relaxed in game dev?

Pseudo Code

public class MyGLRenderer{

    Logic logic;
    various game object variables declared here;

    public MyGLRenderer{

    logic = new Logic(this, res);    //Create new logic object so I can access the update method to use in my gameloop


    public void onDrawFrame{

    logic.update();    //Call logic update
    //Rendering code goes here


public class Resources{

    Quad sprite;
    Bitmap myBitmap;

    public Resource{

    myBitmap = BitmapFactory.decodeResource(view.getResources(), R.drawable.texture);

    sprite = new Quad();



public class Logic{

    MyGLRenderer r;
    Resources res;

    public Logic{MyGLRenderer r, Resources res}

    this.r = r;
    this.res = res;


    public void update(){

    //Problem code

    res.sprite.xPosition+=r.SpriteXOffset  //Just for example - could be anything, a direct access variable like this, a getter, setter or other method.

   //in the above example, xPosition variable is part of the Quad class which Sprite
   // is an instance of and the SpriteXOffset variable belongs to the MyGLRenderer class
   //We could just as easily do the following if both variables were members of the Quad class


share|improve this question

closed as too broad by Josh Petrie Jan 2 '14 at 17:42

There are either too many possible answers, or good answers would be too long for this format. Please add details to narrow the answer set or to isolate an issue that can be answered in a few paragraphs.If this question can be reworded to fit the rules in the help center, please edit the question.

Do some profiling and reach your own conclusions. Personally I think using getters won't matter unless you have a good lot on main loop and are targetting low end mobiles. – petervaz May 21 '13 at 13:13
Hi @petervaz I'm actually just in the process of doing the profiling, problem is I only have 2 physical devices on which to test. I'm using these methods hundreds of times in my loop, so that's thousands of times per second. I am targeting Froyo upwards, so probably quite a few old devices! I am only using these where I would otherwise break the 'law of demeter', otherwise, I am still accessing neighbouring classes variables 'directly' - thanks for reading my question and for commenting!! – BungleBonce May 21 '13 at 13:27
Why do you need to pass the screen position to res.drawSprite when you're getting the values from the passed in sprite anyways? You could put the getting of the screen position in res.drawSprite and your call would look like res.drawSprite(res.furman, mMVPMatrix). Especially as the getting of screen position is already a function of res. – XGundam05 May 21 '13 at 17:23
Also, you should only optimize when you have performance issues. Optimizing for potential issues can actually do more harm than help. – XGundam05 May 21 '13 at 17:25
Hi @XGundam05, I didn't really have performance optimisation in mind when I changed over to the 2nd method. I knew, if anything performance might take a hit. But people kept telling me that's the way I should do it, otherwise I'm reaching through classes which is a big design no-no. Thanks re the x and y thing, you are right of course. However there is a reason for this. For some sprites, I don't use the sprite's 'build in' x and y (for various reasons). I guess I could make more methods depending on how I draw the sprite. Thanks again. – BungleBonce May 21 '13 at 18:17