if (f() != FAIL) or if (FAIL != f())?

Discussion in 'C Programming' started by Wenjie, Jul 28, 2003.

  1. Wenjie

    Wenjie Guest

    Hello,


    I have a question concerning style related to conditional
    decision:

    #define FAIL -1
    int f();

    if (f() != FAIL) or better if ( FAIL != f())? Why?


    Thank you!
    Wenjie
    Wenjie, Jul 28, 2003
    #1
    1. Advertising

  2. Wenjie

    Richard Bos Guest

    (Wenjie) wrote:

    > #define FAIL -1
    > int f();
    >
    > if (f() != FAIL) or better if ( FAIL != f())? Why?


    The former, since it is the clearer by far.

    There are people who advocate using the second, because it can prevent
    an error caused by carelessness in completely different circumstances
    which cannot possibly occur here. Ignore them; writing unclear code
    usually produces more bugs than silly tricks like that solve.

    Richard
    Richard Bos, Jul 28, 2003
    #2
    1. Advertising

  3. Wenjie

    Ashish Guest

    "Wenjie" <> wrote in message
    news:...
    > Hello,
    >
    >
    > I have a question concerning style related to conditional
    > decision:
    >
    > #define FAIL -1
    > int f();
    >
    > if (f() != FAIL) or better if ( FAIL != f())? Why?
    >
    >

    The second style is used to avoid the common mistake of assigning to a
    variable instead of comparing.

    For example...
    if(a == 5) could be wrongly written as if(a = 5), which would return true,
    but instead of comparing a with 5, it would assign the value of 5 to a.

    But, if you use the second style, using an if(5 = a) would give you an
    error.

    In your case, it doesnt matter.


    --
    -Ashish
    --------------------------------------------------------------------------
    Hi! I'm a shareware signature! Send $5 if you use me, send $10 for manual!
    http://www.123ashish.com http://www.softwarefreaks.com
    Ashish, Jul 31, 2003
    #3
  4. Wenjie wrote:

    >
    >
    >
    > I have a question concerning style related to conditional
    > decision:
    >
    > #define FAIL -1
    > int f(void);
    >
    > if (f() != FAIL)
    >
    > or better
    >
    > if ( FAIL != f())


    int i = f();
    if (FAIL == i)

    is better than

    int i = f();
    if (i == FAIL)

    because a typical typographical error

    if (i = FAIL)

    will assign i the value of FAIL
    and the conditional will evaluate to true
    even if 0 == f().

    Both of example should cause a diagnostic message
    to be logged by the compiler if you replace == with =
    because both f() and FAIL are right-hand sides.
    E. Robert Tisdale, Jul 31, 2003
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. walala
    Replies:
    4
    Views:
    2,077
    Ralf Hildebrandt
    Sep 8, 2003
  2. Jürgen Exner

    Re: CGI Perl "use CGI" statement fail

    Jürgen Exner, Jul 31, 2003, in forum: Perl
    Replies:
    0
    Views:
    1,215
    Jürgen Exner
    Jul 31, 2003
  3. Jussi Mononen

    Math::Pari test fail on Compaq OSF1 5.1b

    Jussi Mononen, Jan 2, 2004, in forum: Perl
    Replies:
    0
    Views:
    677
    Jussi Mononen
    Jan 2, 2004
  4. Charles Shannon Hendrix

    REGEX: capturing on optional groups which fail

    Charles Shannon Hendrix, Jun 14, 2004, in forum: Perl
    Replies:
    0
    Views:
    542
    Charles Shannon Hendrix
    Jun 14, 2004
  5. Replies:
    0
    Views:
    500
Loading...

Share This Page