Should I have a global require or local?

I have a file lets call it utilities.lua it needs to be required by quite a few other files sometimes these files are inside other files. For example 

fileB.lua is required inside fileA.lua and both of them need the utilities.lua…but sometimes fileB is loaded on its own as well as fileA on its own. Is it better to require utilities.lua locally in each file which might lead to it be required duplicate times or is it better to require it globally in like the main.lua?  

You’re best off maintaining your utility code in a module and re- requiring it as needed.  

The first time you require (load) a module, it is read from ‘disk’ and loaded into memory.  It stays there unless you force it out.

Subsequent calls to require get the already loaded module right from memory.

utils.lua

local utils = {} function utils.doit() print("HI") end return utils

test1.lua

local utils = require "utils" local test= {} function test.run() utils.doit() end return test

test2.lua (same code, just an example for discussion)

local utils = require "utils" local test= {} function test.run() utils.doit() end return test

main.lua

local test1 = require "test1" -- Causes 'utils.lua' to be read from 'disk' and loaded into memory local test2 = require "test2" -- Simply gets already loaded 'utils.lua' from memory test1.run() test2.run()

One more note:

If you did this instead, you’d be wasting memory and making your code too complex:

test1.lua

local function doit() print("HI") end local test= {} function test.run() doit() end return test

test2.lua 

local function doit() print("HI") end local test= {} function test.run() doit() end return test

main.lua 

local test1 = require "test1" -- Declares function doit() local test2 = require "test2" -- Declares function doit() again (same functionality; twice the memory) test1.run() test2.run()

Finally, you could do this, but it’s just messy and a bit slower:

test1.lua

local test= {} function test.run() doit() end return test

test2.lua

local test= {} function test.run() doit() end return test

main.lua 

\_G.doit = function()    print("HI") end local test1 = require "test1" -- Calls global doit() local test2 = require "test2" -- Calls global doit() test1.run() test2.run()

Okay so just so I’m clear 

If we have a module and we load it in locally into scene1 and then close scene1 and load scene2 the module would still be loaded into memory or is it just if multiple files within the same scene call it because it would then be passed by reference and not created multiple times? I’m assuming when scene 1 is closed it wouldn’t still be loaded into memory but just want to confirm. 

If you load a module in scene 1 and go to scene 2, your module is still loaded, but you don’t have access to it.  When you do your require in scene 2, you don’t load the module again (it’s already loaded), but you get a reference to the previously loaded module.

This is important because if your module executes code as part of it being required, that code will execute on the first require, but not subsequent requires unless you “un-require” the module before reloading this.  BTW composer.removeScene() does an un-require unless you pass in true for the recycle parameter.

But for things like utility / helper function libraries, you typically don’t execute the functions as you load them, but just want access to the function to run them when you need them.

Rob

You’re best off maintaining your utility code in a module and re- requiring it as needed.  

The first time you require (load) a module, it is read from ‘disk’ and loaded into memory.  It stays there unless you force it out.

Subsequent calls to require get the already loaded module right from memory.

utils.lua

local utils = {} function utils.doit() print("HI") end return utils

test1.lua

local utils = require "utils" local test= {} function test.run() utils.doit() end return test

test2.lua (same code, just an example for discussion)

local utils = require "utils" local test= {} function test.run() utils.doit() end return test

main.lua

local test1 = require "test1" -- Causes 'utils.lua' to be read from 'disk' and loaded into memory local test2 = require "test2" -- Simply gets already loaded 'utils.lua' from memory test1.run() test2.run()

One more note:

If you did this instead, you’d be wasting memory and making your code too complex:

test1.lua

local function doit() print("HI") end local test= {} function test.run() doit() end return test

test2.lua 

local function doit() print("HI") end local test= {} function test.run() doit() end return test

main.lua 

local test1 = require "test1" -- Declares function doit() local test2 = require "test2" -- Declares function doit() again (same functionality; twice the memory) test1.run() test2.run()

Finally, you could do this, but it’s just messy and a bit slower:

test1.lua

local test= {} function test.run() doit() end return test

test2.lua

local test= {} function test.run() doit() end return test

main.lua 

\_G.doit = function()    print("HI") end local test1 = require "test1" -- Calls global doit() local test2 = require "test2" -- Calls global doit() test1.run() test2.run()

Okay so just so I’m clear 

If we have a module and we load it in locally into scene1 and then close scene1 and load scene2 the module would still be loaded into memory or is it just if multiple files within the same scene call it because it would then be passed by reference and not created multiple times? I’m assuming when scene 1 is closed it wouldn’t still be loaded into memory but just want to confirm. 

If you load a module in scene 1 and go to scene 2, your module is still loaded, but you don’t have access to it.  When you do your require in scene 2, you don’t load the module again (it’s already loaded), but you get a reference to the previously loaded module.

This is important because if your module executes code as part of it being required, that code will execute on the first require, but not subsequent requires unless you “un-require” the module before reloading this.  BTW composer.removeScene() does an un-require unless you pass in true for the recycle parameter.

But for things like utility / helper function libraries, you typically don’t execute the functions as you load them, but just want access to the function to run them when you need them.

Rob