Skip to content

Code Styling

This page contains the list of conventions that should be used when writing Lua code for the Delta framework.
If you are looking for the home page, please click here.


Why styling matters

Consistency is an important aspect of writing code. It is a tool to make your programming stand out, but most importantly, it avoids headaches when moving from different scripts and working with more people.

In an environment where you've got people working with you on the same project, it is important that everyone can understand each other clearly.
By following the same set of rules, adjusting to code you didn't originally write becomes far more simple, allowing you to fix bugs and extend features efficiently.


Readability through spaces

Whitespaces are a really direct and easy way of making your code more readable.

There are a couple of rules regarding spaces, although most are very straight forward.

Spaces for numerical operations

-- BAD:
print(2*2/(3+1))

-- GOOD:
print(2 * 2 / (3 + 1))

Spaces for function calls

-- BAD:
print("Hello","World!")

-- GOOD:
print("Hello", "World!")

Spaces for tables

-- BAD:
local a = {}
local b = {1,3,2,4}

-- GOOD:
local a = { }
local b = { 1, 3, 2, 4 }

Spaces for iterators

-- BAD:
for i,v in next,{1, 2, 3} do

end

-- GOOD:
for i, v in next, {1, 2, 3} do

end

Spaces for comparisons and definitions

-- BAD:
local x=1

if x<=3 then print'a' end

-- GOOD:
local x = 1

if x <= 3 then print'a' end

Readability through lines

Lines are equally important in ensuring a script is readable. One of the main factors that dictates whether a script is clean compared to another is if it has a proper use of lines.

-- BAD:
for i = 1, 5 do

    print(i)

    if i > 3 then

        print("bigger than 3")
    else
        print("lower than 3")

    end
end

if true then
    print("is true")

end

-- GOOD:
for i = 1, 5 do
    print(i)

    if i > 3 then
        print("bigger than 3")
    else
        print("lower than 3")
    end
end

if true then
    print("is true")
end

The right iterator for the right table

Although this serves as a micro-optimization, it is a good practice to keep in mind.

That being said, you should use ipairs for non-dictionary tables and pairs for dictionaries.

An example of a non-dictionary in Roblox would be :GetChildren().

local Dictionary = {
    ["Test"] = 1,
    ["Test2"] = 2
} -- Notice that this table assigns values to keys

-- BAD:
for i, v in pairs({ 1, 2 }) do print(i) end
for i, v in next, Dictionary do print(i, v) end

-- GOOD:
for i, v in ipairs({ 1, 2 }) do print(i) end
for i, v in pairs(Dictionary) do print(i, v) end


Avoid bool checks

In most comparisons, you should be able to avoid a direct bool check.

Lua contains comparison optimization, meaning that once a check is valid in an or, or a check invalid in an and, the check ends.

Additionally, any object that is not false or nil will return true regardless of value.

local Value = 2 + 2
local Value2 = true

-- BAD:
if not ((1 + 3) == 2) then print("True") end
if (Value == 1) == false then print("Hm") end
if (Value == 4) == true then print("Yes") end
if Value2 == false then print("Odd") end

-- GOOD:
if (1 + 3) ~= 2 then print("True") end
if Value ~= 1 then print("Hm") end
if Value == 4 then print("Yes") end
if not Value2 then print("Odd") end


Optimize when possible

Once you've completed writing your code, you should ensure it is as optimized as possible.
There are many optimization techniques that can be applied depending on the code.
A good first check to make is to look for repetitive code that could be shortened into less lines.

-- BAD:
for i = 0, 4 do
    if (i + 1) < 2 then
        print("Below 2")
    end

    if (i + 1) < 3 then
        print("Below 3")
    end

    if (i + 1) < 4 then
        print("Below 4")
    end

    if (i + 1) >= 5 then
        print("At 5 or above")
    end

    if i == 4 then
        print("It is 4!")
    else
        print("It is not 4.")
    end
end

-- GOOD:
local Sum = nil

for i = 0, 4 do
    Sum = i + 1

    if Sum < 2 then
        print("Below 2")
    elseif Sum < 3 then
        print("Below 3")
    elseif Sum < 4 then
        print("Below 4")
    elseif Sum >= 5 then
        print("At 5 or above")
    end

    print((i == 4) and "It is 4!" or "It is not 4.") -- http://lua-users.org/wiki/TernaryOperator
end


Conditions inside the while loop

Lua contains a micro-optimization where the conditional check of a while loop is skipped if the specified value is true. As such, avoid polling (e.g. wait) in the condition.

-- BAD:
while wait(1) do

end

-- GOOD:
while true do
    local dt = wait(1) -- dt = delta time
end


Naming convention

MetaMethod uses PascalCase. For any declaration with three (3) or more characters, you should use PascalCase. Otherwise, you should use lowerCamelCase.

-- BAD:
local A = 1
local test = 2
local meta_method = 3

-- GOOD:
local a = 1
local Test = 2
local MetaMethod = 3


Code segmenting

Your code should be segmented in a way where the first line is a comment of your username, followed by Delta and any necessary libraries and services, and then variables.
You should also preferably use Delta's service declarations directly and use workspace instead of game:GetService("Workspace").

-- BAD:
local Delta = require(game:GetService("ReplicatedStorage"):WaitForChild("Delta"):WaitForChild("DeltaFramework"))

local Workspace = Delta.Workspace

for i, v in ipairs(Workspace:GetChildren()) do
    print(v.Name)
end

-- GOOD:
-- MetatableIndex

local Delta = require(game:GetService("ReplicatedStorage"):WaitForChild("Delta"):WaitForChild("DeltaFramework"))

for i, v in ipairs(workspace:GetChildren()) do
    print(v.Name)
end


Ensure all tabs are present

All empty lines should have tabs keeping up to the point where the surrounding lines begin at.

-- BAD:
if true then
    >print("Hello")

    >print("World!")
end

-- GOOD:
if true then
    >print("Hello")
    >
    >print("World!")
end

Back to top