[libvirt] [PATCH 2/3] docs: hacking: Add good practices for shortening conditional expressions
Martin Kletzander
mkletzan at redhat.com
Thu May 9 10:53:07 UTC 2019
On Thu, May 09, 2019 at 12:43:33PM +0200, Peter Krempa wrote:
>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 at redhat.com>
>---
> docs/hacking.html.in | 22 ++++++++++++++++++++++
> 1 file changed, 22 insertions(+)
>
>diff --git a/docs/hacking.html.in b/docs/hacking.html.in
>index 081d793360..a2800853ef 100644
>--- a/docs/hacking.html.in
>+++ b/docs/hacking.html.in
>@@ -826,6 +826,28 @@
> }
> </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 (foos)
why is this bad when it is a pointer? Typo?
>+</pre>
>+
> <h2><a id="preprocessor">Preprocessor</a></h2>
>
> <p>Macros defined with an ALL_CAPS name should generally be
>--
>2.21.0
>
>--
>libvir-list mailing list
>libvir-list at redhat.com
>https://www.redhat.com/mailman/listinfo/libvir-list
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: not available
URL: <http://listman.redhat.com/archives/libvir-list/attachments/20190509/bbe4b876/attachment-0001.sig>
More information about the libvir-list
mailing list