apr-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Ralf Mattes ...@mh-freiburg.de>
Subject Re: [PATCH] How to Use strcmp to Check for Equality Without Confusing Your Fellow Code, Or: Isn't There a Macro for That?
Date Wed, 20 Oct 2010 14:48:24 GMT
On Wed, 20 Oct 2010 07:15:21 +0200, tomas wrote:

> Hash: SHA1
> On Tue, Oct 19, 2010 at 06:36:08PM +0200, Malte S. Stretz wrote:
>> Hi folks,
>> I recently started to hack on the Apache HTTPD codebase and immediately
>> stumbled upon one of my personal code smells:  The usage of strcmp and
>> friends and how strings are compared for equality.
>> I know it is hard to agree on which variant is best [...]
> Just a personal data point from a newcomer and mostly lurker: my
> preferences are, in that order:
>   strcmp(...) == 0
>   !strcmp(...)
>   macro
> (macro coming definitely last). My primary language has been C for many
> years; therefore, the idiomatic !strcmp(...) is easy on my eyes, but
> still I consider the comparison to zero more readable (and semantically
> more significant, since it reminds me of the fact that I might want to
> compare the strings lexicographically, then strcmp(...) < 0 makes sense
> too).

I'd like to chime in as yet another lurker here ;-)
Me personal preference list:


I think '!strcmp(' treats strcmp as returning a "generalized boolean" [1]
1 which it plain and simply isn't. It would have been nice if the ISO 
standard would have restricted the return value to {-1,0,1} because then
we could use enums/defines like FIRST_SMALLER, EQUAL  and SECOND_SMALLER 
but unfortunately the didn't (must have been that major performance boost
you get from returning a[i] - b[i] ... :-)

> I don't particularly like the macros because they are necessarily
> project-specific (so PostgreSQL would have one, the Linux kernel another
> and libxml2 yet another). This tends to create little impenetrable
> kingdoms. It hurts most on projects like Apache, where integrating many
> other libraries is day-to-day.

Cheers, Ralf Mattes

View raw message