473,395 Members | 1,726 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

A different syntax for error checking


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
10 1735
"=?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
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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: George Sakkis | last post by:
I downloaded the latest Komodo (3.1) and configured it for python 2.4 so that it doesn't show decorators and genexps as syntax errors, but background syntax checking doesn't seem to work at all for...
8
by: Clifford W. Racz | last post by:
Does anyone know of a decent tool, preferably free, that will check and validate DTD syntax, to make sure that my DTD is correct? The only tool that I have found thus far is the IBM visual DTD...
12
by: lawrence | last post by:
The following function correctly makes everything invisible but then fails to turn the one chosen DIV back to visible. I imagine I'm getting the syntax of the variable wrong? I've tried this with...
4
by: nonzero | last post by:
I was hoping someone could explain the Syntax of the second argument of the bind() function. bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)); .....................(struct...
1
by: Karim Nassar | last post by:
I am writing functions and I find it curious that CREATE FUNCTION does not do syntax checking. Example: test=# CREATE FUNCTION foo(INTEGER) RETURNS BOOLEAN test-# AS 'this is total crap'...
61
by: phil-news-nospam | last post by:
Why does SVG need a different tag than other images? IMHO, SVG should be implemented as an image type just like any other image type, allowing it to work with <img> tags, and ... here is the...
4
by: FM | last post by:
Hi there: My question is about checking my sql-syntax against DB2UDB V9 throug JDBC 2.0 Is there a way to check my syntax,for example "select * from T1"? Thank you for your help. Regards,
1
by: robtyketto | last post by:
Greetings, When checking the syntax under flash mx it displays error (see below) for the code below (Windows MDM Zinc 2.5):- mdm.Database.MSAccess.runQuery("INSERT INTO...
2
by: Sudhakar | last post by:
until i started using the techniques for avoiding sql injection, i have been using a normal insert and select sql query which worked fine. i have a registration page where a user enters their...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.