[libvirt] [PATCH 3/3] docs: hacking: Discourage use of the ternary operator and ban it's abuse

Peter Krempa pkrempa at redhat.com
Thu May 9 10:43:34 UTC 2019


Forbid breaking lines inside the two branches of the ternary operator
and nesting them. Using it in these instances does not help readability.

Signed-off-by: Peter Krempa <pkrempa at redhat.com>
---
 docs/hacking.html.in | 11 +++++++++++
 1 file changed, 11 insertions(+)

diff --git a/docs/hacking.html.in b/docs/hacking.html.in
index a2800853ef..fc4adae354 100644
--- a/docs/hacking.html.in
+++ b/docs/hacking.html.in
@@ -847,6 +847,17 @@ BAD:
     if (!nfoos)
     if (foos)
 </pre>
+      <p>New code should avoid the ternary operator as much as possible.
+        Specifically it must never span more than one line or nest:
+      </p>
+<pre>
+BAD:
+    char *foo = baz ?
+                virDoSomethingReallyComplex(driver, vm, something, baz->foo) :
+                NULL;
+
+    char *foo = bar ? bar->baz ? bar->baz->foo : "nobaz" : "nobar";
+</pre>

     <h2><a id="preprocessor">Preprocessor</a></h2>

-- 
2.21.0




More information about the libvir-list mailing list