Hi all,
A small question on C
What's the difference between 10==n and n==10.
Which one is better ?. why ?
Thank you.
Hi all,
A small question on C
What's the difference between 10==n and n==10.
Which one is better ?. why ?
Thank you.
They both do the same thing.
Code:#include <cmath> #include <complex> bool euler_flip(bool value) { return std::pow ( std::complex<float>(std::exp(1.0)), std::complex<float>(0, 1) * std::complex<float>(std::atan(1.0) *(1 << (value + 2))) ).real() < 0; }
No they evaulate to the same thing.
However it is standard form to do n==10 since it follows the normal left to right reading method. Since you are more conserned with the value of the variable and not the constant it puts the more important information to the beginning.
Also it helps since assignments have to be done a certain way. n = 10 works but 10 = n does not.
Right, you can't assign a value to a constant. This is why some people write it like that, because during some point they have had problems with it and switch to writing the constant on the left to get compiler errors instead of often hard to track bugs.Originally posted by Thantos
Also it helps since assignments have to be done a certain way. n = 10 works but 10 = n does not.
That having been said, I don't personally do this nor do I know anyone who does. It's just something I heard somewhere.
"...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers
They both do the same,
But some people use <constant>==<variable> condition because in case you replace == with =, you can easily identify it.
I prefer the n == 10 style cause of readability. What you do is check if the variable n equals to 10, NOT if 10 equals to the variable.
MagosX.com
Give a man a fish and you feed him for a day.
Teach a man to fish and you feed him for a lifetime.
Both versions result in the same behaviour and same result.
The human readable version is n == 10 because that is just the way most natural languages are build: "if n is 10".
Due to the nature of the beast, many coding standards define it so that only 10 == n is good practice because this way you cannot make the mistake of omitting the second = as 10 = n would give you a compiler error.
hth
-nv
She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."
When in doubt, read the FAQ.
Then ask a smart question.
nvoigt is right
look here
This is my first time of hearing of people doing this.
I can see the benifits but honestly don't think I'll be switching unless forced to.
This has been my new year's coding resolution.
The problem is that this will typically not cause a bug at development time as SomeFunction() will succeed. Which means that this will blow up at run-time after development is finished. Worse, someone can find input that will make SomeFunction() fail and introduce a security vulnerability into the program.Code:ret = SomeFunction(); if (ret = VALUE_SUCCESS) // continue...
So:
Fortunately, most compilers can be made to warn of assignment in conditional expression.Code:if (ERROR_SUCCESS = ret)