Skip to content

Commit 792c356

Browse files
authored
chore(contributing): prefer error over asserts
1 parent 5886719 commit 792c356

1 file changed

Lines changed: 21 additions & 0 deletions

File tree

CONTRIBUTING.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,27 @@ local function check_name(name)
299299
end
300300
```
301301

302+
Prefer if-then-error over assertions:
303+
304+
```lua
305+
-- bad
306+
local function some_func(name)
307+
-- string concatenation happens on good- and failure-case
308+
assert(type(name) == "string", "expected name to be a string, got " .. type(name))
309+
310+
-- do work
311+
end
312+
313+
-- good
314+
local function some_func(name)
315+
if type(name) ~= "string" then
316+
error("expected name to be a string, got " .. type(name))
317+
end
318+
319+
-- do work
320+
end
321+
```
322+
302323
Follow the return values conventions: Lua supports multiple return values, and
303324
by convention, handles recoverable errors by returning `nil` plus a `string`
304325
describing the error:

0 commit comments

Comments
 (0)