473,722 Members | 2,295 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Checking return values for errors, a matter of style?

I've written a piece of code that uses sockets a lot (I know that
sockets aren't portable C, this is not a question about sockets per
se). Much of my code ended up looking like this:

Expand|Select|Wrap|Line Numbers
  1. if (function(socket, args) == -1) {
  2. perror("function");
  3. exit(EXIT_FAILURE);
  4. }
I feel that the ifs destroy the readability of my code. Would it be
better to declare an int variable (say succ) and use the following
structure?

Expand|Select|Wrap|Line Numbers
  1. int succ;
  2.  
  3. succ = function(socket, args);
  4. if (succ == -1) {
  5. perror("function");
  6. exit(EXIT_FAILURE);
  7. }
What's considered "best practice" (feel free to substitute with: "what
do most good programmers use")?
Jul 31 '06 #1
66 3622
Johan Tibell said:
I've written a piece of code that uses sockets a lot (I know that
sockets aren't portable C, this is not a question about sockets per
se). Much of my code ended up looking like this:

if (function(socke t, args) == -1) {
perror("functio n");
exit(EXIT_FAILU RE);
}

I feel that the ifs destroy the readability of my code. Would it be
better to declare an int variable (say succ) and use the following
structure?

int succ;

succ = function(socket , args);
if (succ == -1) {
perror("functio n");
exit(EXIT_FAILU RE);
}

What's considered "best practice" (feel free to substitute with: "what
do most good programmers use")?
My normal practice is to use an int to catch the return value so that I can
inspect it. My library routines - at least, the ones that don't return
pointers - return int, with one particular value indicating success, and
any value other than that indicating the reason for the error. There's no
point in my wondering /why/ a function failed if I can't be bothered to
store the error code returned by that function!

But there are times when I use the other way - classic example would be an
fgets loop:

while(fgets(buf fer, sizeof buffer, stdin) != NULL)
{
dosomethingwith (buffer);
}

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Jul 31 '06 #2
Johan Tibell posted:
I've written a piece of code that uses sockets a lot (I know that
sockets aren't portable C, this is not a question about sockets per
se). Much of my code ended up looking like this:

if (function(socke t, args) == -1) {
perror("functio n");
exit(EXIT_FAILU RE);
}

I feel that the ifs destroy the readability of my code. Would it be
better to declare an int variable (say succ) and use the following
structure?

int succ;

succ = function(socket , args);
if (succ == -1) {
perror("functio n");
exit(EXIT_FAILU RE);
}

What's considered "best practice" (feel free to substitute with: "what
do most good programmers use")?

Have you considered something like:

void Assure(int const i)
{
if(-1 == i)
{
perror("functio n");
exit(EXIT_FAILU RE);
}
}

And then putting something like the following in your code:

Assure( function(socket ,args) );

--

Frederick Gotham
Jul 31 '06 #3
Johan Tibell a écrit :
I've written a piece of code that uses sockets a lot (I know that
sockets aren't portable C, this is not a question about sockets per
se). Much of my code ended up looking like this:

if (function(socke t, args) == -1) {
perror("functio n");
exit(EXIT_FAILU RE);
}

I feel that the ifs destroy the readability of my code. Would it be
better to declare an int variable (say succ) and use the following
structure?

int succ;

succ = function(socket , args);
if (succ == -1) {
perror("functio n");
exit(EXIT_FAILU RE);
}

What's considered "best practice" (feel free to substitute with: "what
do most good programmers use")?
Using the second form allows you to easily see the return value in
the debugger. To see the error code using the first form you would
have to follow the machine in machine code and read the return
register... not so easy in many debuggers.

Jul 31 '06 #4


Johan Tibell wrote On 07/31/06 17:20,:
I've written a piece of code that uses sockets a lot (I know that
sockets aren't portable C, this is not a question about sockets per
se). Much of my code ended up looking like this:

if (function(socke t, args) == -1) {
perror("functio n");
exit(EXIT_FAILU RE);
}

I feel that the ifs destroy the readability of my code. Would it be
better to declare an int variable (say succ) and use the following
structure?

int succ;

succ = function(socket , args);
if (succ == -1) {
perror("functio n");
exit(EXIT_FAILU RE);
}

What's considered "best practice" (feel free to substitute with: "what
do most good programmers use")?
I don't see much difference between these two styles.
They take about the same amount of verbiage, and are equally
readable. I think I'd prefer the second style (slightly) if
the returned value has a significance beyond just success vs.
failure or if the function call is long and involved; I'd favor
the first style if the returned value is strictly a yes/no
status and the function call is fairly brief. It's not worth
making a fetish of, though.

If my program had a lot of these and I got tired of
typing the boilerplate over and over (probably misspelling
EXIT_FALIURE a few times), I'd write myself a tiny wrapper
along the lines of

static void crash(const char *message) {
if (message != NULL)
perror (message);
exit (EXIT_FAILURE);
}

Then the "down in the trenches" code becomes

if (function(socke t, args) == -1)
crash ("function") ;

succ = function(socket , args);
if (succ == -1)
crash ("function") ;

Let's see: Expending five lines on the wrapper function saves
me two lines each time I use it, so I'm ahead of the game as
soon as I've checked for my third error ;-) More importantly,
the smaller "footprint" of the error-handling code lets the
reader scan it with perhaps a little more ease.

--
Er*********@sun .com

Jul 31 '06 #5
jacob navia <ja***@jacob.re mcomp.frwrites:
Johan Tibell a écrit :
>I've written a piece of code that uses sockets a lot (I know that
sockets aren't portable C, this is not a question about sockets per
se). Much of my code ended up looking like this:
if (function(socke t, args) == -1) {
perror("functio n");
exit(EXIT_FAILU RE);
}
I feel that the ifs destroy the readability of my code. Would it be
better to declare an int variable (say succ) and use the following
structure?
int succ;
succ = function(socket , args);
if (succ == -1) {
perror("functio n");
exit(EXIT_FAILU RE);
}
What's considered "best practice" (feel free to substitute with:
"what
do most good programmers use")?

Using the second form allows you to easily see the return value in
the debugger. To see the error code using the first form you would
have to follow the machine in machine code and read the return
register... not so easy in many debuggers.
That could be a valid approach, depending on your development style
and environment.

Personally, I don't use debuggers very often, so it usually wouldn't
occur to me to distort my code to make it easier to use in a debugger.
But if you find it useful, go for it.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jul 31 '06 #6
"Johan Tibell" <jo**********@g mail.comwrites:
I've written a piece of code that uses sockets a lot (I know that
sockets aren't portable C, this is not a question about sockets per
se). Much of my code ended up looking like this:

if (function(socke t, args) == -1) {
perror("functio n");
exit(EXIT_FAILU RE);
}

I feel that the ifs destroy the readability of my code. Would it be
better to declare an int variable (say succ) and use the following
structure?

int succ;

succ = function(socket , args);
if (succ == -1) {
perror("functio n");
exit(EXIT_FAILU RE);
}

What's considered "best practice" (feel free to substitute with: "what
do most good programmers use")?
I find both forms about equally readable.

If you're only going to refer to the function's result once, as you do
here, there's no real need to store the value in a variable. If
you're going to refer to it more than once, a variable is helpful,
perhaps essential. For example:

int result;

result = function(socket , args);
if (result != 0) {
fprintf(stderr,
"function returned %d, errno = %d\n",
result,
errno);
exit(EXIT_FAILU RE);
}

But if the function returns only a simple success/failure code, and
the detailed information is elsewhere, this probably isn't necessary.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jul 31 '06 #7
Keith Thompson a écrit :
Personally, I don't use debuggers very often, so it usually wouldn't
occur to me to distort my code to make it easier to use in a debugger.
Ahhh... You do not use debuggers very often?

Mmmm... Well, they are great tools. I use them very often,
actually I spend most of the time either in the editor
or in the debugger.

The only time when I did not use a debugger was when I was writing
the debugger for lcc-win32. My debugger wasn't then able
to debug itself so I had to develop it without any help, what made
things considerably more difficult...

But if you find it useful, go for it.
Yes, I use them very often.

Jul 31 '06 #8
On 2006-07-31, Johan Tibell <jo**********@g mail.comwrote:
I've written a piece of code that uses sockets a lot (I know that
sockets aren't portable C, this is not a question about sockets per
se). Much of my code ended up looking like this:

if (function(socke t, args) == -1) {
perror("functio n");
exit(EXIT_FAILU RE);
}

I feel that the ifs destroy the readability of my code. Would it be
better to declare an int variable (say succ) and use the following
structure?

int succ;

succ = function(socket , args);
if (succ == -1) {
perror("functio n");
exit(EXIT_FAILU RE);
}

What's considered "best practice" (feel free to substitute with: "what
do most good programmers use")?
It really depends on what your target is. In most of my code, I'm
thinking of removing all the checks for stuff like malloc(10) because
there is only a 0.02% chance that I'll be porting my code to a
non-desktop machine where memory is scarce.

OTOH, if you work for a company that deals in all sorts of systems, it
may be worthwhile to have a meaningful error message printed in the case
of problems.

It's not so much a matter of style as it is a matter of practibility:
your code must be robust, but it should also be easy to read. Nowadays
programs targeted at home computers or servers can assume that you'll
have a 99.99% success rate on a functioning system when allocating
memory < 1Kb.

--
Andrew Poelstra <website down>
To reach my email, use <email also down>
New server ETA: 5 minutes ago.
Aug 1 '06 #9
Andrew Poelstra <fa**********@w p.netwrites:
On 2006-07-31, Johan Tibell <jo**********@g mail.comwrote:
[...]
>What's considered "best practice" (feel free to substitute with: "what
do most good programmers use")?

It really depends on what your target is. In most of my code, I'm
thinking of removing all the checks for stuff like malloc(10) because
there is only a 0.02% chance that I'll be porting my code to a
non-desktop machine where memory is scarce.

OTOH, if you work for a company that deals in all sorts of systems, it
may be worthwhile to have a meaningful error message printed in the case
of problems.

It's not so much a matter of style as it is a matter of practibility:
your code must be robust, but it should also be easy to read. Nowadays
programs targeted at home computers or servers can assume that you'll
have a 99.99% success rate on a functioning system when allocating
memory < 1Kb.
Why would you want to settle for 99.99% when you can get 100%?

It takes only one failure to ruin your whole day.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Aug 1 '06 #10

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

Similar topics

16
2630
by: lawrence k | last post by:
I've made it habit to check all returns in my code, and usually, on most projects, I'll have an error function that reports error messages to some central location. I recently worked on a project where someone suggested to me I was spending too much time writing error messages, and that I was therefore missing the benefit of using a scripting language. The idea, apparently, is that the PHP interpreter writes all the error messages that are...
0
8739
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
9088
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8052
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
6681
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
5995
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4502
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4762
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3207
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2147
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.