By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,743 Members | 1,058 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,743 IT Pros & Developers. It's quick & easy.

A different syntax for error checking

P: n/a

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
Jan 18 '08 #1
Share this Question
Share on Google+
10 Replies


P: n/a
"=?iso-8859-1?q?Tom=E1s_=D3_h=C9ilidhe?=" <to*@lavabit.comwrote:
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
Jan 18 '08 #2

P: n/a
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.
Jan 18 '08 #3

P: n/a
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.
Jan 18 '08 #4

P: n/a
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
Jan 18 '08 #5

P: n/a
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
Jan 18 '08 #6

P: n/a
In article <Xn***************************@194.125.133.14>,
Tomás Ó hÉilidhe <to*@lavabit.comwrote:
>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
Jan 18 '08 #7

P: n/a
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
Jan 18 '08 #8

P: n/a
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
Jan 18 '08 #9

P: n/a
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

Jan 18 '08 #10

P: n/a
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
Jan 18 '08 #11

This discussion thread is closed

Replies have been disabled for this discussion.