On Mon, Nov 18, 2019 at 11:28:08 -0300, Daniel Henrique Barboza wrote:
On 11/18/19 10:53 AM, Peter Krempa wrote:
> On Mon, Nov 18, 2019 at 14:47:03 +0100, Michal Privoznik wrote:
> > On 11/18/19 2:27 PM, Daniel Henrique Barboza wrote:
> >
> > I believe it was John who persuaded us to use explicit integer comparison
> > for integer variables. The idea is that it's clear from the check itself
if
> > we are comparing integers or pointers. And I agree with him - in my new code
> > I always use 'if (x > 0)' or 'if (x != 0)' instead of
'if (x)' or 'if (!x)'.
I'll start paying more attention to it in the code I'll be writing too.
>
> We also encourage it officially in our coding style:
>
>
https://libvirt.org/hacking.html#conditions
>
I wasn't aware that (hasFoos == true) was a valid/encouraged format. I took flak
in QEMU doing these kind of things and got used to do the short format whenever
possible. Interesting.
Note that if (hasFoos) is also valid without any preference towards one
or the other so the short form is good and in most cases not confusing.
What's specifically discouraged is testing integers without a comparison
operator as in such case it obscures that there might be a value of
significance other than the usual boolean cases NULL, !NULL in case of
pointers of false, true in case of booleans.