From: Jamie Lokier <jamie@shareable.org>
To: Lennart Sorensen <lsorense@csclub.uwaterloo.ca>
Cc: Blue Swirl <blauwirbel@gmail.com>, qemu-devel@nongnu.org
Subject: Re: [Qemu-devel] [7234] Use a more natural order
Date: Thu, 23 Apr 2009 23:46:25 +0100 [thread overview]
Message-ID: <20090423224625.GP13326@shareable.org> (raw)
In-Reply-To: <20090423194447.GK3795@csclub.uwaterloo.ca>
Lennart Sorensen wrote:
> On Thu, Apr 23, 2009 at 10:31:05PM +0300, Blue Swirl wrote:
> > I don't think any code style document can cover all possible cases.
> > But another approach can be used: you could try to find a precedent
> > case where this style has been used in QEMU.
>
> # grep -r '([0-9] =' .
> ./net.c: if (0 == errno && '\0' == *last_char &&
> ./hw/gus.c: if (0 == ((mode >> 4) & 1)) {
> ./hw/dma.c: if ((0 == (d->mask & mask)) && (0 != (d->status & (mask << 4)))) {
> ./hw/sb16.c: if (0 == s->needed_bytes) {
> ./hw/sb16.c: if (0 == s->needed_bytes) {
> ./hw/sb16.c: if (0 == s->dma_auto) {
> ./hw/sb16.c: if (0 == s->dma_auto) {
>
> That was just one quick search. Looks like whoever wrote a bunch of
> the audio hardware emulation liked less buggy code.
No, it means they believe that way around makes them less likely to
write bugs. They may be right for themselves. It doesn't mean the
code is less buggy, and it doesn't mean someone else following the
same style would write less buggy code because of the style.
Same for the redundant parentheses around every comparison next to &&.
Really, there is no need: if (condition && condition) is so common
that there's no need to clarify the grouping. Unlike say "x & 1 << 2"
where it's a good idea to add parentheses.
> Well to me software development is a kind of engineering and hence using
> anything but the safest practice that is at all practical makes no sense.
But what you call safest isn't safest for everyone. A lot depends on
what people are familiar with and trained to spot deviations from.
> That means:
> Constants before variables in all comparisons.
> Braces are never optional for blocks.
I disagree with both, but especially the first. You call it safe
practice. I call it likely to cause more bugs because it's unfamiliar
compared with common practice. And because it's the opposite to the
way we say it mathematically or in English.
It's opposite to the "Object Verb Subject" grammar that English
speakers think in: asking "is zero equal to x?" is really weird
English, because that is asking a question _about zero_, which is not
right: zero is not meant to be something about which there is any question.
It's a constant!
That cognitive dissonance adds a translation step between thinking (in
English) and reading/writing, which means more chance of not seeing
other bugs - limited brainpower and all.
"if (0 < x)" is even worse: it's begging to be accidentally written
with ">". More importantly, if it _is_ accidentally written with ">",
the mistake is less likely to be noticed than if it were written the
other way around. But if you write "if (0 == x)" and "if (x > 0)"
just because of "=" C syntax, that's an inconsistent mess.
Do you also write things like "for (x = 0; NUMBER > x; x++)"? No, of
course not.
Sure, it was ok practice back in the days when compilers would let you
write "if (x = 0)", but those days are long gone. Now all compilers
warn, and you can ask them to make it an error. A compiler check is
much safer than depending on writing style, especially a weird one
that's inconsistent with other writing in the same code, and
inconsistent with English thinking.
> The second one is especially hard to get some people to understand.
Accidentally mistaking the scope of a block is unlikely using proper
tools. We're not using NOTEPAD.EXE any more. Editors can indent code
automatically these days (last 20 years or so). Again, using tools is
better than depending on writing style because it catches more mistakes.
Actually I'm sympathetic to people who prefer braces around every
block, and I happily follow such a style when requested. It doesn't
look too painful or weird, unlike the "unnatural" comparison order. :-)
But I don't think it's necessary on projects where everyone uses other
equally good practices instead to catch the same errors.
-- Jamie
next prev parent reply other threads:[~2009-04-23 22:46 UTC|newest]
Thread overview: 38+ messages / expand[flat|nested] mbox.gz Atom feed top
2009-04-23 18:29 [Qemu-devel] [7234] Use a more natural order Blue Swirl
2009-04-23 18:39 ` Andreas Färber
2009-04-23 18:43 ` Blue Swirl
2009-04-23 18:53 ` Lennart Sorensen
2009-04-23 19:01 ` Blue Swirl
2009-04-23 19:10 ` Lennart Sorensen
2009-04-23 19:15 ` Glauber Costa
2009-04-23 19:39 ` Blue Swirl
2009-04-23 19:59 ` Anthony Liguori
2009-04-23 20:20 ` Blue Swirl
2009-04-23 19:57 ` Anthony Liguori
2009-04-23 19:59 ` Lennart Sorensen
2009-04-23 20:03 ` Anthony Liguori
2009-04-23 20:54 ` Lennart Sorensen
2009-04-23 21:15 ` Anthony Liguori
2009-04-23 22:13 ` Jamie Lokier
2009-04-24 0:10 ` Anthony Liguori
2009-04-24 8:18 ` Gerd Hoffmann
2009-04-24 12:14 ` Anthony Liguori
2009-04-24 12:32 ` Stefan Weil
2009-04-23 19:31 ` Blue Swirl
2009-04-23 19:44 ` Lennart Sorensen
2009-04-23 22:46 ` Jamie Lokier [this message]
2009-04-24 18:07 ` Lennart Sorensen
2009-04-24 18:58 ` Nathan Froyd
2009-04-23 19:12 ` M. Warner Losh
2009-04-23 19:28 ` Lennart Sorensen
2009-04-23 19:41 ` M. Warner Losh
2009-04-23 19:55 ` Lennart Sorensen
2009-04-23 20:07 ` M. Warner Losh
2009-04-23 21:01 ` Lennart Sorensen
2009-04-23 23:02 ` Jamie Lokier
2009-04-23 22:52 ` Jamie Lokier
2009-04-23 19:37 ` [Qemu-devel] " Jan Kiszka
2009-04-23 19:46 ` Lennart Sorensen
2009-04-23 21:30 ` malc
2009-04-23 22:10 ` Jamie Lokier
2009-04-24 8:09 ` [Qemu-devel] " Gerd Hoffmann
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=20090423224625.GP13326@shareable.org \
--to=jamie@shareable.org \
--cc=blauwirbel@gmail.com \
--cc=lsorense@csclub.uwaterloo.ca \
--cc=qemu-devel@nongnu.org \
/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).