Wiki

Clone wiki

nota / doc / codingStandards

Coding standards


How to understand standards

Here you find examples of good and bad coding mainly and some code maintenance suggestions. This guide is not here to make your life bad. It helps you to code effectively (measured by time).

By this effectivity we do not mean just optimizing time needed to write first version of code, but also, primary, time consumed by code-maintenance. Programmers usually spend 70 % of their time just by reading the code (so just 30 % of their time remains for making new code). That is why we want your code as much as possible readable. It will help anyone who has to read your code in the future. Following the rules now save time in the future.

That's why we have coding rules.

Also, keep in mind Lua is used for scripting (high level programming). We want code you can read as fairly-tale. Even Lua noob have to understand what is given piece of code about.

Commenting

There is nothing like too-much comments in code!

  • comment in same line if comment relates to given one code line
  • comment in extra line if comment relates to longer pice of code (more than 1 line)

Lua performance tips

Our team try to code performance wise - so we respect the performance tips based on testing in spring community


Example pieces of code

Before you get to syntax rules, take a look on basic coding examples in Lua.

Condition

local someCondtion = true  -- i'm creating local variable and save boolean true in it
if (someCondition) then
    Spring.Echo("Condition was true") -- i print this text into log by using Spring.Echo of Spring API
else
    Spring.Echo("Condition was false")
end

This will print out:

Condition was true

Table definition

Lets create table which we use just as array of items

local listOfFriends = {"Thor", "danil_kalina", "Godde", "PoW", "kmar", "PepeAmpere"}

Lets create table called body to show cool multilevel tables construction:

local body = {
    head = {
        face = {"leftEye", "rightEye", "nose", "mouth"},
        size = 5,
    },
    torso = true,
    bottom = {
        penis = false,
        legs = {"leftLeg", "rightLeg"},
    },
}

For cycle

Lets iterate over list friends (we created above) and print their names

for i=1, #listOfFriends do                                 -- table indexes in Lua starts at 1
    local oneFriend = listOfFriends[i]                     -- i read name of one of my friends from array
    Spring.Echo("This is also my friend: " .. oneFriend)   -- i print concatenated string in log
end

This will print out:

This is also my friend: Thor
This is also my friend: danil_kalina
This is also my friend: Godde
This is also my friend: PoW
This is also my friend: kmar
This is also my friend: PepeAmpere

Function

Lets define new function

local function PlusOne(number)
    return number + 1
end

and use it

local someNumber = 5
local newNumber = PlusOne(someNumber)     -- use function
local Spring.Echo(newNumber)              -- print result into log

This will print out:

6

Mandatory syntax rules

[TBD]

  • brackets style
  • spacing
  • naming functions and variables (no shortcuts)
  • module definition
  • for cycle is the only suggested cycle (do not use repeat, while)

Optional syntax rules

[TBD]


[TBD]

  • this page is based on old google-project standard guide
    • get all rules from there here
    • update page there to reference this new page and mark that one that is obsolete
  • remove example pieces of code and put them on special page

Updated