Thread Tools Display Modes
02/03/15, 02:34 PM   #1
DrGerm
 
DrGerm's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 2
Question Which way should I display a value gained from a constant?

OK, complete newb here (no programming experience whatsoever). The following is in the context of displaying the value of a Constant value to chat, but I think this is a general question about getting and displaying stored values. I will eventually be doing a little more than just printing the actual number to chat.

Should I do:

Example 1:

local currentMasterVolume = GetSetting(SETTING_TYPE_AUDIO, AUDIO_SETTING_AUDIO_VOLUME);
d(currentMasterVolume)
or Example 2:

d(GetSetting(SETTING_TYPE_AUDIO, AUDIO_SETTING_AUDIO_VOLUME))
Both of these work when I test them. I've seen examples in other addons like Example 1, but Example 2 seems simpler. However, I have a feeling that I'm missing something about how to code correctly.

Thanks!
  Reply With Quote
02/03/15, 04:34 PM   #2
circonian
AddOn Author - Click to view addons
Join Date: May 2014
Posts: 613
Originally Posted by DrGerm View Post
Both of these work when I test them. I've seen examples in other addons like Example 1, but Example 2 seems simpler. However, I have a feeling that I'm missing something about how to code correctly.
Either way it does the same thing.

Some of what you saw (written either way) may have just been personal prefference &/or to make the code easier to read. Think if you nested a few of these (this is a slightly exagerated example, but will hopefully let you see the problem):
Lua Code:
  1. d(func1(func1Param1, func1Param2, func2(func2Param1, func2Param2, func3(func3Param1, func3Param2), func2Param4), func1Param4))
It gets hard to read and I gave all of the functions very short names. So in some cases it might be beneficial to save the return value in local variables and then pass it into whatever else needs it just to make it easier to read:
Lua Code:
  1. local func3ReturnValue = func3(func3Param1, func3Param2)
  2. local func2ReturnValue = func2(func2Param1, func2Param2, func3ReturnValue, func2Param4)
  3. local func1ReturnValue = func1(func1Param1, func1Param2, func2ReturnValue, func1Param4)
  4. d(func1ReturnValue)

Also if the value is used anywhere else in the code it would be more beneficial to save the value in a local variable and then use that throughout the code instead of calling GetSetting(..) every time you want that value. One exception to that is if you were calling a function to grab a value that could possibly change as your code is running and you need to make sure you always have the most up to date value then you might be better off calling the function more than once.

Last edited by circonian : 02/03/15 at 04:39 PM.
  Reply With Quote
02/03/15, 07:18 PM   #3
DrGerm
 
DrGerm's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 2
Thumbs up

Originally Posted by circonian View Post
Either way it does the same thing...
Perfect; thanks for the tips. I think I better understand the benefits to either strategy now; I see how this fits into my project now

Last edited by DrGerm : 02/03/15 at 07:21 PM.
  Reply With Quote
02/03/15, 07:33 PM   #4
Sasky
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 231
Originally Posted by circonian View Post
One exception to that is if you were calling a function to grab a value that could possibly change as your code is running and you need to make sure you always have the most up to date value then you might be better off calling the function more than once.
That's risky since you can be inconsistent using different values. The other thing to think about is that function calls have overhead, even if they don't have to communicate with the server, so you'd want to avoid calling more than once.
  Reply With Quote
02/03/15, 10:34 PM   #5
circonian
AddOn Author - Click to view addons
Join Date: May 2014
Posts: 613
Originally Posted by Sasky View Post
That's risky since you can be inconsistent using different values. The other thing to think about is that function calls have overhead, even if they don't have to communicate with the server, so you'd want to avoid calling more than once.
Sasky is right, I didn't make that very clear: the "different values" Sasky mentioned was what I was referring to when I said if the values can change as your code is running and you need an up to date value then call it again.

I really meant different parts of your code/addon/functions possibly, that may run at different times. You may need an up to date value if something has changed. Like in your question: if you were grabbing that audio setting when your addon loaded, but "later" you wanted to do something with it, the user or an addon may have changed the audio setting since last time you grabbed it so you would probably need to get that value again to make sure your working with the correct value.
  Reply With Quote

ESOUI » Developer Discussions » Lua/XML Help » Which way should I display a value gained from a constant?


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