04-12-06, 10:28 AM | #1 |
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 ... 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 Code:
for k in whatever do if not k["one"] or not k["one"]["really"] then GOTONEXT end doSomething(k) end 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 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 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 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 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. |
|
WoWInterface » Developer Discussions » Lua/XML Help » Code optimization for novices |
«
Previous Thread
|
Next Thread
»
|
Display Modes |
Switch to Linear Mode |
Switch to Hybrid Mode |
Threaded Mode |
|
|