-
Notifications
You must be signed in to change notification settings - Fork 16
Coding Standard (English)
MrZ_26 edited this page Apr 6, 2024
·
4 revisions
Note: certain code, like code in applets and external libraries, do not need to follow this guideline.
- Use 4 spaces.
- At the end of file, there should be an empty line (i.e. file ends with a line break character).
| Variable type | Naming style |
|---|---|
| Library | ALLCAPS |
| Library functions | smallCamelCase |
| Local functions | _underscoreAndSmallCamelCase |
| Global variables | ALLCAPS |
| Global functions | smallCamelCase |
| Local variables that need readable names | smallCamelCase |
| Simple local variables | initial letters of multiple words, or one single uppercase letter |
f() -- No semicolon needed at the end of linesif c1 then
s1
elseif c2 then
s2
else
s3
endwhile c do
s
endfor i=1,#l do
s
endfor k,v in next,table do -- preferably replace `pairs(table)` with `next,table`
s
endrepeat
s
until cdo
s
endsingleLineOfCode()-- comment 1-- comment 2
Function definition or code block--[[ Better begin with a short description so that can preview when folded
Prefer leaving out the = sign whenever possible
Comment content
]]
Function definition or code block- "double quote": Text that might be read by players or developers
- 'single quote': Strings that only exist in code and never displayed
-
[[double square brackets]](leave out the=whenever possible): Large chunks of text
This section does not apply to:
- Imported Lua libraries and may retain the original author's code style
- "Syntax Comments" lines starting with
---@xxx - Data code with large blocks aligned with spaces
- Do not add space around comma and (non-word) operator
- func(a, b, c)
+ func(a,b,c)
- x = 1 / (2 + 3)
+ x=1/(2+3)Except:
- After "--"
- After "function" of nameless function
-- Comment
f=function() end- Merge multiple lines if it makes code looks more clear
a=5; func(a)
b=10; func(b)- If a function only takes one parameter
- and the parameter is a
tableor astring(TL: a table/string literal. Not when it's a table/string-type variable) - then omit the parenthesis. For example,
GC.execute{100,40
{'print',"Hello",0,0},
{'print',"World",0,20},
}Use regex replace, replace \s([,+\-*/=><"']|==|>=|<=|~=|\.\.)|([,+\-*/=><"']|==|>=|<=|~=|\.\.)\s with $1$2
- NOTE: This WILL affect indentation. You can convert the indentation to tab, replace, then convert back.
- NOTE: This WILL trim off leading and trailing spaces in a string literal. You can remove
'and"in the expression - NOTE: This WILL trim off leading and trailing spaces around
--. You can remove-in the expression
All GOTO tags need to use one of the following prefixes
-
BREAK_: similar tobreakin other languages, break out of loops (can break out any number of layers; may skip code in the rest of the loop) -
CONTINUE_: similar tocontinuein other languages, skip to end of loop to execute the next loop -
THROW_: similar tocatchorexceptin other languages, for catching errors or jumping out of a code block for some actions -
REPEAT_: for returning to a point before to redo something