[PATCH 9/9] domain_conf: rewrite variable setting to ternary operator

Kristina Hanicova posted 9 patches 3 years, 6 months ago
There is a newer version of this series
[PATCH 9/9] domain_conf: rewrite variable setting to ternary operator
Posted by Kristina Hanicova 3 years, 6 months ago
Signed-off-by: Kristina Hanicova <khanicov@redhat.com>
---
 src/conf/domain_conf.c | 8 ++------
 1 file changed, 2 insertions(+), 6 deletions(-)

diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
index e52f39c809..b600bfec31 100644
--- a/src/conf/domain_conf.c
+++ b/src/conf/domain_conf.c
@@ -4206,12 +4206,8 @@ virDomainObjGetOneDefState(virDomainObj *vm,
     if (virDomainObjUpdateModificationImpact(vm, &flags) < 0)
         return NULL;
 
-    if (live) {
-        if (flags & VIR_DOMAIN_AFFECT_LIVE)
-            *live = true;
-        else
-            *live = false;
-    }
+    if (live)
+        *live = (flags & VIR_DOMAIN_AFFECT_LIVE) ? true : false;
 
     if (virDomainObjIsActive(vm) && flags & VIR_DOMAIN_AFFECT_CONFIG)
         return vm->newDef;
-- 
2.35.3
Re: [PATCH 9/9] domain_conf: rewrite variable setting to ternary operator
Posted by Peter Krempa 3 years, 6 months ago
On Wed, Jul 20, 2022 at 15:11:12 +0200, Kristina Hanicova wrote:
> Signed-off-by: Kristina Hanicova <khanicov@redhat.com>
> ---
>  src/conf/domain_conf.c | 8 ++------
>  1 file changed, 2 insertions(+), 6 deletions(-)
> 
> diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
> index e52f39c809..b600bfec31 100644
> --- a/src/conf/domain_conf.c
> +++ b/src/conf/domain_conf.c
> @@ -4206,12 +4206,8 @@ virDomainObjGetOneDefState(virDomainObj *vm,
>      if (virDomainObjUpdateModificationImpact(vm, &flags) < 0)
>          return NULL;
>  
> -    if (live) {
> -        if (flags & VIR_DOMAIN_AFFECT_LIVE)
> -            *live = true;
> -        else
> -            *live = false;
> -    }
> +    if (live)
> +        *live = (flags & VIR_DOMAIN_AFFECT_LIVE) ? true : false;
>  

https://libvirt.org/coding-style.html#conditional-expressions

We suggest that new code avoids ternary operators.

I'd prefer if this patch is dropped.
Re: [PATCH 9/9] domain_conf: rewrite variable setting to ternary operator
Posted by Michal Prívozník 3 years, 6 months ago
On 7/20/22 15:40, Peter Krempa wrote:
> On Wed, Jul 20, 2022 at 15:11:12 +0200, Kristina Hanicova wrote:
>> Signed-off-by: Kristina Hanicova <khanicov@redhat.com>
>> ---
>>  src/conf/domain_conf.c | 8 ++------
>>  1 file changed, 2 insertions(+), 6 deletions(-)
>>
>> diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
>> index e52f39c809..b600bfec31 100644
>> --- a/src/conf/domain_conf.c
>> +++ b/src/conf/domain_conf.c
>> @@ -4206,12 +4206,8 @@ virDomainObjGetOneDefState(virDomainObj *vm,
>>      if (virDomainObjUpdateModificationImpact(vm, &flags) < 0)
>>          return NULL;
>>  
>> -    if (live) {
>> -        if (flags & VIR_DOMAIN_AFFECT_LIVE)
>> -            *live = true;
>> -        else
>> -            *live = false;
>> -    }
>> +    if (live)
>> +        *live = (flags & VIR_DOMAIN_AFFECT_LIVE) ? true : false;
>>  
> 
> https://libvirt.org/coding-style.html#conditional-expressions
> 
> We suggest that new code avoids ternary operators.
> 
> I'd prefer if this patch is dropped.
> 

And what about:

if (live)
  *live = !!(flags & VIR_DOMAIN_AFFECT_LIVE);

? I agree that current version of the code is more verbose than it needs
to be. And while ternary operators might look bad, in fact I've
suggested them here:


https://gitlab.com/MichalPrivoznik/libvirt/-/commit/d2894d9f07ff2dde77bea53bb39dc8d0176eac85#f6109f17d3eb7394abb620a27ec56d76dd5220b0_4892_4878

Is there any better way?

Michal
Re: [PATCH 9/9] domain_conf: rewrite variable setting to ternary operator
Posted by Martin Kletzander 3 years, 6 months ago
On Wed, Jul 20, 2022 at 04:28:30PM +0200, Michal Prívozník wrote:
>On 7/20/22 15:40, Peter Krempa wrote:
>> On Wed, Jul 20, 2022 at 15:11:12 +0200, Kristina Hanicova wrote:
>>> Signed-off-by: Kristina Hanicova <khanicov@redhat.com>
>>> ---
>>>  src/conf/domain_conf.c | 8 ++------
>>>  1 file changed, 2 insertions(+), 6 deletions(-)
>>>
>>> diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
>>> index e52f39c809..b600bfec31 100644
>>> --- a/src/conf/domain_conf.c
>>> +++ b/src/conf/domain_conf.c
>>> @@ -4206,12 +4206,8 @@ virDomainObjGetOneDefState(virDomainObj *vm,
>>>      if (virDomainObjUpdateModificationImpact(vm, &flags) < 0)
>>>          return NULL;
>>>
>>> -    if (live) {
>>> -        if (flags & VIR_DOMAIN_AFFECT_LIVE)
>>> -            *live = true;
>>> -        else
>>> -            *live = false;
>>> -    }
>>> +    if (live)
>>> +        *live = (flags & VIR_DOMAIN_AFFECT_LIVE) ? true : false;
>>>
>>
>> https://libvirt.org/coding-style.html#conditional-expressions
>>
>> We suggest that new code avoids ternary operators.
>>
>> I'd prefer if this patch is dropped.
>>
>
>And what about:
>
>if (live)
>  *live = !!(flags & VIR_DOMAIN_AFFECT_LIVE);
>
>? I agree that current version of the code is more verbose than it needs
>to be. And while ternary operators might look bad, in fact I've
>suggested them here:
>
>
>https://gitlab.com/MichalPrivoznik/libvirt/-/commit/d2894d9f07ff2dde77bea53bb39dc8d0176eac85#f6109f17d3eb7394abb620a27ec56d76dd5220b0_4892_4878
>
>Is there any better way?
>

I'll jump in as well, since this seems to be a very heated topic.

How about just:

if (live)
     *live = flags & VIR_DOMAIN_AFFECT_LIVE;

??

If you are worried about the value, then be aware that according to C11
[0] and even C99 [1] (could not find reliable source for C89) section
"6.3.1.2 Boolean type", point 1:

     When any scalar value is converted to _Bool, the result is 0 if the
     value compares equal to 0; otherwise, the result is 1.

[0] https://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf
[1] https://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf

>Michal
>
Re: [PATCH 9/9] domain_conf: rewrite variable setting to ternary operator
Posted by Kristina Hanicova 3 years, 6 months ago
On Wed, Jul 20, 2022 at 3:41 PM Peter Krempa <pkrempa@redhat.com> wrote:

> On Wed, Jul 20, 2022 at 15:11:12 +0200, Kristina Hanicova wrote:
> > Signed-off-by: Kristina Hanicova <khanicov@redhat.com>
> > ---
> >  src/conf/domain_conf.c | 8 ++------
> >  1 file changed, 2 insertions(+), 6 deletions(-)
> >
> > diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
> > index e52f39c809..b600bfec31 100644
> > --- a/src/conf/domain_conf.c
> > +++ b/src/conf/domain_conf.c
> > @@ -4206,12 +4206,8 @@ virDomainObjGetOneDefState(virDomainObj *vm,
> >      if (virDomainObjUpdateModificationImpact(vm, &flags) < 0)
> >          return NULL;
> >
> > -    if (live) {
> > -        if (flags & VIR_DOMAIN_AFFECT_LIVE)
> > -            *live = true;
> > -        else
> > -            *live = false;
> > -    }
> > +    if (live)
> > +        *live = (flags & VIR_DOMAIN_AFFECT_LIVE) ? true : false;
> >
>
> https://libvirt.org/coding-style.html#conditional-expressions
>
> We suggest that new code avoids ternary operators.
>
> I'd prefer if this patch is dropped.
>
>
I think that it is reasonably used in this case and makes the code much
more readable. Also its simple enough, no nesting or spanning more lines....


Kristina