tech-kern archive

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

Re: KNF and the C preprocessor



On Tue, Dec 11, 2012 at 08:28:51AM +0000, David Laight wrote:
>

> On Tue, Dec 11, 2012 at 06:10:09AM +0000, David Holland wrote:
> > On Tue, Dec 11, 2012 at 01:27:09AM +0000, Roland C. Dowdeswell wrote:
> >  > As an example, I often define a macro when I am using Kerberos or
> >  > GSSAPI that looks roughly like:
> >  > 
> >  > #ifdef K5BAIL(x) do {
> >  >          ret = x;
> >  >          if (ret) {
> >  >                  /* format error message and whatnot
> >  >                   * in terms of #x.
> >  >                   */
> >  > 
> >  >                  goto bail;
> >  >          }
> >  >  } while (0)
> > 
> > The code like this in src/sys/nfs is a reliably steady source of
> > problems, and I'd argue that macros of this form are not at all worth
> > the problems they cause.
> 
> Absolutely, that is one construct I would ban.
> Encapsulating so you can do:
>       if (K5BAIL(xxx(....), "error text"))
>               goto bail;
> Or even separating the function name (so it can be traced)
> at least leaves the flow control obvious.
> If you embed a goto (or return) in a #define you'd better have the
> defininition very close to the use.

Well, the theory is (whether you believe it or not) is that the
macro name is supposed to give the impression that it will ``bail''
which is commonly understood to mean jump to the cleanup code at
the bottom of the function.  I would thus argue that it does more
or less leave the flow of control relatively obvious.

I do agree that the code in src/sys/nfs is not obvious but that
code already violate /usr/share/misc/style in many other ways,
such as not being capitalised, not being in a do { } while (0),
and so on.

All that said, I only really use this method when I'm using Kerberos
as pretty much every function can return an error and the code
otherwise tends to become less readable due to its length as each
function is mostly error handling which obscures what is actually
being done in the more likely successful case.

--
    Roland Dowdeswell                      http://Imrryr.ORG/~elric/


Home | Main Index | Thread Index | Old Index