Skip to content

Commit 42af517

Browse files
committed
Add resource code guidelines
1 parent 62f98e7 commit 42af517

File tree

1 file changed

+80
-20
lines changed

1 file changed

+80
-20
lines changed

mtasa-resources/CODING_GUIDELINES.md

Lines changed: 80 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,16 @@ Theft Auto (MTA) multiplayer mod**. To ensure high-quality code and a
1818
smooth collaboration process, please adhere to the following **coding
1919
guidelines**.
2020

21+
# General principles
22+
23+
- Write clear, readable, and maintainable code.
24+
- Follow the [DRY](https://en.wikipedia.org/wiki/Don%27t_repeat_yourself)
25+
(Don't Repeat Yourself) principle.
26+
- Adhere to the [KISS](https://en.wikipedia.org/wiki/KISS_principle)
27+
(Keep It Simple, Stupid) principle.
28+
- Use meaningful variable and function names that convey their purpose.
29+
- Comment your code where necessary to explain the logic.
30+
2131
# Code style
2232

2333
### Early return
@@ -49,42 +59,92 @@ function exampleFunction(value)
4959
end
5060
```
5161

52-
### Consistent naming conventions
62+
### Error handling
5363

54-
TODO
64+
Use the Lua [`error (message [, level])`](https://www.lua.org/manual/5.1/manual.html#pdf-error) function to raise warnings when validating parameters passed to a function, with `level` set to `2` so that the error points the error at the function call location. For example:
5565

56-
### Use of constants
66+
```lua
67+
-- This function outputs player's name to the chat box
68+
function outputPlayerName(player)
69+
-- If player is invalid, raise an error
70+
if not (isElement(player) and getElementType(player) == "player") then
71+
error("Invalid player!", 2)
72+
end
73+
74+
outputChatBox(getPlayerName(player))
75+
end
5776

58-
TODO
77+
-- This function is triggered each time a player joins and calls outputPlayerName
78+
local function playerJoin()
79+
outputPlayerName() -- note the missing player argument; the error will point to this line
80+
end
81+
addEventHandler("onPlayerJoin", root, playerJoin)
82+
```
5983

60-
### Indentation and formatting
84+
### Avoid anonymous functions
6185

62-
Ensure your code editor (e.g. [Visual Studio Code](https://code.visualstudio.com/)
63-
applies the rules established by the project's **.editorconfig** file.
86+
Rather than using anonymous functions (i.e when calling [`addEventHandler`](https://wiki.multitheftauto.com/wiki/AddEventHandler)), use a named local function instead. For example:
6487

65-
# General principles
88+
```lua
89+
-- Rather than this:
90+
addEventHandler("onPlayerConnect", root, function()
91+
outputChatBox("Hello!")
92+
end)
93+
94+
-- Do this:
95+
local function onConnect()
96+
outputChatBox("Hello!")
97+
end
98+
addEventHandler("onPlayerConnect", root, onConnect)
99+
```
66100

67-
- Write clear, readable, and maintainable code.
68-
- Follow the [DRY](https://en.wikipedia.org/wiki/Don%27t_repeat_yourself)
69-
(Don't Repeat Yourself) principle.
70-
- Adhere to the [KISS](https://en.wikipedia.org/wiki/KISS_principle)
71-
(Keep It Simple, Stupid) principle.
72-
- Use meaningful variable and function names that convey their purpose.
73-
- Comment your code where necessary to explain the logic.
101+
### Consistent naming conventions
74102

75-
# Script security
103+
All function names and variables should use the camel case naming convention. Constant variables should use upper snake case (see below).
76104

77-
Follow the [Script security](https://wiki.multitheftauto.com/wiki/Script_security)
78-
principles established for MTA:SA scripts to ensure the safety and integrity of your code.
105+
### Use of constants and avoiding [magic numbers](https://en.wikipedia.org/wiki/Magic_number_(programming))
106+
107+
Don't use magic numbers in blocks of code - instead, define such values as "constants" at the top of the file using upper snake case.
108+
109+
*Note: the version of Lua used by MTA does not support the `const` keyword added in Lua 5.4. In MTA Lua, the concept of a `const` is just that - a concept.*
79110

80-
# Error handling
81111

82-
TODO
112+
### Indentation and formatting
113+
114+
Ensure your code editor (e.g. [Visual Studio Code](https://code.visualstudio.com/)
115+
applies the rules established by the project's **.editorconfig** file.
83116

84117
# Performance considerations
85118

86119
- Avoid unnecessary computations within loops.
87120
- Cache results of expensive operations whenever possible.
88121
- Use local variables to improve performance.
89122

123+
### Don't use OOP functionality
124+
125+
Enabling the [OOP](https://wiki.multitheftauto.com/wiki/OOP) functionality in any given resource will incur a performance hit. In general, pull requests to the official resources repository that enable it will generally fail code review.
126+
127+
### Use range-based for loops rather than [`ipairs`](https://www.lua.org/manual/5.1/manual.html#pdf-ipairs)
128+
129+
Range-based for loops are more performant than loops using `ipairs` and should be used whenever possible. For example:
130+
131+
```lua
132+
-- Rather than this:
133+
for _, player in ipairs(getElementsByType("player")) do
134+
iprint(player)
135+
end
136+
137+
-- Do this:
138+
local players = getElementsByType("player")
139+
for i = 1, #players do
140+
iprint(players[player])
141+
end
142+
```
143+
144+
# Script security
145+
146+
Follow the [Script security](https://wiki.multitheftauto.com/wiki/Script_security)
147+
principles established for MTA:SA scripts to ensure the safety and integrity of your code.
148+
149+
90150

0 commit comments

Comments
 (0)