WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   Lua/XML Help (https://www.wowinterface.com/forums/forumdisplay.php?f=16)
-   -   Questions about efficient coding (https://www.wowinterface.com/forums/showthread.php?t=54572)

Sweetsour 09-30-16 05:31 PM

Questions about efficient coding
 
Ever since I've started coding in WoW, and Lua consequently, I've learned a lot of tips on how to use more effective coding practices. Though, I still have a couple curiosities I'm hoping to get cleared up.

1. When I'm sharing variables across files, is it better to put them into global namespace, or within the addon?

Example:
Lua Code:
  1. -- Method #1
  2. local addon, private = ...
  3. private.myVariable = 'contents'
  4.  
  5. -- Method #2
  6. _G["myVariable"] = 'contents'

2. Is it ever ideal to use select()? More specifically, if I need to get a variable that's far down a list of arguments, would I use select(), or the "_,_,_,var" method?

Example:
Lua Code:
  1. local _,_,_,_,_,_,_,_,_,var = MyFunction()
  2.  
  3. -- or --
  4.  
  5. local var = select(10, MyFunction())

Fizzlemizz 09-30-16 05:43 PM

1. If you use the addon local table you won't face the possibility of trampling over someone elses global so, method 1.

2. Historically the word has been don't use select, ever, if at all avoidable. While I would avoid (until there's some definitive proof otherwise) using it in repetative situations like for loops, OnUpdate etc. it's not going to kill your addon if it gets called occaisionally.

Sweetsour 09-30-16 05:48 PM

Yea, I remember you said that in the other post that Luanoob posted, but I wanted to get more clarification without trying to hijack his post, thanks for this.

So, for sure, it's better to use something like "local _,_,_,_,_,_,_,_,_,_,_,var" over select() any day of the week, yea?

sticklord 09-30-16 05:50 PM

These are from what I've noticed and are my personal opinions the best way to do it (could be wrong though)

1: Put them in private table, so that it doesn't clutter the global namespace mostly. It also is quicker to lookup.

2: Select is a tiny bit slower, about 20% or so slower than using the "_,_,_,var" method. If it looks better I'd use select :)

Lombra 09-30-16 06:30 PM

select is never "ideal". There are a few cases where it's required (namely, dealing with an argument list of unknown length), anything else is purely a matter of readability.

I would definitely do this, for example:
Code:

if select(10, returnStuff()) then
rather than
Code:

local _, _, _, _, _, _, _, _, _, x = returnStuff()
if x then

unless you're going to be calling it very frequently.

Sweetsour 09-30-16 06:35 PM

Yea, that's what I've typically been using it for.

For example, I have a table that I run through when switching specs to see if a setting is enabled for certain abilities and whether or not their respective talent is learned.

Lua Code:
  1. rowList = {
  2.     [1] = Auras.db.char.aura[1].Thunderstorm,
  3.     [2] = Auras.db.char.aura[1].EarthElemental,
  4.     [3] = Auras.db.char.aura[1].LightningSurgeTotemEle and select(4,GetTalentInfo(3,1,1)),
  5.     [4] = Auras.db.char.aura[1].EarthgrabTotemEle and select(4,GetTalentInfo(3,2,1)),
  6.     [5] = Auras.db.char.aura[1].WindRushTotemEle and select(4,GetTalentInfo(2,3,1)),
  7.     [6] = Auras.db.char.aura[1].GustWindEle and select(4,GetTalentInfo(2,1,1)),
  8. }

If like that is a lot cleaner than _,_,_,isLearned four times. Granted, this code only runs when I switched specs, so its usage is fairly minimal

SDPhantom 09-30-16 10:15 PM

Quote:

Originally Posted by sticklord (Post 319537)
1: Put them in private table, so that it doesn't clutter the global namespace mostly. It also is quicker to lookup.

The lookup time is about the same if the global lookup was written correctly. An indexing operation on a local table is the same as an indexing operation on the global environment. However, as written, the global table is being indexed twice, once to retrieve itself at _G and again to access the variable given.


All times are GMT -6. The time now is 09:55 PM.

vBulletin © 2024, Jelsoft Enterprises Ltd
© 2004 - 2022 MMOUI