View Single Post
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