]> xenbits.xensource.com Git - libvirt.git/commitdiff
docs: hacking: Add good practices for shortening conditional expressions
authorPeter Krempa <pkrempa@redhat.com>
Thu, 9 May 2019 10:20:42 +0000 (12:20 +0200)
committerPeter Krempa <pkrempa@redhat.com>
Wed, 22 May 2019 12:46:29 +0000 (14:46 +0200)
Document that checking if a integer is (non-)zero should (not must)
avoid the shortened form that C allows as it may confuse readers into
overlooking the other possible values which might be interresting to
handle.

While pointers have distinct values from the point of view of the code
we only care whether it's non-NULL and thus it's documented it's okay
to shorten those.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
ACKed-by: Eric Blake <eblake@redhat.com>
docs/hacking.html.in

index 081d793360ed712d72305fe81b8bd536d87d628c..1f82c668e52807b32ed20e7a5c4a4f4af37f77ee 100644 (file)
   }
 </pre>
 
+    <h2><a id="conditions">Conditional expressions</a></h2>
+      <p>For readability reasons new code should avoid shortening comparisons
+        to 0 for numeric types. Boolean and pointer comparisions may be
+        shortened. All long forms are okay:
+      </p>
+<pre>
+   virFooPtr foos = NULL;
+   size nfoos = 0;
+   bool hasFoos = false;
+
+GOOD:
+    if (!foos)
+    if (!hasFoos)
+    if (nfoos == 0)
+    if (foos == NULL)
+    if (hasFoos == true)
+
+BAD:
+    if (!nfoos)
+    if (nfoos)
+</pre>
+
     <h2><a id="preprocessor">Preprocessor</a></h2>
 
     <p>Macros defined with an ALL_CAPS name should generally be