473,669 Members | 2,371 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_FAILU RE);
}

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 1759
"=?iso-8859-1?q?Tom=E1s_=D3 _h=C9ilidhe?=" <to*@lavabit.co mwrote:
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_FAILU RE);
}
*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_FAILU RE);
}

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("Co uldn'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("Co uldn't open file\n");

!strchr("abcdef g",'e') || exit(EXIT_FAILU RE);
I find this use of the inversion operator no more obscure than writing:

if (!strchr("abcde fg",'e')) exit(EXIT_FAILU RE);
--
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("Co uldn't open file\n");


!strchr("abcdef g",'e') || exit(EXIT_FAILU RE);
I find this use of the inversion operator no more obscure than writing:

if (!strchr("abcde fg",'e')) exit(EXIT_FAILU RE);
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.co mwrote:
>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_FAILU RE);
}

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_FAILU RE);
..............^
%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_FAILU RE);
}

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_FAILU RE);
..............^
%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_FAILU RE);
}

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_FAILI RE);
...

--
[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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2049
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 python 2.4. Even for correct files, it shows a "Syntax checking error: Error checking syntax: retval = 128, stderr=" alert. For python 2.3 it works fine (modulo the new 2.4 syntax of course). Both 2.3 and 2.4 are built from source on Cygwin, not...
8
10334
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 tool, which I do like, but is limited. Basically, your DTD is "good" when it parses... and I am not sure this is the best way to do it. As for validation of XML, I have found a great tool, xmlvalid, here: <...
12
3904
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 both single quotes and no single quotes, but it doesn't work either way. What am I doing wrong? <SCRIPT type='text/javascript'> function makeVisible(nameOfDiv) { document.getElementById('weblogs').style.visibility='hidden';
4
3424
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 sockaddr *)&my_addr.................................... I understand that it is converting the structure at &my_addr to a structure type sockaddr. What I don't understand is the syntax. I don't understand
1
3303
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' LANGUAGE plpgsql; CREATE FUNCTION test=# select foo(1); ERROR: syntax error at or near "this"
61
4724
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 important part ... also work with backgrounds in other tags. I fail to see any wisdom in making SVG different than say PNG (of course the implementation of the rendering code would obvious be different). --
4
10624
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
2636
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 Results(Name,Results,ResultsDate) VALUES("Rob", 50,'"+ Date() +'" )");
2
2097
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 username and if this already exists i display a message by executing a select query and if the username does not exist then i run an insert query. after adopting the technique to avoid sql injection
0
8382
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8893
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8802
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8586
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7405
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6209
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4384
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2028
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1787
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.