Coding style - leeoo/koreader-base GitHub Wiki
- Booleans
It can be helpful to prefix Boolean values or functions used as predicates with is, such as is_directory rather than directory
- Function naming
camelCap
-
Variable naming
- All variables should use underscores: offset_x
- global variables should be prefixed with
G_: G_width
-
Constants ALL_CAPS
-
Module/package naming
Module names are often nouns with names that are short and lowercase, with nothing between words.
- i, k, v, and t are often used as follows:
for k,v in pairs(t) ... end
for i,v in ipairs(t) ... end
mt.__newindex = function(t, k, v) ... end
- Class names
Class names (or at least metatables representing classes) may be mixed case (BankAccount), or they might not be. If so, acronyms (e.g. XML) might only uppercase the first letter (XmlDocument).
Some other tips might be useful:
-
Use locals rather than globals whenever possible.
-
End terminator
Because "end" is a terminator for many different constructs, it can help the reader (especially in a large block) if a comment is used to clarify which construct is being terminated:
for i,v in ipairs(t) do
if type(v) == "string" then
...lots of code here...
end -- if string
end -- for each t
- Check empty table
Determine if a table t is empty (including non-integer keys, which #t ignores):
if next(t) == nil then ...
Reference: http://lua-users.org/wiki/LuaStyleGuide