Sanity Check Optimization
Hello all,
I found I was frequently forgetting to do basic things like re-equip trinkets/cloaks/rings when I use the various portal items that use these slots. Further, I found myself forgetting to reapply poisons when swapping between specs. Lastly, I would hate going across my logs for a night to find that I had run out of agi pots and was not pre-potting/double potting. This addon essentially performs 3 checks every time a readycheck is performed, first it makes sure my ilevel is reasonable, then it checks to ensure I have a DPS poison on, and lastly it scans my bags to check the number of pots I have (if any). If the conditions aren't met, it just puts a nice annoying box of various colors over my character reminding me that I am a failure in life. The code can be found here: http://pastebin.com/pMhDWT3v The code works exactly as I want, and while not super duper resource heavy, I wanted to throw this out to the far better coders that visit this site to see if I could optimize it further! |
There's no need for separate frames for display and handling events. Being hidden or shown doesn't affect whether a frame receives events.
Declaring all your variables at the file scope instead of the scope where they're actually used isn't a good practice. Your "total" and "equipped" variables are only used in the PLAYER_AVG_ITEM_LEVEL_UPDATE event handler, so you should just stick a "local" in front of the line that assigns them the values returned by GetAverageItemLevel(). Similarly, when you need to run a function to find a value (eg. your CheckBags function) rather than declaring a variable at the top of the file, running the function to update the variable's value, then reading the variable, it's cleaner to just have the function return the desired value. That way you just assign it like any other function regurn, eg. "local count = CheckBags()" -- but in this case, you don't need that CheckBags function at all. Since all you want to know is "how many of item 109217 do I have in my bags?" you can just use the API function that answers that question: Code:
local count = GetItemCount(109217) |
Quote:
Quote:
|
Variables are nothing more than names you use to refer to values. You're not allocating extra memory when declaring them, or even when switching them to hold a completely different value. There is a temporary and negligible allocation under the hood, of course, just as a pointer in C, C++, or equivalent languages has, but it's done on the stack and is thus freed when the variable leaves scope.
You shouldn't be stingy about using variables, since their entire existence is designed to facilitate the flow of logic and readability of your code. |
Someething every day.
Quote:
Thanks to the both of you (Torhal and Phanx). |
All times are GMT -6. The time now is 09:10 AM. |
vBulletin © 2024, Jelsoft Enterprises Ltd
© 2004 - 2022 MMOUI