Does it matter how I order my variables?

There is some really weird behavior going on. I am making only one function call at the very bottom of my file called main().  Above that I make a myriad of function and local variable declarations intertwined. So it like

local a

local b

function d() *some code* end

function e() *some code*  end

local f

local g

function h() *some code* end

function main() d() e() h()  end

main()

The weird thing is that different behavior occurs based on this ordering above main. So the above performs differently if b is declared below d instead of above it. Why is this? and how can I avoid this?

It shouldn’t make any difference in the code you posted here. I’m assuming there is some code in one of the functions that changes something and by calling it sooner it has a different result. Just a guess though as I don’t know the actual code. The code you posted above will work the exact same no matter what order you declare the functions and variables as long as the variables are declared before the function is called. 

That code is hard to read though I personally structure everything like so

–Requires

– Local variables

– Functions

That way all my declared variables are at the top of the page. This not only helps debugging but it also makes sure that the variables are declared before any function calls. There really is no right or wrong way but mixing variables and functions seems confusing to me.

Also another note I wouldn’t name your function main(). I’m not 100% sure if that word is exclusive or not but I’ve had issues in the past using generic terms like that. I was using the variable debug and it was causing all kinds of problems. 

Same here. Every module is ordered in the same way:

  • requires

  • local variables

  • forward declarations

  • functions

This really works perfect for me to keep things clear and well-documented, so I highly advise this way of working.

Cheers,

Thomas

It shouldn’t make any difference in the code you posted here. I’m assuming there is some code in one of the functions that changes something and by calling it sooner it has a different result. Just a guess though as I don’t know the actual code. The code you posted above will work the exact same no matter what order you declare the functions and variables as long as the variables are declared before the function is called. 

That code is hard to read though I personally structure everything like so

–Requires

– Local variables

– Functions

That way all my declared variables are at the top of the page. This not only helps debugging but it also makes sure that the variables are declared before any function calls. There really is no right or wrong way but mixing variables and functions seems confusing to me.

Also another note I wouldn’t name your function main(). I’m not 100% sure if that word is exclusive or not but I’ve had issues in the past using generic terms like that. I was using the variable debug and it was causing all kinds of problems. 

Same here. Every module is ordered in the same way:

  • requires

  • local variables

  • forward declarations

  • functions

This really works perfect for me to keep things clear and well-documented, so I highly advise this way of working.

Cheers,

Thomas