Thread Tools Display Modes
Prev Previous Post   Next Post Next
04-12-06, 10:28 AM   #1
Maia
A Deviate Faerie Dragon
AddOn Author - Click to view addons
Join Date: Feb 2006
Posts: 15
Code optimization for novices

I've been working on an addon (which atm is only available to my guildies) during the past weeks - and the more it grows, the more I feel the need for optimizing my code.

I never really learned programming, besides writing extensive JavaScript functions for DHTML a few years ago. So I'm not fluent with object orientation, and my lua code is based on the code I see in so many other addons I analyze.

On the other side if I try to grasp the programming guide at lua.org/pil I mostly fail. Partly because I'd need real world exampled, partly because it seems as if it's written for professional programmers.

So if anyone could help me solve some probably pretty basic problems, I'd appreciate it.

1) the 'local function' problem.
I try not to pollute the namespace with global functions and variables - but as soon as my addon has more than one .lua file (and I prefer to have 5-6 smaller files than one with 1500 lines of code) I don't know how to solve that problem.
So how do I define functions that should only be available to my addon but be available in all my .lua files?

2) the 'local variable' problem.
Somewhere I've seen the concept to store all variables in an array, and to set a short alias name to it to have smaller code. E.g.:
Code:
myAddonData = {}
local m = myAddonData
m.whatever = "one"
m.somethingelse = true
...
This works great if I don't try to access m.xxx in that single lua file. What doesnt work is to define local m = myAddonData in each of my lua files, as they seem to overwrite each other. So what's the solution (other than removing the 'local', therefor making m accessible globally)?

3) problem with loops pt1:
How do I skip to the next data in my loop? I'd like to replace something like:
Code:
for k in whatever do
   if k["one"] then
      if k["one"]["really"] == true then
         doSomething(k)
      end
   end
end
by:
Code:
for k in whatever do
   if not k["one"] or not k["one"]["really"] then GOTONEXT end
   doSomething(k)
end
well, what would I have to replace GOTONEXT with to skip all k I don't want to deal with? I hate nested if statements...

4) problem with loops pt2:
The following just doesn't feel right:
Code:
function getBestRank(spell,damage)
   local rank = 1
   for i = 1, ranks[spell] do
      if healamount[spell][rank] < damage then rank = rank + 1
      else break
      end
   end
   rank = rank - 1
   if rank < 1 then rank = 1
   return rank
end
If it's not obvious, I'd like to loop through ranks of a healing spell and choose the rank that is slightly below the damage of my target. I'd like to underheal slightly, but not overheal.

5) problem with loops pt3:
Do I really need to declare a local here and then use it? Isn't there a more elegant way?
Code:
local really = true
for k in something do
   if not areYouSure(k) then really = false end
end
if really then ... else ... end
6) sorting non-indexed (?) tables/arrays
I'm using an array that stores various info about each raid member which I update on PARTY_MEMBERS_CHANGED:
Code:
roster = { }
for i = 1,GetNumRaidMembers() do
   local name = UnitName("raid"..i)
   roster[name] = {}
   roster[name]["raidid"] = 'raid'..i
   _,roster[name]["class"] = UnitClass('raid'..i)
   _,_,roster[name]["group"] = GetRaidRosterInfo(i)
end
Now I'd like to store current health for each unit, and then find the 5 units with the lowest health (yes, the emergency monitor). My solution atm is:
Code:
health = { }
for name in roster do
   local unit = roster[name].id
   tinsert(health, { name, UnitHealth(unit)/UnitHealthMax(unit) })
end
table.sort(
   health, 
   function(a,b)
      return a[2] < b[2]
   end
)
needy = { }
local i = 1
for k,v in health do
   needy[i] = {}
   needy[i].name = v[1]
   needy[i].hp = v[2]
end
But, do I really need to define a second and a third array? I guess not, but I don't know how to sort the array 'roster' by e.g. roster[name].hp and then access the first five...

I know. Lots of questions. Some may sound very noobish to you. But if you could help optimizing the one or other code snippet, I'd really appreciate it. I'm trying to learn something new from the lua.org guide day for day, but I havent found an answer to my question yet - other than "yeah, use metatables and __.index" - "uum?".

Thanks in advance.

PS: I just found out about the existance of pastbin.com, so I uploaded the sample code snippets there. So if you have a solution, you can update the code online:
http://maia.pastebin.com/656331

Last edited by Maia : 04-12-06 at 02:08 PM.
  Reply With Quote
 

WoWInterface » Developer Discussions » Lua/XML Help » Code optimization for novices


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off