Defining errors and success in functions

  • Thread starter Bilgehan.Balban
  • Start date
B

Bilgehan.Balban

Hi,

Generally in my C code, I use the following convention to return
success or failure:

a) For functions that return a pointer, (allocation, filling-in a
structure etc.) return Null for all errors, and the appropriate pointer
for success.

b) For functions that checks for a condition such as is_working() etc.
obviously 0 for false and 1 for true.

c) For any other functions return 1 for error, 0 for success.

Do you think this is a good approach, or can you suggest any better
approach?

Thanks,
Bahadir
 
A

Antonio Contreras

Hi,

Generally in my C code, I use the following convention to return
success or failure:

a) For functions that return a pointer, (allocation, filling-in a
structure etc.) return Null for all errors, and the appropriate pointer
for success.

b) For functions that checks for a condition such as is_working() etc.
obviously 0 for false and 1 for true.

c) For any other functions return 1 for error, 0 for success.

Do you think this is a good approach, or can you suggest any better
approach?

Slightly OT, but...

As long as there are comments in the code explaining what do the
various return codes mean you can use any convention you want. Whatever
convention you use, use it consistently.

Cases a) and b) are pretty standard. a) is the behaviour of many
standard library functions and b) leads to more human-readable code.

For case c) I've seen more often 0 for success and -1 for error.

Anyway, it's good practice to #define all possible return values so the
code is basically independent of whatever convention you use and easier
to read. I generally use 0 for success and negative numbers for
whatever error conditions I want to signal, with only one exception.
Functions whose porpouse is to move pieces of data of write to a buffer
usually return the number of characters / words written. In this case
(obviously) 0 means error.

HTH
 
K

Kenneth Brody

Hi,

Generally in my C code, I use the following convention to return
success or failure:

a) For functions that return a pointer, (allocation, filling-in a
structure etc.) return Null for all errors, and the appropriate pointer
for success.

b) For functions that checks for a condition such as is_working() etc.
obviously 0 for false and 1 for true.

c) For any other functions return 1 for error, 0 for success.

Do you think this is a good approach, or can you suggest any better
approach?

While those cover the basics, what about functions that need to return
a number, rather than just success/fail? For example, a function to
send data to a device might return the number of bytes written. In
cases like these, it is not uncommon for "-1" to mean error. Of course,
there are cases where negative numbers may be valid return values, so
this can't be a universal rule.

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+
Don't e-mail me at: <mailto:[email protected]>
 
P

pete

Kenneth said:
(e-mail address removed) wrote:

While those cover the basics, what about functions that need to return
a number, rather than just success/fail?

0 instead of 1 for success,
is best when there are a number of returnable failure codes.
 
J

Jack Klein

Hi,

Generally in my C code, I use the following convention to return
success or failure:

a) For functions that return a pointer, (allocation, filling-in a
structure etc.) return Null for all errors, and the appropriate pointer
for success.
OK.

b) For functions that checks for a condition such as is_working() etc.
obviously 0 for false and 1 for true.
OK.

c) For any other functions return 1 for error, 0 for success.

This on is generally OK for small programs, not so good for larger
ones. For a function to succeed, all the operations it has to perform
must succeed. But if the function fails, there may be more than one
possible cause.
Do you think this is a good approach, or can you suggest any better
approach?

Thanks,
Bahadir

In a small program it might not make much difference, all failures are
treated the same. In a larger program, one might want to at least
provide some indication to the user why the program failed, or write
an entry to a log file.

A common way to do this is something like:

enum
{
success,
allocation_failure,
file_open_error,
file_read_error,
file_create_error,
file_write_error,
parameter_invalid,

/* and so on */
} result_type;

Your functions are defined as:

result_type some_function( /* arguments */ )
{ /* body of function */ }

....and each function provides a specific value indicating that it
succeeded (note that 'success' is 0), or why it failed.
 

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

No members online now.

Forum statistics

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

Latest Threads

Top