C ++ styleguide: why have non-lvalues ​​on the left side?

In one C ++ Coding Style Guide , I found one recommendation (p. 41, recommendation No. 53):

Always have non-lvalues ​​on the left side ( 0 == i instead of i == 0 ).

And I do not understand why this is useful? Stick to this practice?

I don’t know, and I don’t know why its a good practice. The only advantage I can think of is to avoid erroneous unintended assignment when comparing ( if (foo = 0){} versus if (foo == 0){} )

Do you have other ideas, why should I use it?

+8
c ++ coding-style
source share
2 answers

Yes, you guessed it right. This is a good, old Yoda condition.

enter image description here

+14
source share

As you say, the reason some people use this is to sometimes avoid typing = when they mean == .

Since it only catches some cases when you are comparing lvalue with a constant or rvalue, and every compiler that I know about will warn you if you make this mistake, there is very little point in that.

At least for native English speakers, the code reads as if it were written back; therefore, the "state of Yoda" some call it. Like many of the rules in corporate style guides, this goes back to the time when working with implacable compilers was higher than writing readable code.

+3
source share

All Articles