A different syntax for error checking

Discussion in 'C Programming' started by Richard Bos, Jan 18, 2008.

  1. Richard Bos

    Richard Bos Guest

    "=?iso-8859-1?q?Tom=E1s_=D3_h=C9ilidhe?=" <> wrote:

    > Over on comp.lang.c++, we were dicussing the replacment of:
    >
    > if (expr) Func();
    >
    > with:
    >
    > expr && Func();
    >
    > My own initial reaction was that, if I saw the latter in code, I'd say
    > the programmer is playing games trying to impress with funky features.


    So would I. If he wants Perl, he knows where to find it.

    > if (input > 7)
    > {
    > pf = fopen("monkey","r");
    >
    > pf || exit(EXIT_FAILURE);
    > }


    *Brrrrrr*

    Richard
    Richard Bos, Jan 18, 2008
    #1
    1. Advertising

  2. Over on comp.lang.c++, we were dicussing the replacment of:

    if (expr) Func();

    with:

    expr && Func();

    My own initial reaction was that, if I saw the latter in code, I'd say
    the programmer is playing games trying to impress with funky features. I
    stated that "if" has this exact purpose in the language, and it should be
    used for this.

    But then one of the contributors replied saying that they use "if" for
    the normal run of code, but then use && and || for error-checking code.
    Sort of like:

    FILE *pf;

    if (input > 7)
    {
    pf = fopen("monkey","r");

    pf || exit(EXIT_FAILURE);
    }

    To be honest, I quite like the idea. The error-checking code clearly
    stands out from the "normal" code.

    --
    Tomás Ó hÉilidhe
    Tomás Ó hÉilidhe, Jan 18, 2008
    #2
    1. Advertising

  3. Richard Bos

    fnegroni Guest

    I don't like it.
    But each to their own.

    He certainly does not score a point with me if I have to maintain his
    code.
    fnegroni, Jan 18, 2008
    #3
  4. Tomás Ó hÉilidhe wrote:
    > Over on comp.lang.c++, we were dicussing the replacment of:
    >
    > if (expr) Func();
    >
    > with:
    >
    > expr && Func();
    >
    > My own initial reaction was that, if I saw the latter in code, I'd say
    > the programmer is playing games trying to impress with funky features. I
    > stated that "if" has this exact purpose in the language, and it should be
    > used for this.
    >
    > But then one of the contributors replied saying that they use "if" for
    > the normal run of code, but then use && and || for error-checking code.
    > Sort of like:
    >
    > FILE *pf;
    >
    > if (input > 7)
    > {
    > pf = fopen("monkey","r");
    >
    > pf || exit(EXIT_FAILURE);
    > }
    >
    > To be honest, I quite like the idea. The error-checking code clearly
    > stands out from the "normal" code.


    This is a terrible idea in C, for two reasons:

    1) It's not common practise
    2) It doesn't read logically.

    In Perl (where this construct *is* common practise and idiomatic),
    functions generally return a true value for success, so the line:

    dosomething(...) or die "Couldn't open file";

    uses the words "or die" in the same way that English uses them. In C,
    functions generally return 0 for success, so the form in C would be:

    dosomething(...) && myerrorfunc("Couldn't open file\n");

    where the word "and" is used in precisely the opposite way from English.
    fopen() returns a valid pointer for success and NULL for failure, so
    this idiom happens to work for fopen(), but fclose() returns 0 (false)
    for success and EOF (true) for failure, so this technique could never be
    used consistently.
    Philip Potter, Jan 18, 2008
    #4
  5. Philip Potter:

    > In C, functions generally return 0 for success, so the form in C would
    > be:
    >
    > dosomething(...) && myerrorfunc("Couldn't open file\n");



    !strchr("abcdefg",'e') || exit(EXIT_FAILURE);


    I find this use of the inversion operator no more obscure than writing:

    if (!strchr("abcdefg",'e')) exit(EXIT_FAILURE);


    --
    Tomás Ó hÉilidhe
    Tomás Ó hÉilidhe, Jan 18, 2008
    #5
  6. Tomás Ó hÉilidhe wrote:
    > Philip Potter:
    >
    >> In C, functions generally return 0 for success, so the form in C would
    >> be:
    >>
    >> dosomething(...) && myerrorfunc("Couldn't open file\n");

    >
    >
    > !strchr("abcdefg",'e') || exit(EXIT_FAILURE);
    >
    >
    > I find this use of the inversion operator no more obscure than writing:
    >
    > if (!strchr("abcdefg",'e')) exit(EXIT_FAILURE);


    Perhaps in pure terms you are correct. Perhaps if language usage had
    evolved to what you suggest, I wouldn't find it so bad. But the fact is,
    I strongly dislike your version; partly because it starts a statement
    with a negation operator, which doesn't often make sense.

    Phil
    Philip Potter, Jan 18, 2008
    #6
  7. In article <Xns9A29694D8F481toelavabitcom@194.125.133.14>,
    Tomás Ó hÉilidhe <> wrote:

    >Over on comp.lang.c++, we were dicussing the replacment of:
    >
    > if (expr) Func();
    >
    >with:
    >
    > expr && Func();
    >
    > My own initial reaction was that, if I saw the latter in code, I'd say
    >the programmer is playing games trying to impress with funky features. I
    >stated that "if" has this exact purpose in the language, and it should be
    >used for this.


    If you're thinking "if this is true, do that", use if(). If you're
    thinking "do this and do that but stop if the first one fails", &&
    may express the idea more clearly - especially when you extend it
    to "this, that, the other, ...".

    Unfortunately in C the range of things you can use as an expression
    (and interpret as true or false) is limited, so it's hard to use
    the idiom consistently.

    -- Richard
    --
    :wq
    Richard Tobin, Jan 18, 2008
    #7
  8. Richard Bos

    Joseph Huber Guest

    Tomás Ó hÉilidhe wrote:
    > Over on comp.lang.c++, we were dicussing the replacment of:
    >
    > if (expr) Func();
    >
    > with:
    >
    > expr && Func();
    >
    > My own initial reaction was that, if I saw the latter in code, I'd say
    > the programmer is playing games trying to impress with funky features. I
    > stated that "if" has this exact purpose in the language, and it should be
    > used for this.
    >
    > But then one of the contributors replied saying that they use "if" for
    > the normal run of code, but then use && and || for error-checking code.
    > Sort of like:
    >
    > FILE *pf;
    >
    > if (input > 7)
    > {
    > pf = fopen("monkey","r");
    >
    > pf || exit(EXIT_FAILURE);
    > }
    >
    > To be honest, I quite like the idea. The error-checking code clearly
    > stands out from the "normal" code.
    >


    But then, why demonstrate it with an example which does not compile? :

    pf || exit(EXIT_FAILURE);
    ..............^
    %CC-E-NEEDNONVOID, In this statement, "exit(...)" has void type, but
    occurs in a context that requires a non-void result.
    (
    with GCC the error is:
    error: void value not ignored as it ought to be
    )
    In all systems I have access, stdlib.h defines "void exit".

    And (for C, don't know about C+), does the standard forbid to evaluate
    all parts of an expression, even if a part already results in 1, or is
    this implementation defined ?

    --

    Joseph Huber - http://www.huber-joseph.de
    Joseph Huber, Jan 18, 2008
    #8
  9. Richard Bos

    Flash Gordon Guest

    Joseph Huber wrote, On 18/01/08 13:22:
    > Tomás Ó hÉilidhe wrote:
    >> Over on comp.lang.c++, we were dicussing the replacment of:
    >>
    >> if (expr) Func();
    >>
    >> with:
    >>
    >> expr && Func();
    >>
    >> My own initial reaction was that, if I saw the latter in code, I'd
    >> say the programmer is playing games trying to impress with funky
    >> features. I stated that "if" has this exact purpose in the language,
    >> and it should be used for this.
    >>
    >> But then one of the contributors replied saying that they use "if"
    >> for the normal run of code, but then use && and || for error-checking
    >> code. Sort of like:
    >>
    >> FILE *pf;
    >>
    >> if (input > 7)
    >> {
    >> pf = fopen("monkey","r");
    >>
    >> pf || exit(EXIT_FAILURE);
    >> }
    >>
    >> To be honest, I quite like the idea. The error-checking code
    >> clearly stands out from the "normal" code.
    >>

    >
    > But then, why demonstrate it with an example which does not compile? :
    >
    > pf || exit(EXIT_FAILURE);
    > ..............^
    > %CC-E-NEEDNONVOID, In this statement, "exit(...)" has void type, but


    <snip>

    > In all systems I have access, stdlib.h defines "void exit".


    The C standard requires this, and I would assume the C++ standard has
    inherited this requirement. So apart from implementations for embedded
    targets which are not required to provide exit at all you should always
    find it defined as having void as the return type.

    > And (for C, don't know about C+), does the standard forbid to evaluate
    > all parts of an expression, even if a part already results in 1, or is
    > this implementation defined ?


    The C standard (and probably the C++ standard) require short-circuit
    evaluation of the logical operators, i.e. the argument on the right is
    only evaluated if the value is needed to determine the result of the
    operation.
    --
    Flash Gordon
    Flash Gordon, Jan 18, 2008
    #9
  10. Richard Bos

    CBFalconer Guest

    Tomás Ó hÉilidhe wrote:
    >

    .... snip ...
    >
    > But then one of the contributors replied saying that they use
    > "if" for the normal run of code, but then use && and || for
    > error-checking code. Sort of like:
    >
    > FILE *pf;
    >
    > if (input > 7) {
    > pf = fopen("monkey","r");
    > pf || exit(EXIT_FAILURE);
    > }
    >
    > To be honest, I quite like the idea. The error-checking code
    > clearly stands out from the "normal" code.


    How about:

    if (input > 7)
    if (!(pf = fopen("monkey", "r")) exit(EXIT_FAILIRE);
    ...

    --
    [mail]: Chuck F (cbfalconer at maineline dot net)
    [page]: <http://cbfalconer.home.att.net>
    Try the download section.



    --
    Posted via a free Usenet account from http://www.teranews.com
    CBFalconer, Jan 18, 2008
    #10
  11. Richard Bos

    Willem Guest

    Richard wrote:
    ) If you're thinking "if this is true, do that", use if(). If you're
    ) thinking "do this and do that but stop if the first one fails", &&
    ) may express the idea more clearly - especially when you extend it
    ) to "this, that, the other, ...".
    )
    ) Unfortunately in C the range of things you can use as an expression
    ) (and interpret as true or false) is limited, so it's hard to use
    ) the idiom consistently.

    Suppose you have a set of procedures that all do one thing, You make them
    all return true on success, false on failure. Then, if you want to process
    the functions one at a time, you can simply write:

    success = procedure_a()
    && procedure_b()
    && procedure_c();

    instead of the really ugly (IMHO)

    success = procedure_a();
    if (success) success = procedure_b();
    if (success) success = procedure_c();


    SaSW, Willem
    --
    Disclaimer: I am in no way responsible for any of the statements
    made in the above text. For all I know I might be
    drugged or something..
    No I'm not paranoid. You all think I'm paranoid, don't you !
    #EOT
    Willem, Jan 18, 2008
    #11
    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. Clifford W. Racz

    Checking XML DTD syntax and validating XML

    Clifford W. Racz, Jun 7, 2004, in forum: XML
    Replies:
    8
    Views:
    1,625
    Clifford W. Racz
    Jun 23, 2004
  2. Jeff Duffy

    Python switch for syntax checking

    Jeff Duffy, Nov 19, 2004, in forum: Python
    Replies:
    6
    Views:
    546
    Scott David Daniels
    Nov 19, 2004
  3. George Sakkis

    Komodo syntax checking for python2.4

    George Sakkis, Apr 26, 2005, in forum: Python
    Replies:
    2
    Views:
    351
    George Sakkis
    Apr 26, 2005
  4. Good Night Moon
    Replies:
    9
    Views:
    265
    Rick DeNatale
    Jul 25, 2007
  5. Mark Richards
    Replies:
    3
    Views:
    302
    Tad McClellan
    Nov 18, 2007
Loading...

Share This Page