Unlike a language like JavaScript, Lua does not have a widely accepted styling across implementations. This style guide aims to ensure that styling is consistent across all Project Error Lua files.
Much of this style guide is based off popular JS/TS conventions.
I.1 - Always use spaces
as target indentation
Primarily for code readability on GitHub as tabs are displayed as 8 spaces by default
I.2 - Use two spaces for indent spacing
Opionated carryover from JS/TS conventions.
E.1 - Non-net cross resource communication should use exports
Why? Although exports are simple wrappers around events, they offer caching.
E.2 - NetEventHandlers on the Server should use RegisterServerEvent
Under the hood this is an alias to
RegisterNetEvent
.RegisterServerEvent
is preferred for easy identification of Server Side handlers. (i.e Searching through a resource)
E.3 - Always use the optional callback argument for RegisterNetEvent
Increased readability and more concise
-- Good
RegisterNetEvent('netEvent', handlerFunc)
-- Bad
RegisterNetEvent('netEvent')
AddEventHandler('netEvent', handlerFunc)
C.1 - Constants should use upper case snakecase
-- Good
local INTERVAL_TIME = 50
-- Bad
local intervalTime = 50
C.2 - Local variables or functions should use camelCase
local myVar = {}
local function myLocalFunction() end
C.3 - Global variables or functions should use upper camelcase
MyGlobalVar = 'nice'
function MyGlobalFunction() end
M.1 - Where possible, avoid using the Citizen prefix for the Citizen
table of functions
The most commonly used citizen table functions are aliased to work with removing the prefix. This appears more concise.
-- Bad
Citizen.CreateThread(fn)
-- Good
CreateThread(fn)
List of aliased functions:
SetTimeout
Wait
CreateThread
RconPrint -- Citizen.Trace