Re: strange case of "if ((a & b))"

From: Justin Pryzby <pryzby(at)telsasoft(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Peter Smith <smithpb2250(at)gmail(dot)com>, Bruce Momjian <bruce(at)momjian(dot)us>, pgsql-hackers(at)lists(dot)postgresql(dot)org
Subject: Re: strange case of "if ((a & b))"
Date: 2021-09-06 00:11:10
Message-ID: 20210906001110.GF26465@telsasoft.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Wed, Aug 18, 2021 at 11:08:57PM -0400, Tom Lane wrote:
> Peter Smith <smithpb2250(at)gmail(dot)com> writes:
> > On Thu, Aug 19, 2021 at 4:29 AM Justin Pryzby <pryzby(at)telsasoft(dot)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);
>
> 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.

I revised the patch based on these comments. I think my ternary patch already
excluded the cases that test something other than a boolean.

Peter: you quoted my patch but didn't comment on it. Your regex finds a lot of
conditional boolean assignments, but I agree that they're best left alone. My
patches are to clean up silly cases, not to rewrite things in a way that's
arguably better (but arguably not worth changing and so also not worth arguing
that it's better).

--
Justin

Attachment Content-Type Size
0001-Avoid-double-parens.patch text/x-diff 5.6 KB
0002-Avoid-verbose-ternary-operator-with-expressions-whic.patch text/x-diff 14.5 KB

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Michael Paquier 2021-09-06 00:17:59 Timeout failure in 019_replslot_limit.pl
Previous Message Shinoda, Noriyoshi (PN Japan FSIP) 2021-09-05 23:53:36 RE: New predefined roles- 'pg_read/write_all_data'