lua-users home
lua-l archive

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


On Thu, Feb 2, 2012 at 18:35, Fabien Bourgeois <fbourgeois@yaltik.com> wrote:
> You're right. I guess it's an old Python reminiscence, just a convention. I
> should be better to not expose private methods at all.

Using locals for private functions makes them faster (with the PUC
interpreter, but probably not in LuaJIT) and really private, but
there's a drawback:
You must declare the locals before they are used, and in some cases
you end up with the declaration and the definition at difference
places. A solution is to declare all private names at the top of the
implementation.

Note that if you want to keep the private attributes really private,
you can also define them as locals in the constructor, but then you
have to define all the methods that access private fields in the
constructor as well (so that they close over the constructor scope),
and the object takes far more memory since you create a slew of
functions with each object. That said, in this case, you don't usually
use a lot of database handles, and the memory overhead is not
important.

Another thing:
  res, err = self._dbpath(new_db, path, ext)
would be better written as
  res, err = new_db:_dbpath(path, ext)

If you refactor your code to use locals, it doesn't matter, off
course, but it is a good practice in general. You save one variable
lookup by using the column notation, and it makes things more explicit
(it looks like a method call).

Kind regards,
-- Pierre-Yves