kamilo 7 Share Posted June 24, 2020 I have gotten used to declaring static variables on top off the class to avoid creating new variables inside of a method (int i = 0). Because i heard new objects created is bad for cpu usage etc/garbage collector. example: private String getHighestHealingFood() { healthRestored = 0; // Declared on top of the class name = ""; // Declared on top of the class for (Item item : getInventory().all(getNormalFilter())) { for (Map.Entry<String, Integer> entry : getFoodMap().entrySet()) { if (item.getName().contains(entry.getKey()) && entry.getValue() > healthRestored) { healthRestored = entry.getValue(); name = entry.getKey(); } } } return name; } as you can see in the above method instead of initializing variables inside the method; i have initialized the variables outside (static variables); so no new ones need to be created and that makes a code a little more efficient; cuz of garbage collector etc But from some of the codes i've been reading on the forums; i am beginning to wonder why all the master scripters don't already do this, and it is a little more efficient even @Articron in his walkTo(); method: As you can see in his inLoop() method he is initializing the variable Tile, Dest; every time the loop runs. So i am wondering why is he doing that if creating new variables everytime he runs the onLoop instead of initializing them at the top; then they would just change inside loop without creating new objects? So you can see why i am confused, since he is a master scripter and he isn't even doing that am i doing something wrong? i want to become a better scripter so i want to learn the CORRECT practise ofcourse. please guide me Link to comment Share on other sites More sharing options...
kamilo 7 Author Share Posted June 24, 2020 So, when is it right to use static variables, because i am using them everywhere so no new objects are created everytime the code is ran; so if my script is a really long one all this pre=declared static variables should save SOME memory etc.. Link to comment Share on other sites More sharing options...
Stoned 52 Share Posted June 24, 2020 Yes you are correct that you should declare the variables at a global level and then use when needed, otherwise the variable gets initialised on each loop which is indeed inefficient. I guess the notice is negligible in some scripts and I'm sure some would argue it is negligible over all - I guess it is more of a 'good habit' more than an requirement. I'd definitely get into the habit if you can though - it will only improve your scripts! Link to comment Share on other sites More sharing options...
kamilo 7 Author Share Posted June 25, 2020 5 hours ago, StonedFarmer said: Yes you are correct that you should declare the variables at a global level and then use when needed, otherwise the variable gets initialised on each loop which is indeed inefficient. I guess the notice is negligible in some scripts and I'm sure some would argue it is negligible over all - I guess it is more of a 'good habit' more than an requirement. I'd definitely get into the habit if you can though - it will only improve your scripts! Oh thank you! i thought i was doing something wrong by globally initializing static variables since i didn't see anyone else using them. if its a good practise, i'll definitely keep it up! Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.