In one C++ coding style guide, I found one particular recommendation (page 41, recommendation number 53):
Always have non-lvalues on the left side (
0 == i
instead ofi == 0
).
And I don't uderstand what is this good for? Are to sticking to this practice?
I'm not and I don't know why is his a good practice. The only advantage I can think of is that is will avoid mistaking an unintentional assignment with a comparison (if (foo = 0){}
versus if (foo == 0){}
)
Have you got any other ideas why should I use it?
Yes, you guessed it right. It's the good, old Yoda condition!!!
As you say, the reason some people use it is to occasionally avoid typing =
when they mean ==
.
Since it only catches some cases, where you're comparing a lvalue with a constant or rvalue, and every compiler I know of will warn you if you make that mistake, there's very little point in doing it.
At least to native English speakers, it makes the code read as if it's written backwards; so a "Yoda condition" some call it do. Like many rules in corporate style guides, it dates back to a time when dealing with unforgiving compilers was a higher priority than writing readable code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With