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
+63-20Lines changed: 63 additions & 20 deletions
Display the source diff
Display the rich diff
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,75 @@ 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
+
### Consistent naming conventions
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
+
All function names and variables should use the camel case naming convention. Constant variables should use upper snake case (see below).
64
87
65
-
#General principles
88
+
### Use of constants and avoiding [magic numbers](https://en.wikipedia.org/wiki/Magic_number_(programming))
66
89
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.
90
+
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.
74
91
75
-
# Script security
92
+
*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.*
76
93
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.
79
94
80
-
#Error handling
95
+
### Indentation and formatting
81
96
82
-
TODO
97
+
Ensure your code editor (e.g. [Visual Studio Code](https://code.visualstudio.com/)
98
+
applies the rules established by the project's **.editorconfig** file.
83
99
84
100
# Performance considerations
85
101
86
102
- Avoid unnecessary computations within loops.
87
103
- Cache results of expensive operations whenever possible.
88
104
- Use local variables to improve performance.
89
105
106
+
### Don't use OOP functionality
107
+
108
+
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.
109
+
110
+
### Use range-based for loops rather than [`ipairs`](https://www.lua.org/manual/5.1/manual.html#pdf-ipairs)
111
+
112
+
Range-based for loops are more performant than loops using `ipairs` and should be used whenever possible. For example:
113
+
114
+
```lua
115
+
-- Rather than this:
116
+
for_, playerinipairs(getElementsByType("player")) do
117
+
iprint(player)
118
+
end
119
+
120
+
-- Do this:
121
+
localplayers=getElementsByType("player")
122
+
fori=1, #playersdo
123
+
iprint(players[player])
124
+
end
125
+
```
126
+
127
+
# Script security
128
+
129
+
Follow the [Script security](https://wiki.multitheftauto.com/wiki/Script_security)
130
+
principles established for MTA:SA scripts to ensure the safety and integrity of your code.
0 commit comments