Skip to content

Commit 9918cb3

Browse files
Fix feedback issues (#3614)
1 parent d21a9a7 commit 9918cb3

File tree

7 files changed

+106
-86
lines changed

7 files changed

+106
-86
lines changed

doc/concepts/coop_multitasking.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ transfers control of the thread from the current fiber to another fiber that is
5454
Any live fiber can be in one of three states: ``running``, ``suspended``, and
5555
``ready``. After a fiber dies, the ``dead`` status is returned. By observing
5656
fibers from the outside, you can only see ``running`` (for the current fiber)
57-
and ``suspended`` for any other fiber waiting for an event from eventloop (``ev``)
57+
and ``suspended`` for any other fiber waiting for an event from the event loop (``ev``)
5858
for execution.
5959

6060
.. image:: yields.svg

doc/dev_guide/lua_style_guide.rst

Lines changed: 83 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -65,16 +65,19 @@ Indentation and formatting
6565

6666
.. code-block:: lua
6767
68+
-- Good
6869
if (a == true and b == false) or (a == false and b == true) then
6970
<...>
70-
end -- good
71+
end
7172
73+
-- Bad
7274
if a == true and b == false or a == false and b == true then
7375
<...>
74-
end -- bad
76+
end
7577
78+
-- Good but not explicit
7679
if a ^ b == true then
77-
end -- good, but not explicit
80+
end
7881
7982
* Type conversion
8083

@@ -83,31 +86,32 @@ Indentation and formatting
8386

8487
.. code-block:: lua
8588
89+
-- Bad
8690
local a = 123
8791
a = a .. ''
88-
-- bad
8992
93+
-- Good
9094
local a = 123
9195
a = tostring(a)
92-
-- good
9396
97+
-- Bad
9498
local a = '123'
9599
a = a + 5 -- 128
96-
-- bad
97100
101+
-- Good
98102
local a = '123'
99103
a = tonumber(a) + 5 -- 128
100-
-- good
101104
102105
* Try to avoid multiple nested ``if``'s with common body:
103106

104107
.. code-block:: lua
105108
109+
-- Good
106110
if (a == true and b == false) or (a == false and b == true) then
107111
do_something()
108112
end
109-
-- good
110113
114+
-- Bad
111115
if a == true then
112116
if b == false then
113117
do_something()
@@ -117,56 +121,59 @@ Indentation and formatting
117121
do_something()
118122
end
119123
end
120-
-- bad
121124
122125
* Avoid multiple concatenations in one statement, use ``string.format`` instead:
123126

124127
.. code-block:: lua
125128
129+
-- Bad
126130
function say_greeting(period, name)
127131
local a = "good " .. period .. ", " .. name
128132
end
129-
-- bad
130133
134+
-- Good
131135
function say_greeting(period, name)
132136
local a = string.format("good %s, %s", period, name)
133137
end
134-
-- good
135138
139+
-- Best
136140
local say_greeting_fmt = "good %s, %s"
137141
function say_greeting(period, name)
138142
local a = say_greeting_fmt:format(period, name)
139143
end
140-
-- best
141144
142145
* Use ``and``/``or`` for default variable values
143146

144147
.. code-block:: lua
145148
149+
-- Good
146150
function(input)
147151
input = input or 'default_value'
148-
end -- good
152+
end
149153
154+
-- Ok but excessive
150155
function(input)
151156
if input == nil then
152157
input = 'default_value'
153158
end
154-
end -- ok, but excessive
159+
end
155160
156161
* ``if``'s and return statements:
157162

158163
.. code-block:: lua
159164
165+
-- Good
160166
if a == true then
161167
return do_something()
162168
end
163-
do_other_thing() -- good
169+
do_other_thing()
164170
171+
-- Bad
165172
if a == true then
166173
return do_something()
167174
else
168175
do_other_thing()
169-
end -- bad
176+
end
170177
171178
* Using spaces:
172179

@@ -175,69 +182,71 @@ Indentation and formatting
175182

176183
.. code-block:: lua
177184
185+
-- Bad
178186
function name (arg1,arg2,...)
179-
end -- bad
187+
end
180188
189+
-- Good
181190
function name(arg1, arg2, ...)
182-
end -- good
191+
end
183192
184193
- Add a space after comment markers:
185194

186195
.. code-block:: lua
187196
188-
while true do -- inline comment
189-
-- comment
190-
do_something()
197+
while true do -- Inline comment
198+
-- Comment
199+
do_something()
191200
end
192201
--[[
193-
multiline
202+
Multiline
194203
comment
195204
]]--
196205
197206
- Surrounding operators:
198207

199208
.. code-block:: lua
200209
210+
-- Bad
201211
local thing=1
202212
thing = thing-1
203213
thing = thing*1
204214
thing = 'string'..'s'
205-
-- bad
206215
216+
-- Good
207217
local thing = 1
208218
thing = thing - 1
209219
thing = thing * 1
210220
thing = 'string' .. 's'
211-
-- good
212221
213222
- Add a space after commas in tables:
214223

215224
.. code-block:: lua
216225
226+
-- Bad
217227
local thing = {1,2,3}
218228
thing = {1 , 2 , 3}
219229
thing = {1 ,2 ,3}
220-
-- bad
221230
231+
-- Good
222232
local thing = {1, 2, 3}
223-
-- good
224233
225234
- Add a space in map definitions after equals signs and commas:
226235

227236
.. code-block:: lua
228237
238+
-- Bad
229239
return {1,2,3,4}
230240
return {
231241
key1 = val1,key2=val2
232242
}
233-
-- bad
234243
244+
-- Good
235245
return {1, 2, 3, 4}
236246
return {
237247
key1 = val1, key2 = val2,
238-
key3 = vallll
248+
key3 = val3
239249
}
240-
-- good
241250
242251
You can also use alignment:
243252

@@ -258,25 +267,25 @@ Indentation and formatting
258267

259268
.. code-block:: lua
260269
270+
-- Bad
261271
if thing ~= nil then
262-
-- ...stuff...
272+
-- ... stuff ...
263273
end
264274
function derp()
265-
-- ...stuff...
275+
-- ... stuff ...
266276
end
267277
local wat = 7
268-
-- bad
269278
279+
-- Good
270280
if thing ~= nil then
271-
-- ...stuff...
281+
-- ... stuff ...
272282
end
273283
274284
function derp()
275-
-- ...stuff...
285+
-- ... stuff ...
276286
end
277287
278288
local wat = 7
279-
-- good
280289
281290
- Delete whitespace at EOL (strongly forbidden. Use ``:s/\s\+$//gc`` in vim
282291
to delete them).
@@ -290,14 +299,18 @@ add a prefix, or add a table instead of a prefix:
290299

291300
.. code-block:: lua
292301
302+
-- Very bad
293303
function bad_global_example()
294-
end -- very, very bad
304+
end
295305
296306
function good_local_example()
297307
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
299312
_G.modulename = {}
300-
_G.modulename.good_local_example = good_local_example -- better
313+
_G.modulename.good_local_example = good_local_example
301314
302315
Always use a prefix to avoid name conflicts.
303316

@@ -369,11 +382,11 @@ To write modules, use one of the two patterns (don't use ``modules()``):
369382
local M = {}
370383
371384
function M.foo()
372-
...
385+
...
373386
end
374387
375388
function M.bar()
376-
...
389+
...
377390
end
378391
379392
return M
@@ -383,16 +396,16 @@ or
383396
.. code-block:: lua
384397
385398
local function foo()
386-
...
399+
...
387400
end
388401
389402
local function bar()
390-
...
403+
...
391404
end
392405
393406
return {
394-
foo = foo,
395-
bar = bar,
407+
foo = foo,
408+
bar = bar,
396409
}
397410
398411
Commenting
@@ -401,17 +414,19 @@ Commenting
401414
Don't forget to comment your Lua code. You shouldn't comment Lua syntax (assume that the reader already
402415
knows the Lua language). Instead, tell about functions/variable names/etc.
403416

417+
Start a sentence with a capital letter and end with a period.
418+
404419
Multiline comments: use matching (``--[[ ]]--``) instead of simple
405420
(``--[[ ]]``).
406421

407422
Public function comments:
408423

409424
.. code-block:: lua
410425
411-
--- Copy any table (shallow and deep version)
426+
--- Copy any table (shallow and deep version).
412427
-- * deepcopy: copies all levels
413428
-- * 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.
415430
-- @function gsplit
416431
-- @table inp original table
417432
-- @shallow[opt] sep flag for shallow copy
@@ -429,43 +444,44 @@ Use the ``tap`` module for writing efficient tests. Example of a test file:
429444
local test = require('tap').test('table')
430445
test:plan(31)
431446
432-
do -- check basic table.copy (deepcopy)
447+
do
448+
-- Check basic table.copy (deepcopy).
433449
local example_table = {
434-
{1, 2, 3},
435-
{"help, I'm very nested", {{{ }}} }
450+
{ 1, 2, 3 },
451+
{ "help, I'm very nested", { { { } } } }
436452
}
437453
438454
local copy_table = table.copy(example_table)
439455
440456
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"
444460
)
445461
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"
449465
)
450466
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"
454470
)
455471
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"
459475
)
460476
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"
464480
)
465481
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"
469485
)
470486
end
471487

0 commit comments

Comments
 (0)