whenever I have methods that run every frame, for example
public int everyFrameMethod()
{
int a = 0;
return a;
}
instead of doing this I have been doing
public int a;
public int everyFrameMethod()
{
a = 0;
return a;
}
based on the notion that this will lead to less garbage collection since im never initiating a new int
now bear in mind that the example is super simplified, of course the most effective would be to just do return 0; ,
this is just a simplified example to ask about whether its a good coding practice or bad to avoid initiating variables within methods and instead use permanent fields within the class that never get collected
I agree with the above and will also like to add that micro optimizations like this are not needed during writing code, are needed only if eventually this actually causes a performance problem.
Having your variables local, is preferable because it reduces the number of states an object can have. Lesser states for an object or even better going functional and having immutable objects with only one possible state, greatly increases productivity because it minimizes the possible bugs and the time you need for testing and debugging.
Even if this was a reference type and was allocating, I would argue that during development it should still be local and if your benchmarks at production show you that it causes a problem because it is in a hot path or is the bottleneck and also this performance loss is greater than the performance of other operations, only then you change it to global to have it cached.
Food for thought: Good/Bad programming habits do not equate to “less garbage collection” or even speed of execution. Good programming habits should useful across, projects, computer languages and even time.
The optimization you are hoping for (as was explained above) doesn’t actually occur. Still doesn’t mean your example is necessarily a bad idea if other methods need to reference property “a”.
Local variables are a benefit… I’ve coded in a language that only support public variables. Talk about a nightmare.
It’s certainly true if you are doing pure functional programming, but not if you’re doing object-oriented programming (because creating simple abstractions and encapsulating implementation details is more important than strict immutability) or data-oriented design (because optimizing for data transformations is more important than strict immutability).
But I do agree that it’s a good idea in general to:
Aim to keep the scope of variables as small as possible (local > private > public).
Avoid methods having any unexpected side effects.
Be wary of member variables, as they can quickly introduce a lot of cyclomatic complexity to your methods. Keep them to a manageable level, and encapsulate the complexity they bring behind simple abstractions.
Oh good a conversation Your example can actually serve as an opposite case. No question that if the rotation needed can be obtained via a method it serves it’s purpose.
On the other hand if it isn’t a matter of setting a rotation but doing “work” that can set a rotation as part of it’s job then calling a ReinitializeTarget() method (to make up a name) can reasonably modify the transform of the object as part of the reinitialization.
I suspect the ideal solution relies heavily upon the context.