Skip to content

Commit 984d056

Browse files
authored
Merge pull request #6 from jlillis/resources-guidelines-proposal
Add additional resource code guidelines
2 parents 494368e + 91200e0 commit 984d056

File tree

1 file changed

+63
-20
lines changed

1 file changed

+63
-20
lines changed

mtasa-resources/CODING_GUIDELINES.md

Lines changed: 63 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,75 @@ 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+
### Consistent naming conventions
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+
All function names and variables should use the camel case naming convention. Constant variables should use upper snake case (see below).
6487

65-
# General principles
88+
### Use of constants and avoiding [magic numbers](https://en.wikipedia.org/wiki/Magic_number_(programming))
6689

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.
7491

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.*
7693

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.
7994

80-
# Error handling
95+
### Indentation and formatting
8196

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.
8399

84100
# Performance considerations
85101

86102
- Avoid unnecessary computations within loops.
87103
- Cache results of expensive operations whenever possible.
88104
- Use local variables to improve performance.
89105

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 _, player in ipairs(getElementsByType("player")) do
117+
iprint(player)
118+
end
119+
120+
-- Do this:
121+
local players = getElementsByType("player")
122+
for i = 1, #players do
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.
131+
132+
90133

0 commit comments

Comments
 (0)