Passing a variable vs. Storing that variable

Just as a general question I was wondering which method would be the best practice. Passing the variable containing the game world in the update function during every update of an entity, or, upon creation of the entity, store it as a variable in the entity class. I’ve used the second method before and it worked just fine. If I recall correctly minecraft uses the former of the two. What is your opinion on which is better?

It doesn’t really make much of a difference, and never use the Minecraft source as a reference. As much as Notch is a genius, it doesn’t mean his code is a good example.

Personally, I store it as a variable, as it gives me nicer code to work with.

I do the same as Heroes. If I need to use an object frequently, I just store it. If I only use it occasionally, I pass it in.

Ok, thanks. I was only citing Notch’s code because it was one example that I could think of.

Always limit the scope of variables, thus passing in required parameters on the fly should be the default way.
Except, of course, the caller has no parameter access, values are needed across multiple methods or a reasonable component structure is built for a reason.
The more places you store objects, the more messy und unmaintainable the overall object structure gets. In case of cyclic references, memory leaks could be the effect as well.

Will go ahead and give my opinion of the benefit of passing rather than storing, I usually have a lot of fields but try my best to use methods and passing parameters.

From what I have read, due to modern hardware method calling is practically free in terms of performance, as is passing instances of variables.

Just remember if you are passing an object it gets passed as is, if you pass primitive data (int, float etc etc) it passes a copy not the actual variable. That is where the wonderfulness of OO comes in :D.

So passing an int to a method and changing it, won’t change the actualy field, only the passed variable.

Anyway, I use a lot of fields but try to use methods to alter fields and pass values/objects, it makes it more maintainable as well.

Does the state of the variable change if the object it refers to changes ro does it stay the same?

Yes, if you make a new object or point to a different object, the state of all variables inside that object change (unless it is a static variable).

With the 1 FPS it may save somehow on a horrible an xp or something, and just because it seems nicer and I can call the object anytime- with my opinion injected into the thought I also think its a good idea to give on creation.

With computers being so fast, it doesn’t matter at all… Completely doesn’t matter…

if your going to really pass it in all the time it might be better to just to use dependency injection. sadly java doesnt have it, but scala’s cake pattern provides beast dependency injection at compile time vs run time.

@Agro
First of all, DI in Java isn’t that bad(yes it exists) and quite usefull and when you do it right you won’t have any speed problems either. The cake pattern in Scala is a nice solution, but not as powerful as runtime DI, so even in Scala you would use libs like guice.

@topic
At first you should always try to pass a value. This can get cumbersome in many cases, so you want to store a reference to the var, but in that case always try to store the reference in the most local scope.

Another vote for passing the world variable as a parameter, although I have also used the other approach.

Passing it as a variable and thus limiting the variable scope sometimes also helps in understanding your code and in debugging since the world variable then can only be modified or used within this scope, instead of in the entire class. But meh, this is one of these cases where depending on how your code looks one or the other can be better.