[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: Lennart Sorensen
Subject: Re: [Qemu-devel] [7234] Use a more natural order
Date: Thu, 23 Apr 2009 15:55:53 -0400
User-agent: Mutt/1.5.18 (2008-05-17)

On Thu, Apr 23, 2009 at 01:41:36PM -0600, M. Warner Losh wrote:
> 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.

I wouldn't be surprised if there were languages other than english where
the reverse would be the natural order, so I don't think your argument
is worth much there.

> 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.

The compiler can't guess what your logic should be.  It can tell that
you tried to assign a value to a constant.

> 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.

Then you were fixing the wrong problem.  The problem isn't the order,
but simply that sometimes people get their logic backwards.  The correct
fix would have been:

if (1 > x)

if in fact the logic was backwards.

> I find this argument unpersuasive when the compiler will already warn
> me about if (x = 0).

Some compilers warn you.  Not all do.  An error from all compilers is
also far better than a warning from some compilers.

This is why people writing safety critical code in C require this order.
It is simply the safest choice.

Len Sorensen

reply via email to

[Prev in Thread] Current Thread [Next in Thread]