[PATCH] coding-style: Allow some use of ternary operators

Michal Privoznik posted 1 patch 1 year, 8 months ago
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/libvirt tags/patchew/74d640cf2d9b76203b20e42acbbab9ba6fd87477.1658754838.git.mprivozn@redhat.com
There is a newer version of this series
docs/coding-style.rst | 7 ++++++-
1 file changed, 6 insertions(+), 1 deletion(-)
[PATCH] coding-style: Allow some use of ternary operators
Posted by Michal Privoznik 1 year, 8 months ago
While we all understand that excessive use of ternary operator
may worsen code readability (e.g. nested, multi-line expression),
there are few cases where using it actually improves code
readability. For instance, when a function takes a long list of
arguments out of which one depends on a boolean expression, or
when formatting "yes"/"no" or "on"/"off" values based on a
boolean variable (although one can argue that the latter is a
subset of the former). Just consider alternatives to:

  virBufferAsprintf(buf, "<elem>%s</elem>\n", boolVar ? "yes" : "no");

In fact, this pattern occurs plenty in our code. Exempt if from
our "no ternary operators" rule.

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
---
 docs/coding-style.rst | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/docs/coding-style.rst b/docs/coding-style.rst
index bf0a80fbc5..038f18bda2 100644
--- a/docs/coding-style.rst
+++ b/docs/coding-style.rst
@@ -470,7 +470,9 @@ Pointer comparisons may be shortened. All long forms are okay.
     if (!foo)                # or: if (foo == NULL)
 
 New code should avoid the ternary operator as much as possible.
-Specifically it must never span more than one line or nest:
+Specifically it must never span more than one line or nest. However,
+its usage in very basic cases is warranted (e.g. when deciding
+between two constant strings):
 
 ::
 
@@ -481,6 +483,9 @@ Specifically it must never span more than one line or nest:
 
     char *foo = bar ? bar->baz ? bar->baz->foo : "nobaz" : "nobar";
 
+  GOOD:
+    virBufferAsprintf(buf, "<element>%s</element>\n", boolVar ? "yes" : "no");
+
 Preprocessor
 ------------
 
-- 
2.35.1
Re: [PATCH] coding-style: Allow some use of ternary operators
Posted by Daniel P. Berrangé 1 year, 8 months ago
On Mon, Jul 25, 2022 at 03:13:58PM +0200, Michal Privoznik wrote:
> While we all understand that excessive use of ternary operator
> may worsen code readability (e.g. nested, multi-line expression),
> there are few cases where using it actually improves code
> readability. For instance, when a function takes a long list of
> arguments out of which one depends on a boolean expression, or
> when formatting "yes"/"no" or "on"/"off" values based on a
> boolean variable (although one can argue that the latter is a
> subset of the former). Just consider alternatives to:
> 
>   virBufferAsprintf(buf, "<elem>%s</elem>\n", boolVar ? "yes" : "no");
> 
> In fact, this pattern occurs plenty in our code. Exempt if from

s/if/it/

> our "no ternary operators" rule.
> 
> Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
> ---
>  docs/coding-style.rst | 7 ++++++-
>  1 file changed, 6 insertions(+), 1 deletion(-)
> 
> diff --git a/docs/coding-style.rst b/docs/coding-style.rst
> index bf0a80fbc5..038f18bda2 100644
> --- a/docs/coding-style.rst
> +++ b/docs/coding-style.rst
> @@ -470,7 +470,9 @@ Pointer comparisons may be shortened. All long forms are okay.
>      if (!foo)                # or: if (foo == NULL)
>  
>  New code should avoid the ternary operator as much as possible.
> -Specifically it must never span more than one line or nest:
> +Specifically it must never span more than one line or nest. However,
> +its usage in very basic cases is warranted (e.g. when deciding
> +between two constant strings):

"However" shouldn't generally be the start of a sentence.

I'd suggest

  Its usage in basic cases is warranted (e.g. when deciding between
  two constant strings), however, it must never span more than one
  line or nest.

>  
>  ::
>  
> @@ -481,6 +483,9 @@ Specifically it must never span more than one line or nest:
>  
>      char *foo = bar ? bar->baz ? bar->baz->foo : "nobaz" : "nobar";
>  
> +  GOOD:
> +    virBufferAsprintf(buf, "<element>%s</element>\n", boolVar ? "yes" : "no");
> +
>  Preprocessor
>  ------------
>  
> -- 
> 2.35.1
> 

With regards,
Daniel
-- 
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
Re: [PATCH] coding-style: Allow some use of ternary operators
Posted by Claudio Fontana 1 year, 8 months ago
A common sense improvement.

Reviewed-by: Claudio Fontana <cfontana@suse.de>

On 7/25/22 15:13, Michal Privoznik wrote:
> While we all understand that excessive use of ternary operator
> may worsen code readability (e.g. nested, multi-line expression),
> there are few cases where using it actually improves code
> readability. For instance, when a function takes a long list of
> arguments out of which one depends on a boolean expression, or
> when formatting "yes"/"no" or "on"/"off" values based on a
> boolean variable (although one can argue that the latter is a
> subset of the former). Just consider alternatives to:
> 
>   virBufferAsprintf(buf, "<elem>%s</elem>\n", boolVar ? "yes" : "no");
> 
> In fact, this pattern occurs plenty in our code. Exempt if from
> our "no ternary operators" rule.
> 
> Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
> ---
>  docs/coding-style.rst | 7 ++++++-
>  1 file changed, 6 insertions(+), 1 deletion(-)
> 
> diff --git a/docs/coding-style.rst b/docs/coding-style.rst
> index bf0a80fbc5..038f18bda2 100644
> --- a/docs/coding-style.rst
> +++ b/docs/coding-style.rst
> @@ -470,7 +470,9 @@ Pointer comparisons may be shortened. All long forms are okay.
>      if (!foo)                # or: if (foo == NULL)
>  
>  New code should avoid the ternary operator as much as possible.
> -Specifically it must never span more than one line or nest:
> +Specifically it must never span more than one line or nest. However,
> +its usage in very basic cases is warranted (e.g. when deciding
> +between two constant strings):
>  
>  ::
>  
> @@ -481,6 +483,9 @@ Specifically it must never span more than one line or nest:
>  
>      char *foo = bar ? bar->baz ? bar->baz->foo : "nobaz" : "nobar";
>  
> +  GOOD:
> +    virBufferAsprintf(buf, "<element>%s</element>\n", boolVar ? "yes" : "no");
> +
>  Preprocessor
>  ------------
>