Skip to content
Hzj_jie edited this page Mar 27, 2017 · 19 revisions

General

  • Indentation - 4 spaces
  • Line Length - 80 characters

Naming

  • Classnames - UpperCamelCase
  • Functions - lowerCamelCase
  • Variables - all_lower_case_with_underscores
  • Constants - ALL_UPPER_CASE_WITH_UNDERSCORES
  • Filenames - shortnouns

exceptions:

  • global variables should be prefixed with G_: G_width
  • boolean variables should be prefixed as predicates: is_directory
  • 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

Order of Requires

Please order the requires in following rules, local Class = require("class") -- Uppercase class local Device = require("device") -- Uppercase class local func = require("func") -- Lowercase function local glob = require("glob") -- Lowercase function local _ = require("gettext") -- Symbol local T = require("ffi/util").template -- One of the functions in a file local V = require("utils2").version -- The other function, ordered by ASCII.

Tips

  • 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

Clone this wiki locally