Thread: is it 10==n or n==10

  1. #1
    char main() RoshanX's Avatar
    Join Date
    Mar 2003
    Posts
    68

    is it 10==n or n==10

    Hi all,
    A small question on C
    What's the difference between 10==n and n==10.
    Which one is better ?. why ?
    Thank you.

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    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;
    }

  3. #3
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    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.

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Originally posted by Thantos
    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.

    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

  5. #5
    Registered User
    Join Date
    Dec 2003
    Posts
    24
    They both do the same,

    But some people use <constant>==<variable> condition because in case you replace == with =, you can easily identify it.

  6. #6
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    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.

  7. #7
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    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.

  8. #8
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    nvoigt is right
    look here

  9. #9
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    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.

  10. #10
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    This has been my new year's coding resolution.

    Code:
    ret = SomeFunction();
    if (ret = VALUE_SUCCESS)
         // continue...
    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.

    So:
    Code:
    if (ERROR_SUCCESS = ret)
    Fortunately, most compilers can be made to warn of assignment in conditional expression.

Popular pages Recent additions subscribe to a feed