Coding Style
Overview
Coding style refers to guidelines designed to ensure consistency and readability of code. These are recommended conventions to reduce ambiguity that may arise when writing code and to improve collaboration and maintenance.
Lua script has a flexible and concise grammar structure compared to other languages, so if developers do not establish a unified convention, the expression style of the code can become excessively diverse. Therefore, by using a consistent coding style, you can quickly understand the meaning of the code, such as the scope or purpose of variables, and increase development efficiency.
Naming Conventions Based on Declaration Scope
The following rules are used to distinguish between file-scoped variables, which are declared at the top of the script, and local variables, which are declared within a specific scope, such as a function or control statement.
Variables
File-scoped variables declared at the top of the script should be named with the first letter of each word in uppercase.
-- Good
local Number = 1
GlobalValue = 2
-- Bad
local number = 1
globalValue = 2
Variables declared within specific scopes such as functions or control statements should be named with only the first letter in lowercase.
-- Good
local function SomeFunction()
local value = 3
if value >= 3 then
local someBoolean = false
end
end
-- Bad
local function SomeFunction()
local Value = 3
if Value >= 3 then
local SomeBoolean = false
end
end
Functions
Function names should be capitalized with the first letter of each word in uppercase.
-- Good
local function SomeFunction()
print("Do Someting")
end
function SomeFunction()
print("Do Someting")
end
-- Bad
local function someFunction()
print("Do Someting")
end
function someFunction()
print("Do Someting")
end
Function Parameters and Return Values
Function parameters and return values must be named with the first letter in lowercase, and spaces should be placed between parameters.
-- Good
local function Sum(numValue1, numValue2)
local result = numValue1 + numValue2
local isSuccess = (result ~= nil)
return isSuccess, result
end
-- Bad
local function Sum(NumValue1,NumValue2)
local result = NumValue1 + NumValue2
local isSuccess = (result ~= nil)
return isSuccess,result
end
Operators
Add a space between operators.
-- Good
local Sum = 1 + 5
local IsPositiveNumber = Sum > 0
if SomeValue == 1 && SomeValue == 2 then
print("Valid Value")
elseif SomeValue == 3 then
print("Value Exceeded)
else
print("Invalid Value"")
end
-- Bad
local Sum=1+5
local IsPositiveNumber=Sum>0
if SomeValue==1&&SomeValue==2 then
print("Valid Value")
elseif SomeValue==3 then
print("Value Exceeded)
else
print("Invalid Value"")
end
Indentation and Line Breaks
Use indentation to clarify the hierarchy of the code, such as the scope and flow.
-- Good
if someCondition1 then
if someCondition2 then
print("Correct!")
end
end
-- Bad
if someCondition1 then
if someCondition2 then
print("Correct!")
end
end
Include a line break at the beginning of a control statement, such as a table, conditional, or loop.
-- Good
local NumberList =
{
1, 2, 3
}
for i = 1, 5 do
print(i)
end
-- Bad
local NumberList = {
1, 2, 3
}
for i = 1, 5 do print(i)
end
In a Team Project
In a collaborative project, it is important to unify the coding style among team members. A consistent coding style improves code readability, makes maintenance easier, and helps smooth collaboration among team members. Therefore, it is advisable to define coding rules in the early stages of the project and ensure that everyone follows them. Agree upon variable names, function names, indentation styles, etc., and proceed with the work based on these guidelines!
Last updated