Re: strange case of "if ((a & b))" - Mailing list pgsql-hackers

From Bruce Momjian
Subject Re: strange case of "if ((a & b))"
Date
Msg-id 20210819182636.GA28998@momjian.us
Whole thread Raw
In response to Re: strange case of "if ((a & b))"  (Tom Lane <tgl@sss.pgh.pa.us>)
List pgsql-hackers
On Wed, Aug 18, 2021 at 11:08:57PM -0400, Tom Lane wrote:
> Peter Smith <smithpb2250@gmail.com> writes:
> > On Thu, Aug 19, 2021 at 4:29 AM Justin Pryzby <pryzby@telsasoft.com> wrote:
> >> -       state->oneCol = (origTupdesc->natts == 1) ? true : false;
> >> +       state->oneCol = origTupdesc->natts == 1;
> 
> FWIW, I am definitely not a fan of removing the parentheses in this
> context, because readers might wonder if you meant an "a = b = 1"
> multiple-assignment, or even misread it as that and be confused.
> So I'd prefer
> 
>           state->oneCol = (origTupdesc->natts == 1);

Good point --- extra parentheses are not always bad.
> 
> In the context of "return (a == b)", I'm about neutral on whether
> to keep the parens or not, but I wonder why this patch does some
> of one and some of the other.
> 
> I do agree that "x ? true : false" is silly in contexts where x
> is guaranteed to yield zero or one.  What you need to be careful
> about is where x might yield other bitpatterns, for example
> "(flags & SOMEFLAG) ? true : false".  Pre-C99, this type of coding
> was often *necessary*.  With C99, it's only necessary if you're
> not sure that the compiler will cast the result to boolean.

Agreed.

-- 
  Bruce Momjian  <bruce@momjian.us>        https://momjian.us
  EDB                                      https://enterprisedb.com

  If only the physical world exists, free will is an illusion.




pgsql-hackers by date:

Previous
From: Mark Dilger
Date:
Subject: Re: reporting TID/table with corruption error
Next
From: Bruce Momjian
Date:
Subject: Re: elog.c query_id support vs shutdown