Quantcast Questions about efficient coding - WoWInterface
Thread Tools Display Modes
09-30-16, 05:31 PM   #1
Sweetsour
A Flamescale Wyrmkin
AddOn Author - Click to view addons
Join Date: Dec 2014
Posts: 128
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())
  Reply With Quote
09-30-16, 05:43 PM   #2
Fizzlemizz
I did that?
 
Fizzlemizz's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Dec 2011
Posts: 1,214
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.
__________________
Fizzlemizz
Maintainer of Discord Unit Frames and Discord Art.
Author of FauxMazzle, FauxMazzleHUD and Move Pad Plus.
  Reply With Quote
09-30-16, 05:48 PM   #3
Sweetsour
A Flamescale Wyrmkin
AddOn Author - Click to view addons
Join Date: Dec 2014
Posts: 128
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?
  Reply With Quote
09-30-16, 05:50 PM   #4
sticklord
A Wyrmkin Dreamwalker
AddOn Author - Click to view addons
Join Date: Aug 2014
Posts: 57
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
  Reply With Quote
09-30-16, 06:30 PM   #5
Lombra
A Molten Giant
 
Lombra's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2006
Posts: 554
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.
__________________
Grab your sword and fight the Horde!

Last edited by Lombra : 09-30-16 at 06:33 PM.
  Reply With Quote
09-30-16, 06:35 PM   #6
Sweetsour
A Flamescale Wyrmkin
AddOn Author - Click to view addons
Join Date: Dec 2014
Posts: 128
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

Last edited by Sweetsour : 09-30-16 at 06:39 PM.
  Reply With Quote
09-30-16, 10:15 PM   #7
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 1,919
Originally Posted by sticklord View Post
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.
__________________
ESOUI AddOns | WoWInterface AddOns
"All I want is a pretty girl, a decent meal, and the right to shoot lightning at fools."
-Anders (Dragon Age: Origins - Awakening)
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Questions about efficient coding

Thread Tools
Display Modes

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