lua-users home
lua-l archive

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


2018-08-07 8:38 GMT+02:00 Daurnimator <quae@daurnimator.com>:
> On 7 August 2018 at 16:05, Dirk Laurie <dirk.laurie@gmail.com> wrote:
>> Inside that 'if', write a message on io.stderr and return false.
>
> Please don't!
> This will manifest as e.g. a user reporting an error about trying to
> index 'false'.
> When they try and run code such as:
>
> tls = require "org.conman.nfl.tls"
> function dothething()
>    tls.handshake(mysocket)
> end
> -- at some point later in response to user input:
>     dothething()
>
> It's better to fail fast and keep the error message close to the cause!

I don't agree. Returning 'false' is a Lua idiom.

>
> But also, writing to stderr from a library is an antipattern: please
> never do this.
> I might e.g. just be checking to see if your module is available and do:
> pcall(require, "yourmodule")
> I don't want output appearing in the user's terminal when I do that!

I'll concede that this is a plausible reason for not writing to stderr,
although If I were that user, I would not minded the hint about some
feature that the program would have exploited if available.

But this second reason nullifies the first. The purpose of returning
'false' is precisely so that a pcall will catch the error.