[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Qemu-devel] [7234] Use a more natural order
From: |
M. Warner Losh |
Subject: |
Re: [Qemu-devel] [7234] Use a more natural order |
Date: |
Thu, 23 Apr 2009 13:41:36 -0600 (MDT) |
In message: <address@hidden>
address@hidden (Lennart Sorensen) writes:
: On Thu, Apr 23, 2009 at 01:12:50PM -0600, M. Warner Losh wrote:
: > In message: <address@hidden>
: > address@hidden (Lennart Sorensen) writes:
: > : On Thu, Apr 23, 2009 at 06:29:47PM +0000, Blue Swirl wrote:
: > : > Revision: 7234
: > : > http://svn.sv.gnu.org/viewvc/?view=rev&root=qemu&revision=7234
: > : > Author: blueswir1
: > : > Date: 2009-04-23 18:29:47 +0000 (Thu, 23 Apr 2009)
: > : > Log Message:
: > : > -----------
: > : > Use a more natural order
: > :
: > : It may be more natural, but it is also less safe.
: > :
: > : After all
: > :
: > : if (0 = x) {
: > :
: > : fails compile, while
: > :
: > : if (x = 0) {
: > :
: > : compiles silently even when you didn't mean that.
: >
: > This style is evil and must die. I don't know any nice way to put
: > it. It encourages sloppiness. Also, it breaks down when you add
: > inequality:
: >
: > if (x < 1)
: >
: > becomes
: >
: > if (1 >= x)
:
: No it doesn't. It becomes:
:
: if (1 > x)
:
: Why would it be anything else?
Hmmm, see how tricky this style is? It is confusing computing the
contrapositive to the expression you want to express. Or rather you
aren't computing the contrapositive here, which is what got me into
trouble. I usually don't make mistakes like this, and I made it in
coming up with the example.
: > which is also error prone.
: >
: > The compiler will warn about your example, but won't warn if I
: > transcribe things wrongly as
: >
: > if (1 < x)
:
: Nothing wrong with that. That's perfectly valid, if you want to check
: that x is greater than 1.
Correct. The compiler doesn't warn you that you've gotten your
backwards flipping around wrong. Which is the argument for this style
when it comes to equality. So you've traded one class of problems for
another. And this class of problem is just as hard to find.
I've fixed several bugs like this over the years from coders that
thought this was a good way to program.
if (1 < x)
rather than
if (x < 1)
is the most common pattern I've had to fix.
: Putting constants first means that you can't accidentally use assignment
: when you meant equality. You can't fix all the stupidies possible in C,
: but you can at least try to avoid some of them when possible.
I find this argument unpersuasive when the compiler will already warn
me about if (x = 0).
Warner
- Re: [Qemu-devel] [7234] Use a more natural order, (continued)
- Re: [Qemu-devel] [7234] Use a more natural order, Blue Swirl, 2009/04/23
- Re: [Qemu-devel] [7234] Use a more natural order, Lennart Sorensen, 2009/04/23
- Re: [Qemu-devel] [7234] Use a more natural order, Jamie Lokier, 2009/04/23
- Re: [Qemu-devel] [7234] Use a more natural order, Lennart Sorensen, 2009/04/24
- Re: [Qemu-devel] [7234] Use a more natural order, Nathan Froyd, 2009/04/24
Re: [Qemu-devel] [7234] Use a more natural order, M. Warner Losh, 2009/04/23
Re: [Qemu-devel] [7234] Use a more natural order, Jamie Lokier, 2009/04/23
Re: [Qemu-devel] [7234] Use a more natural order, Jamie Lokier, 2009/04/23
[Qemu-devel] Re: [7234] Use a more natural order, Jan Kiszka, 2009/04/23
Re: [Qemu-devel] [7234] Use a more natural order, Gerd Hoffmann, 2009/04/24