Skip to content

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.

Indentation

  • Use 4 spaces.

End of file

  • At the end of file, there should be an empty line (i.e. file ends with a line break character).

Naming conventions

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

Common code block formats

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

Comments

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

Strings

  • 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

Code compacting

Reduce spaces: do not add space in these places

  • 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

Reduce parenthesis

  • If a function only takes one parameter
  • and the parameter is a table or a string (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},
}

Regex tool for reducing space

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.

Goto

All GOTO tags need to use one of the following prefixes:

  • BREAK_: similar to break 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 to continue in other languages, skip to end of loop to execute the next loop
  • THROW_: similar to catch or except 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