A different syntax for error checking

R

Richard Bos

=?iso-8859-1?q?Tom=E1s_=D3_h=C9ilidhe?= said:
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
 
T

Tomás Ó hÉilidhe

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.
 
F

fnegroni

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.
 
P

Philip Potter

Tomás Ó hÉilidhe said:
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.
 
T

Tomás Ó hÉilidhe

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);
 
P

Philip Potter

Tomás Ó hÉilidhe said:
Philip Potter:



!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
 
R

Richard Tobin

Tomás Ó hÉilidhe said:
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
 
J

Joseph Huber

Tomás Ó hÉilidhe said:
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 ?
 
F

Flash Gordon

Joseph Huber wrote, On 18/01/08 13:22:
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

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.
 
C

CBFalconer

Tomás Ó hÉilidhe said:
.... 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);
...
 
W

Willem

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top