What is best practice?

I was wondering what the best way to handle the following examples are, with speed being the main focus.

Example 1:

if a \> 50 then  
  
 if b \> 10 then  
 --do something  
 elseif c \> 10 then  
 --do something else  
 end  
end  

OR:

if a \> 50 and b \> 10 then  
 --do something  
elseif a \> 50 and c \> 10 then  
 --do something else  
end  

Example 2:

local function callMe()  
 --do something  
end  
  
local function runMe()  
 callMe()  
end  
  
runMe()  

OR

local function runMe()  
  
 local function callMe()  
 --do something  
 end  
  
 callMe()  
end  
  
runMe()  

Any insights would be appreciated. [import]uid: 129287 topic_id: 25832 reply_id: 325832[/import]

Example 1 : This is a semantic issue.

Type A results in less instructions executed (hence, your app runs faster) but you will probably never notice the performance gain unless you do a large volume of those calls. In most short-use situations Type B is superior because it’s easier to read.

My advice? Focus exclusively on making your code easy to read at first and then optimize later if needed.

Example 2 : Er, can you even declare a function like that? I assumed you had to do either:

local function runMe() -- or... local runMe = function()

In regards to your Type A or Type B examples though, it really depends on (again) how often you call versus code clarity. Keeping the function outside is good for keeping your functions isolated/seperated. But it’s faster to read code within the local scope. (Again, just probably not enough faster unless you call it an awful lot)

Anyway, my two cents… [import]uid: 41884 topic_id: 25832 reply_id: 105292[/import]

Example 1: either should be fine. There might be a tiny difference in speed, but as Richard says, it’s doubtful that you’d notice it unless you’re running this conditional check like 1000 times in a loop. Go with what’s easier for you to read/write in this case.

Example 2: Again, either should be fine. I like “wrapping” little functions within other functions when they’re directly related. Easier to find them that way. Of course, make sure you don’t need that internal function for any other purpose, because you won’t be able to directly call it once it’s wrapped in the outer function.

Anyway, good questions. I think “best practice” methods are useful for all developers to read, even those who have been coding longer, just as a reminder to be vigilant on performance.

Brent Sorrentino
[import]uid: 9747 topic_id: 25832 reply_id: 105307[/import]

@richard9: Thank you for taking time sharing your insights. You’re absolutely right about the declaring of the functions. I don’t know how that happened, but I’ll fix the example. It’s nice to get other people’s views and experience on coding practice, and what you’re saying makes sense. Although, in example 1 I actually find B the easiest to read. If there isn’t anything (or very little) to gain from using one over the other, I’ll go for clearity and readability. Thanks again.

@Brent Sorrentino: Thank you for joining the discussion and for confirming richard’s thoughts. The more people agreeing on the method, the more likely it’s the best practice. [import]uid: 129287 topic_id: 25832 reply_id: 105309[/import]

Note that Example 2-B is perfectly valid and not a “mistake”. :slight_smile: You can definitely wrap functions within each other, and call the inner function from within the outer function. A very relevant example is calling the inner function after a short timer, like this:

local function runMe()  
  
 local function callMe()  
 --do something  
 end  
 timer.performWithDelay( 100, callMe)  
  
end  

You could also pull the inner function out, place it somewhere above, and call it via the same timer, but personally I find it convenient to wrap them together. If “callMe()” is a very simple function that is primarily (or only) used by “runMe()”, then I know where to find it, and that it probably doesn’t need to used elsewhere in my game.

Brent
[import]uid: 9747 topic_id: 25832 reply_id: 105314[/import]

@Brent: I’ve already edited out the mistake which was in the declaring of the function. I had written local function = runMe() instead of local function runMe() or local runMe = function() as richard9 mentioned. [import]uid: 129287 topic_id: 25832 reply_id: 105321[/import]