You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: mtasa-resources/CODING_GUIDELINES.md
+80-20Lines changed: 80 additions & 20 deletions
Original file line number
Diff line number
Diff line change
@@ -18,6 +18,16 @@ Theft Auto (MTA) multiplayer mod**. To ensure high-quality code and a
18
18
smooth collaboration process, please adhere to the following **coding
19
19
guidelines**.
20
20
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
+
21
31
# Code style
22
32
23
33
### Early return
@@ -49,42 +59,92 @@ function exampleFunction(value)
49
59
end
50
60
```
51
61
52
-
### Consistent naming conventions
62
+
### Error handling
53
63
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:
55
65
56
-
### Use of constants
66
+
```lua
67
+
-- This function outputs player's name to the chat box
68
+
functionoutputPlayerName(player)
69
+
-- If player is invalid, raise an error
70
+
ifnot (isElement(player) andgetElementType(player) =="player") then
71
+
error("Invalid player!", 2)
72
+
end
73
+
74
+
outputChatBox(getPlayerName(player))
75
+
end
57
76
58
-
TODO
77
+
-- This function is triggered each time a player joins and calls outputPlayerName
78
+
localfunctionplayerJoin()
79
+
outputPlayerName() -- note the missing player argument; the error will point to this line
80
+
end
81
+
addEventHandler("onPlayerJoin", root, playerJoin)
82
+
```
59
83
60
-
### Indentation and formatting
84
+
### Avoid anonymous functions
61
85
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:
- 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
74
102
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).
76
104
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.*
79
110
80
-
# Error handling
81
111
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.
83
116
84
117
# Performance considerations
85
118
86
119
- Avoid unnecessary computations within loops.
87
120
- Cache results of expensive operations whenever possible.
88
121
- Use local variables to improve performance.
89
122
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_, playerinipairs(getElementsByType("player")) do
134
+
iprint(player)
135
+
end
136
+
137
+
-- Do this:
138
+
localplayers=getElementsByType("player")
139
+
fori=1, #playersdo
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.
0 commit comments