This style guide contains a list of guidelines that we try to follow for our
projects. It does not attempt to make arguments for the styles; its goal is
to provide consistency across projects.
Feel free to fork this style guide and change to your own
liking, and file issues / pull requests if you have questions, comments, or if
you find any mistakes or typos.
Use the constructor syntax for table property creation where possible.
-- badlocal player = {}
player.name='Jack'
player.class='Rogue'-- goodlocal player = {
name ='Jack',
class ='Rogue'
}
Define functions externally to table definition.
-- badlocal player = {
attack =function()
-- ...stuff...end
}
-- goodlocalfunctionattack()
endlocal player = {
attack = attack
}
Consider nil properties when selecting lengths.
A good idea is to store an n property on lists that contain the length
(as noted in Storing Nils in Tables)
-- nils don't countlocal list = {}
list[0] =nil
list[1] ='item'print(#list) -- 0print(select('#', list)) -- 1
When tables have functions, use self when referring to itself.
-- badlocal me = {
fullname =function(this)
return this.first_name+''+ this.last_nameend
}
-- goodlocal me = {
fullname =function(self)
returnself.first_name+''+self.last_nameend
}
-- badlocal name ="Bob Parr"-- goodlocal name ='Bob Parr'-- badlocal fullName ="Bob "..self.lastName-- goodlocal fullName ='Bob '..self.lastName
Strings longer than 80 characters should be written across multiple lines
using concatenation. This allows you to indent nicely.
-- badlocal errorMessage ='This is a super long error that was thrown because of Batman. When you stop to think about how Batman had anything to do with this, you would get nowhere fast.'-- badlocal errorMessage ='This is a super long error that \was thrown because of Batman. \When you stop to think about \how Batman had anything to do \with this, you would get nowhere \fast.'-- badlocal errorMessage =[[This is a super long error that was thrown because of Batman. When you stop to think about how Batman had anything to do with this, you would get nowhere fast.]]-- goodlocal errorMessage ='This is a super long error that '..'was thrown because of Batman. '..'When you stop to think about '..'how Batman had anything to do '..'with this, you would get nowhere '..'fast.'
Always use local to declare variables. Not doing so will result in
global variables to avoid polluting the global namespace.
-- bad
superPower =SuperPower()
-- goodlocal superPower =SuperPower()
Assign variables at the top of their scope where possible. This makes it
easier to check for existing variables.
-- badlocal bad =function()
test()
print('doing stuff..')
//..other stuff..local name =getName()
if name =='test'thenreturnfalseendreturn name
end-- goodlocalfunctiongood()
local name =getName()
test()
print('doing stuff..')
//..other stuff..if name =='test'thenreturnfalseendreturn name
end
Prefer defaults to else statements where it makes sense. This results in
less complex and safer code at the expense of variable reassignment, so
situations may differ.
--badlocalfunctionfull_name(first, last)
local name
if first and last then
name = first ..''.. last
else
name ='John Smith'endreturn name
end--goodlocalfunctionfull_name(first, last)
local name ='John Smith'if first and last then
name = first ..''.. last
endreturn name
end
Short ternaries are okay.
localfunctiondefault_name(name)
-- return the default 'Waldo' if name is nilreturn name or'Waldo'endlocalfunctionbrew_coffee(machine)
return machine and machine.is_loadedand'coffee brewing'or'fill your water'end
Single line blocks are okay for small statements. Try to keep lines to 80 characters.
Indent lines if they overflow past the limit.
-- goodif test thenreturnfalseend-- goodif test thenreturnfalseend-- badif test <1anddo_complicated_function(test) ==falseor seven ==8and nine ==10thendo_other_complicated_function()end-- goodif test <1anddo_complicated_function(test) ==falseor
seven ==8and nine ==10thendo_other_complicated_function()
returnfalseend
The module should not use the global namespace for anything ever. The
module should be a closure.
The file should be named like the module.
-- thing.lualocal thing = { }
local meta = {
__call =function(self, key, vars)
print key
end
}
returnsetmetatable(thing, meta)
Note that modules are loaded as singletons
and therefore should usually be factories (a function returning a new instance of a table)
unless static (like utility libraries.)
请发表评论