-
Notifications
You must be signed in to change notification settings - Fork 67
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 lines
if c1 then
s1
elseif c2 then
s2
else
s3
end
while c do
s
end
for i=1,#l do
s
end
for k,v in next,table do -- preferably replace `pairs(table)` with `next,table`
s
end
repeat
s
until c
do
s
end
singleLineOfCode()--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
table
or 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 tobreak
in other languages, break out of loops (can break out any number of layers; may skip code in the rest of the loop) -
CONTINUE_
: similar tocontinue
in other languages, skip to end of loop to execute the next loop -
THROW_
: similar tocatch
orexcept
in 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