svn commit: r201794 - in head/sys: ddb dev/ep dev/ex netinet6

Dag-Erling Smørgrav des at des.no
Mon Jan 11 13:44:13 UTC 2010


Bruce Evans <brde at optusnet.com.au> writes:
> All of these bugs should have been avoided by using normal style.  KNF
> doesn't even use the "!" operator for testing simple booleans!  Older
> KNF code in kern uses comparisions with 0 fairly consistently for testing
> flags in a bitmap being 0.  Even the not-so-old KNF code in kern that
> tests the not-so-old flag TDF_SINTR does this (there is only one instance,
> in kern_sig.c, where the above is spelled correctly as
> ((td->td_flags & TDF_SINTR) == 0).  I like to use ! for testing even sets
> of flags so I prefer the above, but this is not KNF.
>
> Spelling for testing the opposite sense is more mixed.  Omitting the
> != 0 seems to be most common.

ISTR that 1) the rule is "don't use it as a predicate unless the name
clearly marks it as such" and 2) it isn't actually written down
anywhere...

TD_IS_SLEEPING(td) is clearly a predicate, so it's OK not to compare it
explicitly with 0; (td->td_flags & TDF_SINTR) may well be a predicate,
but this is not obvious from a quick glance at the code, so there should
be an explicit comparison.

The canonical example is strcmp(), which is *not* a predicate, but is
often used (and easily misused) as one.  It almost makes me want to add
the following to <string.h>:

#define streq(s1, s1) (strcmp((s1), (s2)) == 0)
#define strlt(s1, s1) (strcmp((s1), (s2)) < 0)
#define strgt(s1, s1) (strcmp((s1), (s2)) > 0)

(or the equivalent static inline functions to avoid double expansion
issues)

DES
-- 
Dag-Erling Smørgrav - des at des.no


More information about the svn-src-all mailing list