Oct 17, 2017, 10:07 AM
Following RFC LL-001, this is now the official LL style guide for Lua. Any new code should follow this style.
Modifications may be proposed through a new RFC, which suggests alterations to the prior RFC.
Modifications may be proposed through a new RFC, which suggests alterations to the prior RFC.
Quote:Code Layout
Tabs or Spaces?
Always tabs.
For leading whitespace, always use tabs.
C vs Lua Style
Always use Lua style operators (--[[]]-- vs /**/, ~= vs !=, not vs !), to allow for external lua parsers.
Semicolons.
Use discouraged, except in run-on single-line operations.
if condition then x = 7; y = 2; return x, y end
Maximum Line Length
Maximum Recommended Length: 100 Characters.
Soft Limit (I'll start getting pissed if all your lines are longer than this): 150 Characters.
Hard Limit (No lines longer than): 250 Characters.
Blank Lines
Do not include whitespace on blank lines.
Top-level function and class definitions are seperated by a single blank line.
Use single blank lines within method or functions bodies, where this will improve readability.
Encoding
UTF-8 is prefered.
Operator and Function Whitespace
Avoid extra whitespace in the following situations.
Prior to parentheses, brackets or braces.
print("My Argument") is allowed and encouraged.
print( "My Argument" ) is discouraged.
print( "My Argument") is highly discouraged.
print( "My Argument" ) is just pissing me off.
Immediately prior to a comma.
print(x, y) is allowed and encouraged.
print(x,y) is allowed, but discouraged.
print(x , y) is discouraged.
Exception:
During colour, angle or vector definitions, where all components are 0, no spacing is encouraged.
Angle(0,0,0) is allowed and encouraged.
Angle(0, 0, 0) is allowed.
Always surround the following operators with a single space on each side.
Assignment (=)
Comparison (==, <, >, <=, >=, !=, ~=)
Arithmetic Operators (+, -, *, /)
x = 1
y = 1
z = x + y
steamID = 10
-- Allowed and encouraged.
x = 1
y = 2
z = x + y
steamID = 10
-- Discouraged.
Naming Conventions
Variables, Fields and Methods use either lowerCamelCase or UpperCamelCase (personal preference)
Enum use Uppercase /w Underscores (EMOTE_DRUNK)
Variable names should be short and self-descriptive.
Variable names should not be single letters, except where allowable due to loop or convention (i for loop iterator, x for loop iterator's value, k for key, v for value, x/y for coordinate pairs)
Exmaples
mySuperVariable is discouraged, as it does not describe the purpose of the variable.
storedCmds is encouraged, as it's short and describes the purpose of the variable (stores commands).
TheVariableThatStoresAllTheWordsInTheDictionaryBecauseINeedItForSomeReason is discouraged, as it's long, and thus harder to read and type.
Comments
First word should be capitalised, unless the word is an identifier starting with a lower-case letter.
If a comment is short, the ending full-stop may be omitted.
Comments should either be on the line precending the code, or on the line of the code.
Block Comments
Block comments apply to the following code block.
Use Lua style operators for block comments.
When closing a pre-code block, block comment, use ]]--.
Inline Comments
Inline comments start with a double dash, and a single space.
Function-Doc Comments
Prior to any function, a LuaDoc style function documentation comment needs to be added.
Start with a triple dash (---), with secondary lines starting with a double-dash (--).
The description can be over multiple lines. The description must end with a full-stop.
Header
The header starts with a double dash, double open square bracket, and double dash (--[[--).
The first line contains the opening, a space, and the short description.
Following lines contain the extended description.
Following lines contain the tags.
Final line contains the closing tag, which is a double dash, double close square bracket, double dash (--]]--)
Required Tags
author
copyright
release
Example
--[[-- Clientside code relating to the chatbox.
Does stuff and things about the chatbox, like displaying chat, recieving messages and shit like that.
2015-2017 Limelight Gaming
CityRP 2
Legacy Team
--]]--
Tag Lines.
Tag lines start with a double dash, a space and a @.
author - Defines the author of a module or file.
copyright - Defines the copyright of the mdoule/file.
field - Descripts a table field definition.
param - Takes two arguuments, the name of a function param and the description.
release - String describing the release of the module or file.
return - Describes a function return value.
see - Refer to other function or table.
useage - Describe the use of the function or variable.
Example
--- The description of the function
-- which can be spread over multiple lines.
--p1 The first argument
--p2 The second arguments.
--A string value
--A secondary string value
--table.concat
function testFunc(p1, p2)
return "Hi", "Internet"
end
Strings
Prefer double-quoted strings over single-quoted strings.