-
Notifications
You must be signed in to change notification settings - Fork 0
StyleGuide
- Don't use global namespaces without reasons.
- Don't make the function that have too many features.
- Use the editor that can use LDoc.
local function indent_example(query)
for key, value in pairs(tbl) do
if key == query then
return { key = key, value = value}
end
end
end
Indent with 4 spaces. Applies automatically by editorconfig.
local local_variable_name = 1
Use lower snake case.
GlobalVariableName = 2
Don't use global namespaces without reasons. Use upper camel case.
local LOCAL_CONSTANT_NAME = 3
GLOBAL_CONSTANT = 3
Use upper snake case. And use Enum if constants isn't isolated.
--- @enum EnumName
ENUM_NAMES = {
FIRST_ENUM_FIELD = 1,
SECOND_ENUM_FIELD = 2,
THIRD_ENUM_FIELD = 3,
}
Please write ldoc. Use upper camel case for LDoc enum name.
Use upper snake case for field name and enum name.
--- @param name string
--- @return string new_name
local function local_function(name)
print(string.format("Hi %s!", name))
return string.format("local %s", name)
end
Use lower snake case for function name.
--- @param name string
--- @return string new_name
function GlobalFunction(name)
print(string.format("Hi %s!", name))
return string.format("global %s", name)
end
Use upper camel case for function name.
--- @module luaclass
local luaclass = {}
--- @class LuaClass
--- @field name string
local LuaClass = {
name = "lua-class",
}
--- @param name string
--- @return LuaClass
function luaclass.new(name) then
local self = {name = name}
setmetatable(self, {__index = LuaClass})
return self
end
return luaclass
Use upper camel case. If you define a class that isn't in a module, write it like this:
function LuaClass.new(name) then
--- @module luaclass
local lualuaclass = {}
--- @class LuaLuaClass: LuaClass
--- @field bio string
local LuaLuaClass = {
bio = "Hello world!",
}
--- @param name string
--- @param bio string
--- @return LuaLuaClass
function lualuaclass.new(name, bio)
--- @type any
local self = LuaClass.new(name)
self.bio = bio
setmetatable(self, {__index = LuaLuaClass})
return self
end
return lualuaclass
The only one class is defined per file. The same applies to inheritance.
--- @param name string
function LuaClass:updateName(name)
self.name = name
end
Use :
Use lower snake case.
--- @param name string
function LuaClass.updateName(name)
self.name = name
end
Use .
Use lower snake case for static method name.
--- @module "human"
local human = {}
return human
Use ** Module name and file name must be the same.
local human = require("human")
local dog = require("animal.dog")
Use the require function. Don't use dofile (because it uses global namespace).
local hello = "Hello!"
local hi = string.format("Hi \"%s!\"", name)
USE DOUBLE QUOTES and DON'T USE SINGLE QUOTES.
If you wanna use double quotes in a string, use escaping.