Coding Standards for Lua Source Files and Lua World Scripts

From Serious Sam Wiki
< Lua
Jump to: navigation, search


Variable naming conventions

Variables holding functions

Names of variables which hold function values should always start with a capital letter. If there are multiple words in variable name, each word should start with a capital letter. For example:

function Func()
  ...
end

function LongNameFunction()
  ...
end

Exceptions: Functions (and not only functions) from Lua standard library are all lowercase (for example print, ipairs, pairs, etc.)

Variables holding strings, numbers, booleans, tables, ... (everything but functions in short)

Names of variables which hold anything else except a function should start with a lowercase letter. If there are multiple words in a variable name, each word (except for the first word) should start with a capital letter. For example:

n = 10
number = 20
name = "Foo"

numberOfPuppets = 30

puppetArray = {"Puppet1", "Puppet2"}

Source code indentation

Blocks of Lua code must be properly indented: each block must be indented by exactly one level from its parent block. Block belonging to the main chunk should not be indented. Indentation should be two characters wide. If you are using some other editor (not script editor built in Serious Editor), you should set the tab size to two and set that tabs are expanded into spaces.

Examples:

-- code in main chunk
print("main")
table.insert(myArray, 10)

function Foo(a)
  local z = a
  if z > 10 then
    local b = z
    print("z > 10")
    if b > 0 then
      print("b > 0")
      print("returning...")
      return b
    end
  elseif z = 0 then
    print("z = 0")
    return a
  end
end