On Fri, 2016-04-15 at 16:20 +0200, Julia Lawall wrote:
> On Fri, 15 Apr 2016, Christian König wrote:
> > Am 15.04.2016 um 09:15 schrieb Julia Lawall:
> > > Move constants to the right of binary operators.
> > >
> > > Generated by: scripts/coccinelle/misc/compare_const_fl.cocci
> > >
> > > Signed-off-by: Fengguang Wu <fengguang.wu at intel.com>
> > > Signed-off-by: Julia Lawall <julia.lawall at lip6.fr>
> >
> > In general the patch looks ok, but do we have a documented preference where
> > to
> > place constants in the coding style docs?
> >
> > While it's not so much of a problem any more with modern compilers, some
> > people still prefer to have it on the left side to catch accidental value
> > assignments.
>
> I don't know if it is documented. Joe Perches suggested that on the right
> was better in general - maybe he knows if this is written somewhere.
>
> There are 504 occurrences of NULL == in the kernel, and 19524 occurrences
> of == NULL.
A long time ago Linus wrote:
> On Wed, 2004-03-10 at 18:36, Linus Torvalds wrote:
> > > 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.
> >Â
> > Not my experience.  I'd personally prefer a stated preference for.
> >Â
> > (lvalue == rvalue) vs (rvalue == lvalue)
>Â
> The thing is, your "vs" above makes no sense.
>Â
> Quite often, both sides are rvalues, or lvalues. In fact, often you may
> not even know from a simple syntactic look which one either side is, sinceÂ
> they can be macros etc.
>Â
> So asking for consistency is just impossible, because the exact sameÂ
> expression may semantically parse as either depending on things likeÂ
> macros that have architectural dependencies.Â
>Â
> So the rule would have to be something like this:
>Â Â - if one side is _obviously_ a lvalue, and the other side is _obviously_Â
>Â Â Â Â a rvalue, then do X.
>Â
> That kind of rule makes very little sense, but if you want a stated
> preference, then my preference is to say that in that obvious case, the
> lvalue comes on the left side, and the rvalue comes on the right side.
>Â
> Why? Because that is literally how people think. People have been taughtÂ
> since before first grade to think "if I have 8 apples, and I give Tom oneÂ
> apple, how many apples do I have".
>Â
> Notice how I didn't say "if 8 applies is what I have.."
>Â
> The reason for "if (x == 8)" comes from the way we're taught to think.Â
> Arguing against that _fact_ is just totally non-productive, and you haveÂ
> to _force_ yourself to write it the other way around.
>Â
> And that just means that you will do other mistakes. You'll spend yourÂ
> time thinking about trying to express your conditionals in strange ways,Â
> and then not think about the _real_ issue.
>Â
> So let's make a few rules:
>Â
>Â Â - write your logical expressions the way people EXPECT them to beÂ
>Â Â Â Â written. No silly rules that make no sense.
>Â
>Â Â Â Â Ergo:
>Â
>Â Â Â Â Â Â Â Â Â if (x == 8)
>Â
>Â Â Â Â is the ONE AND ONLY SANE WAY.
>Â
>Â Â - avoid using assignment inside logical expressions unless you have aÂ
>Â Â Â Â damn good reason to.
>Â
>Â Â Â Â Ergo: write
>Â
>Â Â Â Â Â Â Â Â Â error = myfunction(xxxx)
>Â Â Â Â Â Â Â Â Â if (error) {
>Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â ...
>Â
>Â Â Â Â instead of writing
>Â
>Â Â Â Â Â Â Â Â Â if (error = myfunction(xxxx))
>Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â ....
>Â
>Â Â Â Â which is just unreadable and stupid.
>Â
>Â Â - Don't get hung about stupid rules.Â
>Â
>Â Â Â Â Ergo: sometimes assignments in conditionals make sense, especially in
>Â Â Â Â loops. Don't avoid them just because of some silly rule. But strive to
>Â Â Â Â use an explicit equality test when you do so:
>Â
>Â Â Â Â Â Â Â Â Â while ((a = function(b)) != 0)Â
>Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â ...
>Â
>Â Â Â Â is fine.
>Â
>Â Â - The compiler warns about the mistakes that remain, if you follow theseÂ
>Â Â Â Â rules.
>Â
>Â Â - mistakes happen. Deal with it. Having tons of rules just makes themÂ
>Â Â Â Â more likely. Expect mistakes, and make sure they are fixed quickly.
>Â
> Is that "stated preference" enough?
>Â