-
Notifications
You must be signed in to change notification settings - Fork 73
Coding Standard (English)
MrZ_26 edited this page Oct 1, 2022
·
3 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- Text that might be displayed to players (or debug information for devs): use "double quote"
- Strings that only exist in code and never displayed: use 'single quote'
-
[[double square brackets]](leave out the=whenever possible) for large chunks of text
- After comma
- Around an operator
- Before an opening parenthesis
- After an ending parenthesis
(Translator's note: here are some examples so that it's more obvious what we are talking about.)
- someFunc(a, b, c)
+ someFunc(a,b,c)
- x = 1 / (2 + 3)
+ x=1/(2+3)
- if someFunc() then
+ if someFunc()then- 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.DO{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.
-
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