Sujet : Re: C23 thoughts and opinions
De : invalid (at) *nospam* invalid.invalid (Richard Kettlewell)
Groupes : comp.lang.cDate : 23. May 2024, 10:07:23
Autres entêtes
Organisation : terraraq NNTP server
Message-ID : <wwv34q9q7d0.fsf@LkoBDZeT.terraraq.uk>
References : 1 2 3 4
User-Agent : Gnus/5.13 (Gnus v5.13) Emacs/28.2 (gnu/linux)
Malcolm McLean <
malcolm.arthur.mclean@gmail.com> writes:
static int haserror(LEXER *lex)
{
return lex->error[0] ? 1 : 0;
}
>
error is a character buffer which holds the error message if an error
has been encountered. And for convenience it is placed in the
lexer. If here is no error, it holds the empty string. However it's
not entirely obvious that testing the message directly is the way you
should be testing for an error condition, so I wrote that little
function to make things clearer.
>
It's easy enough to make it return a boolean, of course. But I don't
see a real benefit.
Possible benefits:
1) It conveys information to the reader about the nature of the
function. In this particular case the name also conveys that
information well enough, so there’s not actually much to be gained
here, but it other contexts there may be more of an advantage.
2) It conveys information to the compiler that may be exploited by the
optimizer (depending on the compilation model, the capabilities of
the target platform and optimizer, etc).
We are gradually migrating functions with boolean sense to returning
bool, albeit not very systematically, mainly for reasons #1.
-- https://www.greenend.org.uk/rjk/