qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Eric Blake <eblake@redhat.com>
To: arei.gonglei@huawei.com, qemu-devel@nongnu.org
Cc: peter.maydell@linaro.org, peter.crosthwaite@xilinx.com,
	weidong.huang@huawei.com, stefanha@redhat.com, mst@redhat.com,
	marcel.a@redhat.com, luonengjun@huawei.com, armbru@redhat.com,
	lcapitulino@redhat.com, av1474@comtv.ru, kraxel@redhat.com,
	aliguori@amazon.com, imammedo@redhat.com, dmitry@daynix.com,
	pbonzini@redhat.com, peter.huangpeng@huawei.com,
	afaerber@suse.de, dgilbert@redhat.com
Subject: Re: [Qemu-devel] [PATCH v2 1/8] CODING_STYLE: Section about conditional statement
Date: Thu, 31 Jul 2014 21:07:04 -0600	[thread overview]
Message-ID: <53DB0458.5020605@redhat.com> (raw)
In-Reply-To: <1406860365-5516-2-git-send-email-arei.gonglei@huawei.com>

[-- Attachment #1: Type: text/plain, Size: 2406 bytes --]

On 07/31/2014 08:32 PM, arei.gonglei@huawei.com wrote:
> From: Gonglei <arei.gonglei@huawei.com>
> 
> Yoda conidtions lack of readability, and QEMU have a

s/conidtions/conditions/
s/of //
s/have/has/

> strict compiler configuration for checking a common
> mistake like "if (dev = NULL)". Make it a written rule.
> 
> Signed-off-by: Gonglei <arei.gonglei@huawei.com>
> ---
>  CODING_STYLE | 19 +++++++++++++++++++
>  1 file changed, 19 insertions(+)
> 
> diff --git a/CODING_STYLE b/CODING_STYLE
> index 4280945..11a79d4 100644
> --- a/CODING_STYLE
> +++ b/CODING_STYLE
> @@ -91,3 +91,22 @@ Mixed declarations (interleaving statements and declarations within blocks)
>  are not allowed; declarations should be at the beginning of blocks.  In other
>  words, the code should not generate warnings if using GCC's
>  -Wdeclaration-after-statement option.
> +
> +6. Conditional statement

s/statement/statements/

> +
> +Please don't use Yoda conditions because of lack of readability. Furthermore,
> +it is not the QEMU idiomatic coding style. Example:
> +
> +Usually a conditional statement in QEMU would be written as:
> +if (a == 0) {
> +    /* Reads like: "If a is equal to 0..." */
> +    do_something();
> +}
> +
> +Yoda conditions describe the same expression, but reversed:
> +if (0 == a) {
> +    /* Reads like: "If 0 equals to a" */
> +    do_something();
> +}
> +
> +The constant is listed first, then the variable being compared to.
> 

This spends more lines documenting the bad style than the good, and
doesn't quite flow with the rest of the document.  At the risk of
sounding like a complete rewrite, how about:

=====
When comparing a variable for (in)equality with a constant, list the
constant on the right, as in:

if (a == 0) {
    do_something();
}

Rationale: Yoda conditionals (as in 'if (0 == a)') are awkward to read.
Besides, good compilers already warn users when == is mis-typed as =,
even when the constant is on the right.
=====

and maybe some other ideas also worth adding:

=====
Avoid redundant comparisons: (bool_expr == true) is better written as
(bool_expr), and (ptr == NULL) is shorter as (!ptr).  Use of !!value is
a convenient shorthand for converting a value into a boolean.
=====

-- 
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 539 bytes --]

  reply	other threads:[~2014-08-01  3:07 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-08-01  2:32 [Qemu-devel] [PATCH v2 for-2.2 0/8] about Yoda conditions arei.gonglei
2014-08-01  2:32 ` [Qemu-devel] [PATCH v2 1/8] CODING_STYLE: Section about conditional statement arei.gonglei
2014-08-01  3:07   ` Eric Blake [this message]
2014-08-01  3:31     ` Gonglei (Arei)
2014-08-01  2:32 ` [Qemu-devel] [PATCH v2 2/8] usb: a trivial code change for more idiomatic writing style arei.gonglei
2014-08-01  3:08   ` Eric Blake
2014-08-01  3:32     ` Gonglei (Arei)
2014-08-01  3:42       ` Andreas Färber
2014-08-01  3:54         ` Gonglei (Arei)
2014-08-01  6:41         ` Markus Armbruster
2014-08-01  6:50           ` Gonglei (Arei)
2014-08-01 15:56           ` Eric Blake
2014-08-01  2:32 ` [Qemu-devel] [PATCH v2 3/8] audio: " arei.gonglei
2014-08-01  2:32 ` [Qemu-devel] [PATCH v2 4/8] isa-bus: " arei.gonglei
2014-08-01  2:32 ` [Qemu-devel] [PATCH v2 5/8] " arei.gonglei
2014-08-01  2:32 ` [Qemu-devel] [PATCH v2 6/8] spice: " arei.gonglei
2014-08-01  2:32 ` [Qemu-devel] [PATCH v2 7/8] vl: " arei.gonglei
2014-08-01  2:32 ` [Qemu-devel] [PATCH v2 8/8] vmxnet3: " arei.gonglei
2014-08-01  2:51 ` [Qemu-devel] [PATCH v2 for-2.2 0/8] about Yoda conditions Eric Blake
2014-08-01  3:23   ` Gonglei (Arei)

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=53DB0458.5020605@redhat.com \
    --to=eblake@redhat.com \
    --cc=afaerber@suse.de \
    --cc=aliguori@amazon.com \
    --cc=arei.gonglei@huawei.com \
    --cc=armbru@redhat.com \
    --cc=av1474@comtv.ru \
    --cc=dgilbert@redhat.com \
    --cc=dmitry@daynix.com \
    --cc=imammedo@redhat.com \
    --cc=kraxel@redhat.com \
    --cc=lcapitulino@redhat.com \
    --cc=luonengjun@huawei.com \
    --cc=marcel.a@redhat.com \
    --cc=mst@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=peter.crosthwaite@xilinx.com \
    --cc=peter.huangpeng@huawei.com \
    --cc=peter.maydell@linaro.org \
    --cc=qemu-devel@nongnu.org \
    --cc=stefanha@redhat.com \
    --cc=weidong.huang@huawei.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).