Question

The question itself is not directly related to XNA but the issue i have is. I am interested in the performance effect for referencing objects to methods/functions. For instance, in XNA i often see code referencing the complete Game1 object when there is only the need for a specific value or object like the GraphicsDevice, or just the viewport lying much deeper in the hierarchy. I always reference the specific value or object because i think this is best practise, often i have to go back and add another value if needed.

So what is best practise for referencing values and objects to methods/functions? Does it matter much or is it just somekind of pointer? So a pointer to simple int or a huge object would be the same? Things get more obvious when this gets stored in another class in a property, there needs to be another memory block reserved for the property, right?

Was it helpful?

Solution

'Referencing' an object for the sake of accessing only a small portion of it's proporties/methods is not good practice. It is much better to expose the required functionality, make it static, then do something like:

// Calling a static method
Game1.DoSomething();
// Accessing a static property
int test += Game1.MyInt;

I think the reason why you are seeing XNA code samples referencing the whole object is because a number of XNA programmers do not have object orientated development backgrounds compared to straight-up C# developers, and are thus unaware of best practices for using the language efficiently.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top