lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]




On Thu, Aug 9, 2018 at 4:24 AM Frank Kastenholz <fkastenholz@verizon.net> wrote:
Isn’t it good programming practice to always check return values?


Depends on the interface contract. When memory allocation fails in C (e.g. using malloc) you end up with a null pointer, which you check for. In C++ the contract is different, the failure causes an exception (unless you call the non-throwing version of 'new', I know), so you should not test for null, because the contract guarantees that the pointer will never be null (your program will continue in an exception handler if allocation fails).

Lua has two standard conventions for reporting errors: error/lua_error ('throwing' an error), or returning false plus an error string. 'require' only uses and supports the first one, so that's the idiomatic way to report errors. If you construct additional ways to report errors you modify the interface contract of a standard Lua interface. You have to have a really, really, really good reason for doing that, especially if you publish the code, or other people have to maintain it.

It's better to add your own variant of 'require' if you need something non-standard.

--