View Single Post
08-30-17, 12:02 AM   #7
MunkDev
A Scalebane Royal Guard
 
MunkDev's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2015
Posts: 431
Originally Posted by Terenna View Post
When I saw the difference in readability between arg2 and spellID or what not I saw the importance of naming variables more descriptively. One possible problem with this is that the 12th argument for a swing_damage event is a an amount of damage or a string of miss, block, parry, dodge, etc, whereas the 12th argument for spell_damage is a spellID. To assuage this problem, I could have the first 4 arguments set as they are always the same, and then perform an if check on the 2nd argument to determine type of CLEU event. At that point I could assign variables that would be more accurately and descriptively named, but I would be utilizing extra CPU cycles to perform the check, overwrite the 2nd and 4th arguments unnecessarily and also have extra variables taking up (albeit minuscule) amounts of memory.
It's very common to use event-specific functions stored in your frame table and then run them from your event handler. This helps to compartmentalise the code and effectively removes the problem of choosing whether to name your arguments descriptively or not.

Lua Code:
  1. frame:SetScript("OnEvent", function(self, event, ...)
  2.     if self[event] then
  3.         self[event](self, ...)
  4.     end
  5. end)
  6.  
  7. function frame:PLAYER_ENTERING_WORLD(...)
  8.     -- code
  9. end
  10.  
  11. function frame:PLAYER_REGEN_DISABLED(...)
  12.     -- code
  13. end
  14.  
  15. function frame:COMBAT_LOG_EVENT_UNFILTERED(...)
  16.     -- code
  17. end

Any code that is general and applicable to every event can be written directly in the event handler. In the case of processing combat events, you can apply the same principle. In terms of optimisation, there are a couple of things you can do, such as upvalueing the sub table in your color picker instead of doing the table lookup 3 times (one for each color value), but it will hardly make any difference with such a tiny code snippet.
__________________

Last edited by MunkDev : 08-30-17 at 12:47 AM.
  Reply With Quote