@@ -65,16 +65,19 @@ Indentation and formatting
65
65
66
66
.. code-block :: lua
67
67
68
+ -- Good
68
69
if (a == true and b == false) or (a == false and b == true) then
69
70
<...>
70
- end -- good
71
+ end
71
72
73
+ -- Bad
72
74
if a == true and b == false or a == false and b == true then
73
75
<...>
74
- end -- bad
76
+ end
75
77
78
+ -- Good but not explicit
76
79
if a ^ b == true then
77
- end -- good, but not explicit
80
+ end
78
81
79
82
* Type conversion
80
83
@@ -83,31 +86,32 @@ Indentation and formatting
83
86
84
87
.. code-block :: lua
85
88
89
+ -- Bad
86
90
local a = 123
87
91
a = a .. ''
88
- -- bad
89
92
93
+ -- Good
90
94
local a = 123
91
95
a = tostring(a)
92
- -- good
93
96
97
+ -- Bad
94
98
local a = '123'
95
99
a = a + 5 -- 128
96
- -- bad
97
100
101
+ -- Good
98
102
local a = '123'
99
103
a = tonumber(a) + 5 -- 128
100
- -- good
101
104
102
105
* Try to avoid multiple nested ``if ``'s with common body:
103
106
104
107
.. code-block :: lua
105
108
109
+ -- Good
106
110
if (a == true and b == false) or (a == false and b == true) then
107
111
do_something()
108
112
end
109
- -- good
110
113
114
+ -- Bad
111
115
if a == true then
112
116
if b == false then
113
117
do_something()
@@ -117,56 +121,59 @@ Indentation and formatting
117
121
do_something()
118
122
end
119
123
end
120
- -- bad
121
124
122
125
* Avoid multiple concatenations in one statement, use ``string.format `` instead:
123
126
124
127
.. code-block :: lua
125
128
129
+ -- Bad
126
130
function say_greeting(period, name)
127
131
local a = "good " .. period .. ", " .. name
128
132
end
129
- -- bad
130
133
134
+ -- Good
131
135
function say_greeting(period, name)
132
136
local a = string.format("good %s, %s", period, name)
133
137
end
134
- -- good
135
138
139
+ -- Best
136
140
local say_greeting_fmt = "good %s, %s"
137
141
function say_greeting(period, name)
138
142
local a = say_greeting_fmt:format(period, name)
139
143
end
140
- -- best
141
144
142
145
* Use ``and ``/``or `` for default variable values
143
146
144
147
.. code-block :: lua
145
148
149
+ -- Good
146
150
function(input)
147
151
input = input or 'default_value'
148
- end -- good
152
+ end
149
153
154
+ -- Ok but excessive
150
155
function(input)
151
156
if input == nil then
152
157
input = 'default_value'
153
158
end
154
- end -- ok, but excessive
159
+ end
155
160
156
161
* ``if ``'s and return statements:
157
162
158
163
.. code-block :: lua
159
164
165
+ -- Good
160
166
if a == true then
161
167
return do_something()
162
168
end
163
- do_other_thing() -- good
169
+ do_other_thing()
164
170
171
+ -- Bad
165
172
if a == true then
166
173
return do_something()
167
174
else
168
175
do_other_thing()
169
- end -- bad
176
+ end
170
177
171
178
* Using spaces:
172
179
@@ -175,69 +182,71 @@ Indentation and formatting
175
182
176
183
.. code-block :: lua
177
184
185
+ -- Bad
178
186
function name (arg1,arg2,...)
179
- end -- bad
187
+ end
180
188
189
+ -- Good
181
190
function name(arg1, arg2, ...)
182
- end -- good
191
+ end
183
192
184
193
- Add a space after comment markers:
185
194
186
195
.. code-block :: lua
187
196
188
- while true do -- inline comment
189
- -- comment
190
- do_something()
197
+ while true do -- Inline comment
198
+ -- Comment
199
+ do_something()
191
200
end
192
201
--[[
193
- multiline
202
+ Multiline
194
203
comment
195
204
]]--
196
205
197
206
- Surrounding operators:
198
207
199
208
.. code-block :: lua
200
209
210
+ -- Bad
201
211
local thing=1
202
212
thing = thing-1
203
213
thing = thing*1
204
214
thing = 'string'..'s'
205
- -- bad
206
215
216
+ -- Good
207
217
local thing = 1
208
218
thing = thing - 1
209
219
thing = thing * 1
210
220
thing = 'string' .. 's'
211
- -- good
212
221
213
222
- Add a space after commas in tables:
214
223
215
224
.. code-block :: lua
216
225
226
+ -- Bad
217
227
local thing = {1,2,3}
218
228
thing = {1 , 2 , 3}
219
229
thing = {1 ,2 ,3}
220
- -- bad
221
230
231
+ -- Good
222
232
local thing = {1, 2, 3}
223
- -- good
224
233
225
234
- Add a space in map definitions after equals signs and commas:
226
235
227
236
.. code-block :: lua
228
237
238
+ -- Bad
229
239
return {1,2,3,4}
230
240
return {
231
241
key1 = val1,key2=val2
232
242
}
233
- -- bad
234
243
244
+ -- Good
235
245
return {1, 2, 3, 4}
236
246
return {
237
247
key1 = val1, key2 = val2,
238
- key3 = vallll
248
+ key3 = val3
239
249
}
240
- -- good
241
250
242
251
You can also use alignment:
243
252
@@ -258,25 +267,25 @@ Indentation and formatting
258
267
259
268
.. code-block :: lua
260
269
270
+ -- Bad
261
271
if thing ~= nil then
262
- -- ...stuff...
272
+ -- ... stuff ...
263
273
end
264
274
function derp()
265
- -- ...stuff...
275
+ -- ... stuff ...
266
276
end
267
277
local wat = 7
268
- -- bad
269
278
279
+ -- Good
270
280
if thing ~= nil then
271
- -- ...stuff...
281
+ -- ... stuff ...
272
282
end
273
283
274
284
function derp()
275
- -- ...stuff...
285
+ -- ... stuff ...
276
286
end
277
287
278
288
local wat = 7
279
- -- good
280
289
281
290
- Delete whitespace at EOL (strongly forbidden. Use ``:s/\s\+$//gc `` in vim
282
291
to delete them).
@@ -290,14 +299,18 @@ add a prefix, or add a table instead of a prefix:
290
299
291
300
.. code-block :: lua
292
301
302
+ -- Very bad
293
303
function bad_global_example()
294
- end -- very, very bad
304
+ end
295
305
296
306
function good_local_example()
297
307
end
298
- _G.modulename_good_local_example = good_local_example -- good
308
+ -- Good
309
+ _G.modulename_good_local_example = good_local_example
310
+
311
+ -- Better
299
312
_G.modulename = {}
300
- _G.modulename.good_local_example = good_local_example -- better
313
+ _G.modulename.good_local_example = good_local_example
301
314
302
315
Always use a prefix to avoid name conflicts.
303
316
@@ -369,11 +382,11 @@ To write modules, use one of the two patterns (don't use ``modules()``):
369
382
local M = {}
370
383
371
384
function M.foo()
372
- ...
385
+ ...
373
386
end
374
387
375
388
function M.bar()
376
- ...
389
+ ...
377
390
end
378
391
379
392
return M
383
396
.. code-block :: lua
384
397
385
398
local function foo()
386
- ...
399
+ ...
387
400
end
388
401
389
402
local function bar()
390
- ...
403
+ ...
391
404
end
392
405
393
406
return {
394
- foo = foo,
395
- bar = bar,
407
+ foo = foo,
408
+ bar = bar,
396
409
}
397
410
398
411
Commenting
@@ -401,17 +414,19 @@ Commenting
401
414
Don't forget to comment your Lua code. You shouldn't comment Lua syntax (assume that the reader already
402
415
knows the Lua language). Instead, tell about functions/variable names/etc.
403
416
417
+ Start a sentence with a capital letter and end with a period.
418
+
404
419
Multiline comments: use matching (``--[[ ]]-- ``) instead of simple
405
420
(``--[[ ]] ``).
406
421
407
422
Public function comments:
408
423
409
424
.. code-block :: lua
410
425
411
- --- Copy any table (shallow and deep version)
426
+ --- Copy any table (shallow and deep version).
412
427
-- * deepcopy: copies all levels
413
428
-- * shallowcopy: copies only first level
414
- -- Supports __copy metamethod for copying custom tables with metatables
429
+ -- Supports __copy metamethod for copying custom tables with metatables.
415
430
-- @function gsplit
416
431
-- @table inp original table
417
432
-- @shallow[opt] sep flag for shallow copy
@@ -429,43 +444,44 @@ Use the ``tap`` module for writing efficient tests. Example of a test file:
429
444
local test = require('tap').test('table')
430
445
test:plan(31)
431
446
432
- do -- check basic table.copy (deepcopy)
447
+ do
448
+ -- Check basic table.copy (deepcopy).
433
449
local example_table = {
434
- {1, 2, 3},
435
- {"help, I'm very nested", {{ { }} } }
450
+ { 1, 2, 3 },
451
+ { "help, I'm very nested", { { { } } } }
436
452
}
437
453
438
454
local copy_table = table.copy(example_table)
439
455
440
456
test:is_deeply(
441
- example_table,
442
- copy_table,
443
- "checking, that deepcopy behaves ok"
457
+ example_table,
458
+ copy_table,
459
+ "checking, that deepcopy behaves ok"
444
460
)
445
461
test:isnt(
446
- example_table,
447
- copy_table,
448
- "checking, that tables are different"
462
+ example_table,
463
+ copy_table,
464
+ "checking, that tables are different"
449
465
)
450
466
test:isnt(
451
- example_table[1],
452
- copy_table[1],
453
- "checking, that tables are different"
467
+ example_table[1],
468
+ copy_table[1],
469
+ "checking, that tables are different"
454
470
)
455
471
test:isnt(
456
- example_table[2],
457
- copy_table[2],
458
- "checking, that tables are different"
472
+ example_table[2],
473
+ copy_table[2],
474
+ "checking, that tables are different"
459
475
)
460
476
test:isnt(
461
- example_table[2][2],
462
- copy_table[2][2],
463
- "checking, that tables are different"
477
+ example_table[2][2],
478
+ copy_table[2][2],
479
+ "checking, that tables are different"
464
480
)
465
481
test:isnt(
466
- example_table[2][2][1],
467
- copy_table[2][2][1],
468
- "checking, that tables are different"
482
+ example_table[2][2][1],
483
+ copy_table[2][2][1],
484
+ "checking, that tables are different"
469
485
)
470
486
end
471
487
0 commit comments