-
Notifications
You must be signed in to change notification settings - Fork 11
/
lua-code-style-guide.lua
132 lines (103 loc) · 3.28 KB
/
lua-code-style-guide.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
-- lua code style guide
-- describes a good practices and my personal preferences
local camelCase, forVariables
PascalCaseForLibs = {}
function PascalCaseForClasses:AndMethods(camelCase, forArguments)
-- tabs, tab size 4
end
local doubleQuotes = "instead of single quotes"
local evenWhen = "you need to put \"double quotes\" in string, just escape it!"
local orAtLesat = [[Use "multiline" strings]]
local sequentialArrays = {
"i",
"prefer",
"to",
"not",
"do",
[6] = "this",
"arrays should be defined without indexes"
}
local associativeObjects = {
dont = "use",
["brackets"] = "if possible"
}
local mixedTables = {
"sequential",
"come",
"first",
["then"] = "we",
put = "associative"
}
local stringsConcatenation = "i prefer".. toPut .." spaces only between variables, but dont put it near quotes"
ifArgumentsTooLong(
iPrefer("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."),
"to split it",
"like this"
)
butSingleLongArgument("I prefer to put it in single line. qwertyuiopasdfghjklzxcvbnm1234567890qwertyuiopasdfghjklzxcvbnm1234567890qwertyuiopasdfghjklzxcvbnm1234567890qwertyuiopasdfghjklzxcvbnm1234567890qwertyuiopasdfghjklzxcvbnm1234567890")
-- its good for things like:
PrintTable(
database.user.getAvatar({
userid = 1
})
)
local avoid = "continue", "//", '/* */' -- and any other custom syntax
if dontUse and angleBrackets and ifPossible then end
if (onlyIf or reallyRequired) and dontWasteYourTime and onUseless and monkeyWork then end
if (
splitTooLong and
statements and
likeThis
) then
local something
elseif (
(split and complex and logic) and
(like and this) and
(player:IsGoodPerson() and player:IsUseGoodCodeStylePractics()) and -- statement of 1st object
(lorem:IpsumDolor() and sit:Amet()) -- statement of 2rd object
) then
local something
end
function dontUseElseStatementIfPossible()
if becauseThisReduces then
return thePyramidsDepth
end
return "Yre not an Egyptian right?"
end
function Pyramids()
if makeYour then
if pyramidsDepth then
if noMoreThan3Floors then
end
end
end
if (orBetter and doIt and likeThis) == false then return end
andSplitYourCode("To small functions")
end
while player:GetVelocity():Length() >= player:GetRunSpeed() do
end
-- is better than
repeat
until player:GetVelocity():Length() < player:GetRunSpeed()
-- because while loop is much clean
-- repeat until adds nothing, but the non-obviousness of the code.
function dontUse(gotoStatements)
goto looksUgly
modernProgrammingLangues("have a better features than spaghetti", "goto is rudiment of the ancient programming languages our grandfathers used", "its also unsupported in 5.1 & jit")
::looksUgly::
end
dontUseGlobalsIfPossible = "make globals is a bad practice! make your code modular, include modules with require - this will make your code more structured and clean."
-- require only needed functions
local hmac = require("openssl").hmac.hmac
hmac(...)
-- is better than
local openssl = require("openssl")
openssl.hmac.hmac(...)
local patterns = table.concat({ -- i prefer to write patterns this way, this makes them easy to read & modify.
"<avatarIcon>",
"<%!%[", "CDATA%[",
"(.-)",
"%]%]>",
"</avatarIcon>"
})
looksBetterThan = "<avatarIcon><%!%[CDATA%[(.-)%]%]></avatarIcon>" -- huh?