Re: Memo on coding practices: strcmp() does not yield bool - Mailing list pgsql-hackers

From Tom Lane
Subject Re: Memo on coding practices: strcmp() does not yield bool
Date
Msg-id 10864.962989518@sss.pgh.pa.us
Whole thread Raw
In response to Re: Memo on coding practices: strcmp() does not yield bool  (Ed Loehr <eloehr@austin.rr.com>)
List pgsql-hackers
Ed Loehr <eloehr@austin.rr.com> writes:
> I see your examples demonstrate the danger of inappropriate or
> inattentive type conversion (e.g., splicing an int into a char), but I'm
> missing the danger you see, beyond a style offense, of "if (strcmp(foo,
> bar))"?

"if (strcmp(foo, bar))" is portable, no doubt about it.  My point is
that the idiom encourages one to think of strcmp() as yielding bool,
which leads directly to the sort of thinko I exhibited.  It's a
slippery-slope argument, basically.

I had always disliked the idiom on stylistic grounds, but I never quite
had a rational reason why.  Now I do: it's a type violation.  If C had
a distinction between int and bool then you'd not be allowed to write
this.  As an old Pascal programmer I prefer to think of the two types
as distinct...
        regards, tom lane


pgsql-hackers by date:

Previous
From: "Robert B. Easter"
Date:
Subject: Re: [BUGS] Re: Re: [SQL] MAX() of 0 records.
Next
From: Tom Lane
Date:
Subject: Re: [BUGS] Re: [SQL] MAX() of 0 records.