Quantcast how is better to write ... - WoWInterface
Thread Tools Display Modes
03-29-20, 07:48 AM   #1
gmarco
A Frostmaul Preserver
 
gmarco's Avatar
AddOn Author - Click to view addons
Join Date: Dec 2009
Posts: 290
how is better to write ...

Hi all,

having a lot of times in this period I am here polishing and refreshing my addons code ...

A couple of questions:

1) having code like this:

Lua Code:
  1. local ldb = LibStub:GetLibrary("LibDataBroker-1.1")
  2. local dataobj = ldb:NewDataObject("gmVideoSwitch", {
  3.     type = "data source",
  4.     icon = "Interface\\Addons\\"..ADDON.."\\bluewin.tga",
  5.     text = "-"
  6. })

Lua Code:
  1. function dataobj.OnEnter(self)
  2.     -- code here
  3. end

or

Lua Code:
  1. dataobj.OnEnter = function(self)
  2.     -- code is here
  3. end

The above statement are equal I suppose, which is better to be written ?

And the second question.

Being defined as:

Lua Code:
  1. local dataobj

all its functions are local too automagically or it is better to defined them local too ?

Thanks.
__________________
This is Unix-Land. In quiet nights, you can hear the Windows machines reboot.
  Reply With Quote
03-29-20, 09:15 AM   #2
myrroddin
A Pyroguard Emberseer
 
myrroddin's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2008
Posts: 1,107
Look at my code in your other thread.
  Reply With Quote
03-29-20, 11:54 AM   #3
gmarco
A Frostmaul Preserver
 
gmarco's Avatar
AddOn Author - Click to view addons
Join Date: Dec 2009
Posts: 290
Hi myrroddin,

So it is better to write functions in this way ?

Lua Code:
  1. dataobject.OnClick = function(object, button)
  2.     -- open configuration
  3. end

even if I put them out of its context ?

But I'd like to know also if it could be a problem to write in the other way or at the end of the story both syntax works in the same way with the same results.

Thanks so much for your time.



Thanks.
__________________
This is Unix-Land. In quiet nights, you can hear the Windows machines reboot.
  Reply With Quote
03-29-20, 12:46 PM   #4
Seerah
Fishing Trainer
 
Seerah's Avatar
WoWInterface Super Mod
Featured
Join Date: Oct 2006
Posts: 10,720
As you said, they're equal.
__________________
"You'd be surprised how many people violate this simple principle every day of their lives and try to fit square pegs into round holes, ignoring the clear reality that Things Are As They Are." -Benjamin Hoff, The Tao of Pooh

  Reply With Quote
03-29-20, 01:38 PM   #5
gmarco
A Frostmaul Preserver
 
gmarco's Avatar
AddOn Author - Click to view addons
Join Date: Dec 2009
Posts: 290
Thanks sp much Seerah.

I have tried to write in both way and they obviusly works but before rewrite all addons in the same way and try to write everytime in the same ways I wanted to be sure

And about the fact that the "dataobj" is defined local ... all its functions are local by defaults or I have to defined them "local" to let them be local ?

Thanks.
__________________
This is Unix-Land. In quiet nights, you can hear the Windows machines reboot.
  Reply With Quote
03-29-20, 06:54 PM   #6
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 1,906
In general, they're equal, but there is a very niche difference between them, and that's the order of when the function is compiled in memory and when the variable it's assigned to is created. This difference can only be seen when dealing with local functions trying to access their own pointer, usually by recursive calling.


Code:
local function RecursiveFunc()
The local is created before the function is compiled. This function can see the local variable and is capable of calling itself.


Code:
local RecursiveFunc=function()
The function is compiled before the local is created, so this function cannot see itself. Attempts to call recursively will try to look for it in the global environment.
__________________
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)

Last edited by SDPhantom : 03-29-20 at 07:08 PM.
  Reply With Quote
03-29-20, 11:39 PM   #7
myrroddin
A Pyroguard Emberseer
 
myrroddin's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2008
Posts: 1,107
I'd suggest creating the LDB object in PLAYER_LOGIN because you only need to do that once. You could do in the main chunk of your addon code but that would force you to look up the object to see if it exists, and if so, get its value, and if not, create it. That's doing more CPU work than necessary. Anyway, here is the variant if you do not use PLAYER_LOGIN:
Code:
local dataobj = dataobj or ldb:NewDataObject("gmVideoSwitch", {
    -- blah
})
  Reply With Quote
03-30-20, 03:13 AM   #8
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 1,906
Originally Posted by gmarco View Post
And the second question.

Being defined as:

Lua Code:
  1. local dataobj

all its functions are local too automagically or it is better to defined them local too ?

Thanks.
I missed this question and it looks like it hasn't been answered yet. dataobj in your code ends up being a table. Everything in the table are entries. They're neither locals nor globals. In terms of performance, each indexing operation you perform is akin to accessing a global. This also means when you have nested tables, each indexing operation stacks up when you navigate through them to reference an entry.
__________________
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
03-30-20, 08:07 AM   #9
gmarco
A Frostmaul Preserver
 
gmarco's Avatar
AddOn Author - Click to view addons
Join Date: Dec 2009
Posts: 290
Originally Posted by myrroddin View Post
I'd suggest creating the LDB object in PLAYER_LOGIN because you only need to do that once. You could do in the main chunk of your addon code but that would force you to look up the object to see if it exists, and if so, get its value, and if not, create it. That's doing more CPU work than necessary.
I see what you mean ... the problem is for my way of writing that usually I create the frames in the last part of the code of the addon and if I put the create of LDB object there I was unable to build all the others part of the dataobj .


Usually I write in similar way:

Lua Code:
  1. local ADDON, namespace = ...
  2. local L = namespace.L
  3.  
  4. -- code
  5.  
  6. local LibQTip = LibStub('LibQTip-1.0')
  7. local ldb = LibStub:GetLibrary("LibDataBroker-1.1")
  8.  
  9. local dataobj = ldb:NewDataObject(ADDON, {
  10.     type = "data source",
  11.     icon = "Interface\\Addons\\"..ADDON.."\\icon.tga",
  12.     text = "-"
  13. })
  14.  
  15. local frame = CreateFrame("Frame")
  16.  
  17. local function OnRelease(self)
  18.      -- code
  19. end  
  20.  
  21. local function Button_OnClick(row, arg, button)
  22.      -- code
  23. end
  24.  
  25. local function anchor_OnEnter(self)
  26.  
  27.     arg = {}
  28.  
  29.     if self.tooltip then
  30.         LibQTip:Release(self.tooltip)
  31.         self.tooltip = nil  
  32.     end
  33.     local row,col
  34.     local tooltip = LibQTip:Acquire(ADDON.."tip", 6,"LEFT","LEFT","LEFT","LEFT","RIGHT","RIGHT")
  35.     self.tooltip = tooltip
  36.     tooltip:SmartAnchorTo(self)
  37.     tooltip:EnableMouse(true)
  38.     tooltip.OnRelease = OnRelease
  39.     tooltip.OnLeave = OnLeave
  40.     tooltip:SetAutoHideDelay(.1, self)
  41.  
  42.     row,col = tooltip:AddLine("")
  43.     row,col = tooltip:AddLine("")
  44.  
  45.     if #deaths > 0 then
  46.  
  47.         row,col = tooltip:AddLine()
  48.         tooltip:SetCell(row,1,L["Deaths Reports"],"CENTER",6)
  49.         tooltip:SetColumnTextColor(1,1,1,0)
  50.         row,col = tooltip:AddLine("")
  51.  
  52.     -- code
  53.  
  54.     row,col = tooltip:Show()
  55. end
  56.  
  57. function dataobj.OnEnter(self)
  58.     anchor_OnEnter(self)
  59. end
  60.  
  61. function dataobj.OnLeave(self)
  62.     -- Null operation: Some LDB displays get cranky if this method is missing.
  63. end
  64.  
  65. function dataobj.OnClick(self, button)
  66.    -- code
  67. end
  68.  
  69. --code
  70. frame:RegisterEvent("PLAYER_ENTERING_WORLD")
  71. frame:SetScript("OnEvent", function(self, event, ...)
  72.    
  73.     if event == "ZONE_CHANGED_NEW_AREA" or event == "PLAYER_ENTERING_WORLD" then   
  74.         -- code
  75.     end
  76.    
  77.     if event == "COMBAT_LOG_EVENT_UNFILTERED" then
  78.                -- code
  79.     end
  80. end)


Thanks so much for your inputs !
__________________
This is Unix-Land. In quiet nights, you can hear the Windows machines reboot.
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » how is better to write ...

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