[zeromq-dev] Questions about Coding Style

Pieter Hintjens ph at imatix.com
Sat Feb 11 22:53:21 CET 2012


On Sun, Feb 12, 2012 at 4:47 AM, niXman <i.nixman at gmail.com> wrote:

> Do you all agree with that the arguments must be checked? And with the
> fact that in case of error of arguments is not necessary to use assert
> but set errno and return -1?

Up to a point. Some invalid arguments are best handled with an
assertion. The question is, can the calling program do anything useful
with a return code? If it is passing a buggy argument, say a NULL
socket, it is much safer to assert than to pass a return code. Buggy
code is not trustable. I.e. good chance it won't properly handle the
return code.

However if a program gets input from, say, a socket, it cannot assert
if that input is bad.

The question is, "is error handling part of the core functionality of
this path, or not". If it is, then check everything pedantically. If
not, asserts are safer and will lead to more robust code, faster.

You can easily prove this by taking a common mistake, e.g. passing an
uninitialized message to a send() call. Now, compare using a -1 return
code to asserting. The return code requires more code in the
application (which means more chance of further errors), and creates
more chance that error won't be caught at all, leading to strange
behavior that is hard to debug. Whereas an assert is immediately
caught, at the precise place the error happens, and it tells the user
precisely what is wrong.

So in my libraries, like CZMQ, I validate input arguments with
asserts, systematically.

-Pieter



More information about the zeromq-dev mailing list