lua-users home
lua-l archive

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


Am 10.06.2016 um 22:38 schröbte Hisham:

My reasoning for the latter is that using inconsistent, `local x =
require "y"` types of names is counterproductive: especially when
looking at someone else's code, having to keep in mind how a module
author decided to call a well-known library is an avoidable extra
cognitive load, and having to skim back and forth to look at the
require() list is annoying.

I disagree with that one. One main benefit of "the new way" of using modules is that you can have short local aliases for long module names. We need long module names because those names are global, so you have to be careful to avoid naming conflicts. I often use `local L = require("lpeg")` *or* `local L = require("srfi1")` (a module for linked lists). Lpeg code often has multiple module function calls in the same expression, so a short namespace variable keeps things readable. I don't yet know what I will do when I need both of them in the same file ... Often I will re-use the function name prefix of the C functions as Lua namespace, like `fl_run()` (C code) -> `fl.run()` (Lua code).
Also, `local 30log = require("30log")` won't work anyway.


For code I write, I also try to have the project/rockspec name match
the name of the main module, using lowercase for module names and
snake_case for methods (I use TitleCase only when doing OOP).

Consistency is important: I see people complain about "luafilesystem
vs lfs" all the time (we might add a module index to LuaRocks in the
future so that `luarocks install lfs` does the right thing, but it's a
bit harder than it seems at first glance). I'd say this was an
unfortunate decision when LuaFileSystem was first written (not to
mention that the name of this library is super confusing, since
LuaFileSystem is not, well, a file system — this is not a rhetorical
point, it _did_ leave me puzzled the first time I saw the name, back
in the day).

LuaSocket isn't a socket either. Both are modules that contain functions that cover a certain field -- filesystem functions or socket functions, respectively. I would have expected `require("filesystem")` instead of `require("lfs")` (similar to `require("socket")` or `require("posix")`), though. The "lua" prefix is pretty redundant in a `require` call, but the "lfs" feels like a completely new (and unrelated) name -- especially considering that LuaSocket and lsocket are two separate projects. Btw., I suspect that the module was named "lfs" because in Lua 5.1 you would automatically get a global variable with the same name as the module, and "luafilesystem" (or "filesystem") as a variable name was just a bit too long ...


So, in short, the way I try to do things is to keep it simple:

project Foo, rockspec foo, local foo = require("foo")

I think it's the path of least surprise for users of the project. :)

-- Hisham


Philipp