Which is better: local foo = require("module1") in each screen OR require("module1") in main.lua?

Hi guys.

I am trying to understand better the use of require (as in require external module like ui, particle candy and so on…)

In some codes here in the forum, I see either:

A- local foo = require(“module1”)

OR

B- require(“module1”)

My questions:

1- Which one is better in term of memory usage and application speed?

2- Where do you guys usually put your require statements? In main.lua (using B so the modules are globally accessible)? OR do you require modules as you need them using A? If the latter, what happened if say you need the ui.lua (for buttons) on multiple screens (director)?

Thank you for any pointers. i am having a hard time with a “simple” statement like require!

Mo [import]uid: 49236 topic_id: 17432 reply_id: 317432[/import]

Remember that “require” loads a file only once. Invoking “require” multiple times for the same Lua file does not add any performance penalty to your application.

If you are trying to program in a modular fashion, which I highly recommend for ease of debugging and extending, then definitely go with the “local var = require( file )” approach.

For an example of an object-oriented Corona framework that uses this approach, check out my new framework CooL (Corona Object-Oriented Lua). It takes that exact same approach:

https://github.com/dejayc/CooL/blob/master/Classes/CooL

Note that according to the excellent book “Programming in Lua” (http://www.lua.org/pil/15.4.html):

A more disciplined approach is to declare as locals only the functions you need, or at most the packages you need.

That’s solid advice. [import]uid: 71767 topic_id: 17432 reply_id: 72607[/import]