Efficient design for your lua library classes ?
Posted: Fri Feb 01, 2013 8:33 am
Hi all,
I would like to have some clever thoughts about good habits on making library classes, especially with Lua.
In terms of clean design and efficiency, of course.
Well, as I am not a former coder, just a random guy who's hanging with Lua, I don't have a clear opinion about what is best way to proceed. But I always considered that, when designing a class library, most of the time you 're hiding complex operations in the internals of the library source. Then, you expose a public interface to the user, with public methods.
I also tend to think that one should not trust (at all) to the user inputs. So, inside methods, it might be better to perform some argument checking, before processing the inputs and return the results. And I'm fine with that. Problem is, one thing I hate is, when an err occurs because invalid args were passed-in, the stack trace report the line number that failed inside the library itself. Well, to prevent that, and just report the line from the user code that caused the problem, I can use Lua's errorfunction, at level 2. Doing this in each and every single method you esecially care can end up with an ugly code... Any thoughts on this ?
Second, class attributes. You might want to define some specific attributes inside a class, and you don't want the user to be able access these attributes explicitely. But instead, you'd like to force him to use some getters/setters you provide as library methods. Actually, proxy tables can do the trick. Or at least, define these attributes as locals in the library code, as follows :
Once again, any thoughts on this ?
That would be all. For now.
Thanks reading.
I would like to have some clever thoughts about good habits on making library classes, especially with Lua.
In terms of clean design and efficiency, of course.
Well, as I am not a former coder, just a random guy who's hanging with Lua, I don't have a clear opinion about what is best way to proceed. But I always considered that, when designing a class library, most of the time you 're hiding complex operations in the internals of the library source. Then, you expose a public interface to the user, with public methods.
Code: Select all
local internal_vars
-- variable declarations
local internal_funcs = ...
-- funcs declarations
-- the class itself
local libClass = {}
-- some methods
function libClass:methodA(...) ... end
function libClass:methodB(...) ... end
-- exposes the interface to the user
return libClass
Second, class attributes. You might want to define some specific attributes inside a class, and you don't want the user to be able access these attributes explicitely. But instead, you'd like to force him to use some getters/setters you provide as library methods. Actually, proxy tables can do the trick. Or at least, define these attributes as locals in the library code, as follows :
Code: Select all
local attr1 = ...
local libClass = {...}
function libClass:setAttr1(...) ... end
function libClass:getAttr1() return attr1 end
That would be all. For now.
Thanks reading.