lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


It was thus said that the Great Patrick Mc(avery once stated:
> 
> My newest project will probably be under 1K lines of code and would work 
> with a poorly designed layout but I want to improve my habits. I do 
> understand PIL/modules/upvalues quite well now but I am wondering if 
> there are different strategies for managing much larger projects. I 
> understand that lightroom is over 100k lines of Lua and there are other 
> large applications too. Are there numerous design patterns out there to 
> keep people from clobbering variables and organizing this much code? Do 
> people usually just divide their code up into many modules? It seems 
> that this is the main way privacy is achieved in let's say Python. Lua 
> is a different tool and I want to learn the best way to use it 
> irrespective of other languages.

  Given that "require" and "module" add to the global namespace, I've been
creating modules (for my own use but some have been released) under a module
called "org.conman" (my domain reversed).  That way, my "org.conman.dns"
(for a working example) does not conflict with any other "dns" module I may
have loaded.  I also have "org.conman.table" (with a few useful functions
for tables), "org.conman.string" and "org.conman.debug".  I don't think
anyone else has done that for Lua, but it *is* a popular pattern with Java
from what I understand.  

  Anyway, with that, I can do:

    local dns    = org.conman.dns
    local debug  = setmetatable(org.conman.debug  , { __index = _G.debug  })
    local string = setmetatable(org.conman.string , { __index = _G.string })

and not have to worry about clashes.  

  -spc