Re: (0 == foo), rather than (foo == 0)

From: Peter Williams
Date: Wed Mar 10 2004 - 22:10:54 EST


Linus Torvalds wrote:

On Thu, 11 Mar 2004, Peter Williams wrote:

Richard B. Johnson wrote:

People who develop kernel code know the difference between
'==' and '=' and are never confused my them.

And you never make typing mistakes? That's admirable or should I say incredible.


The thing is, people tend to make fewer typing mistakes of that kind, than just plain logic errors from not thinking right about something.

And while "0 == foo" may be logically the same thing as "foo == 0", the fact is, the latter is what people are used to seeing. And by being used to seeing it, they have an easier time thinking about it.

As a result, using the former just tends to increase peoples confusion by
making code harder to read, which in turn tends to increase the chance of bugs.

So don't do it. The kind of bug that the "0 == x" syntax protects against
is LESS LIKELY to happen than the kind of bug it tends to cause.

Linus

OK. I'll change all of such occurences in our EBS patches.

As somebody pointed out -Wall will (help) detect most of these errors by suggesting () be placed around any expression of the form a = b that occurs inside a simple boolean expression which will cause those people who care about eliminating warning messages to reevaluate the code and make sure they really meant a = b and replace it with (a = b) to get rid of the warning error.

The reason that I say "most" rather than "all" is (that testing shows) that if the a = b is part of a more complex expression and is already in () in order to (for instance) ensure the desired precedence -Wall will not flag it as a possible problem.

Peter
--
Dr Peter Williams, Chief Scientist peterw@xxxxxxxxxx
Aurema Pty Limited Tel:+61 2 9698 2322
PO Box 305, Strawberry Hills NSW 2012, Australia Fax:+61 2 9699 9174
79 Myrtle Street, Chippendale NSW 2008, Australia http://www.aurema.com


-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/