Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > returned error code

Reply
Thread Tools

returned error code

 
 
iu2
Guest
Posts: n/a
 
      07-02-2007
Hi all,
I'm interested in your experience with returned error codes:
What's better, return true/false for success/fail, or return 0 for Ok
and a negative for failure?
This question arose since in my project we use both systems, which led
to a bug of misinterpreting returned code (a function retured 0 for
success but the programmer treated it as false).

The 0/negative system has been suggested to allow a range of failure
codes. But practically we almost don't analyze them. Feeling that I
shoud be thankful for handling and reporting error at all, I thought
of getting rid of 0/negative system and work only with true/false.
This also produces shorter code:
"if (SomeFunc())" instead of "if (SomFunc() == 0)" (or even worse,
due to tidy programming
"if (SomeFunc() == code_ok)").

What do you say?
Thanks

 
Reply With Quote
 
 
 
 
FabioAng
Guest
Posts: n/a
 
      07-02-2007

"iu2" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com...
> Hi all,
> I'm interested in your experience with returned error codes:
> ..., or return 0 for Ok and a negative for failure?


That's the Unix sytle and I think it's a valid one.

> This question arose since in my project we use both systems, which led
> to a bug of misinterpreting returned code (a function retured 0 for
> success but the programmer treated it as false).
>
> The 0/negative system has been suggested to allow a range of failure
> codes. But practically we almost don't analyze them. Feeling that I
> shoud be thankful for handling and reporting error at all, I thought
> of getting rid of 0/negative system and work only with true/false.


Why don't you want to use exceptions ?
http://www.boost.org/more/generic_exception_safety.html

Fabio


 
Reply With Quote
 
 
 
 
James Kanze
Guest
Posts: n/a
 
      07-02-2007
On Jul 2, 9:51 am, iu2 <(E-Mail Removed)> wrote:

> I'm interested in your experience with returned error codes:
> What's better, return true/false for success/fail, or return 0 for Ok
> and a negative for failure?


Neither. You really need to use an enum:

enum ErrorStatus
{
ok,
someError,
someOtherError
} ;

> This question arose since in my project we use both systems, which led
> to a bug of misinterpreting returned code (a function retured 0 for
> success but the programmer treated it as false).


Precisely. The same problem exists for true/false: does true
mean success, or failure. If you have to compare with a
symbolic constant, like ok, the problem can't occur.

> The 0/negative system has been suggested to allow a range of failure
> codes. But practically we almost don't analyze them. Feeling that I
> shoud be thankful for handling and reporting error at all, I thought
> of getting rid of 0/negative system and work only with true/false.
> This also produces shorter code:
> "if (SomeFunc())" instead of "if (SomFunc() == 0)" (or even worse,
> due to tidy programming
> "if (SomeFunc() == code_ok)").


Shorter doesn't always mean more readable or more
understandable. This is one case where longer is better.

--
James Kanze (GABI Software) email:(E-Mail Removed)
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

 
Reply With Quote
 
iu2
Guest
Posts: n/a
 
      07-02-2007
On Jul 2, 10:13 am, "FabioAng" <(E-Mail Removed)> wrote:
> "iu2" <(E-Mail Removed)> wrote in message
>
> news:(E-Mail Removed) ups.com...
>
> > Hi all,
> > I'm interested in your experience with returned error codes:
> > ..., or return 0 for Ok and a negative for failure?

>
> That's the Unix sytle and I think it's a valid one.
>
> > This question arose since in my project we use both systems, which led
> > to a bug of misinterpreting returned code (a function retured 0 for
> > success but the programmer treated it as false).

>
> > The 0/negative system has been suggested to allow a range of failure
> > codes. But practically we almost don't analyze them. Feeling that I
> > shoud be thankful for handling and reporting error at all, I thought
> > of getting rid of 0/negative system and work only with true/false.

>
> Why don't you want to use exceptions ?http://www.boost.org/more/generic_exception_safety.html
>
> Fabio


I'd like to but:
1. It's too late for my project now...
2. In some cases I don't want an exception. For example something
fails, but I want to respond only after x successive failures. Then
each single failure should realy return an error code (I think) and
somewhere higher in the calling path I count the failures.

iu2

 
Reply With Quote
 
Roland Pibinger
Guest
Posts: n/a
 
      07-02-2007
On Mon, 02 Jul 2007 02:41:45 -0700, James Kanze wrote:
>On Jul 2, 9:51 am, iu2 <(E-Mail Removed)> wrote:
>> I'm interested in your experience with returned error codes:
>> What's better, return true/false for success/fail, or return 0 for Ok
>> and a negative for failure?
>> This question arose since in my project we use both systems, which led
>> to a bug of misinterpreting returned code (a function retured 0 for
>> success but the programmer treated it as false).

>
>Precisely. The same problem exists for true/false: does true
>mean success, or failure. If you have to compare with a
>symbolic constant, like ok, the problem can't occur.


Naming conventions are helpful in such cases. Functions that start
with is... or has... are supposed to answer a question, e.g.

bool isConnected (DBConnection& dbc);


--
Roland Pibinger
"The best software is simple, elegant, and full of drama" - Grady Booch
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      07-03-2007
On Jul 2, 2:45 pm, (E-Mail Removed) (Roland Pibinger) wrote:
> On Mon, 02 Jul 2007 02:41:45 -0700, James Kanze wrote:
> >On Jul 2, 9:51 am, iu2 <(E-Mail Removed)> wrote:
> >> I'm interested in your experience with returned error codes:
> >> What's better, return true/false for success/fail, or return 0 for Ok
> >> and a negative for failure?
> >> This question arose since in my project we use both systems, which led
> >> to a bug of misinterpreting returned code (a function retured 0 for
> >> success but the programmer treated it as false).


> >Precisely. The same problem exists for true/false: does true
> >mean success, or failure. If you have to compare with a
> >symbolic constant, like ok, the problem can't occur.


> Naming conventions are helpful in such cases. Functions that start
> with is... or has... are supposed to answer a question, e.g.


> bool isConnected (DBConnection& dbc);


That's a different issue. I wouldn't say that a function like
"isConnected" failed. It returns status information. (Almost
by definition, a function whose name starts with "is" returns a
bool.) On the other hand, if the function name is
"isConnected", it should almost certainly be const, and not try
to establish a connection (which might fail). And the meaning
of a bool return value of a function named "establishConnection"
is ambiguous.

--
James Kanze (GABI Software) email:(E-Mail Removed)
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
System.Net.WebException: The remote server returned an error: (500) Internal Server Error. kuladeep.mohan@gmail.com ASP .Net 5 08-26-2009 09:36 AM
FxCopCmd.exe returned error code 8. Mukesh ASP .Net 4 12-25-2006 08:37 AM
HttpWebRequest is giving: "Error on HTTP request: The remote server returned an error: (403) Forbidden." edsuslen@yahoo.com ASP .Net 2 09-27-2005 12:17 PM
A tool returned an error code from "Performing registration" error Wes Stebbins ASP .Net 1 07-15-2005 08:05 PM
WebRequest Error, the remote server returned an error: (401) obeOnline ASP .Net 2 01-27-2005 03:29 PM



Advertisments