Skip to content

Tables

Use object access for constant keys, array access for non-constant

    local company = {
        boss = "Sam"
    }
BAD
    local boss = company["boss"]
GOOD
    local boss = company.boss

When using table values multiple times, store it in a local variable

This increases readability, but also increases performance as you are not accessing the table constantly.

BAD
    local concatenation = myTable["key"] .. myTable["key"]
GOOD
    local myTableValue = myTable["key"]
    local concatenation = myTableValue .. myTableValue

table.insert() sucks

It is not efficient/performant, you should only use this when you need to insert at a specific index.

Inserting at the end of a table

BAD
    table.insert(myTable, "value")
GOOD
    myTable[#myTable + 1] = "value"

Inserting/Overwriting a given key

BAD
    table.insert(myTable, "key", "value")
GOOD
    myTable["key"] = "value"