Re: [PATCH 6/9] drivers/net/wireless/iwlwifi/iwl-4965.c: Correct use of ! and &

Julia Lawall
2008-03-05T12:35:53Z

On Wed, 5 Mar 2008, Ingo Molnar wrote:

>
> * Julia Lawall <julia@diku.dk> wrote:
>
> > There are some legitimate uses of !x & y which are actually of the
> > form !x & !y, where x and y are function calls. That is a not
> > particularly elegant way of getting both x and y to be evaluated and
> > then combining the results using "and". If such code is considered
> > acceptable, then perhaps the sparse patch should be more complicated.
>
> i tend to be of the opinion that the details in C source code should be
> visually obvious and should be heavily simplified down from what is
> 'possible' language-wise - with most deviations and complications that
> depart from convention considered an error. I'd consider "!fn1() &
> !fn2()" a borderline coding style violation in any case - and it costs
> nothing to change it to "!fn1() && !fn2()".

!fn1() && !fn2() does not have the same semantics as !fn1() & !fn2(). In
!fn1() & !fn2() both function calls are evaluated. In !fn1() && !fn2(),
if !fn1() returns false then !fn2() is not evaluated. I haven't studied
the particular instances of fn2(), though, to know whether it makes a
difference.

One could instead do something like:

x = fn1();
y = fn2();
if (!x && !y) ...

It would certainly be clearer, but more verbose.

julia