Return value

Am I the only one who follows this error code policy?
1
2
3
4
5
6
7
8
9
10
11
bool f(){
	if (there_was_an_error)
		return 0;
	return 1;
}

something_other_than_bool f2(){
	if (there_was_an_error)
		return error_code;
	return 0;
}

I've always liked to use the ! in the condition as a sort of visual cue for alarm:
 
if (!f())


Am I alone on this?
Firstly, for your bool function you should return true/false. Typically false indicates that the function was not successful.

1
2
3
4
5
6
7
bool someFunction() {
 
 if (problem)
  return false;

 return true;
}


If your using another type, it'd typically be an enumerated type telling you how the function completed.

I typically use bool if the function is able to fail gracefully, or I need to be fast. And I use exceptions otherwise to handle errors.
I switch between 0 and 1. I get easily swayed by the last example i saw. I reserve true and false for question type functions e.g. bool isSomethingSet()

I like what you said about using the !.
if (!doSomething() )
error_occured

So i'll stick with that (again i'm being swayed by last example). Thanks for the post.
IMO true/false > 1/0 because 1/0 forces it to be converted to bool (which although it isn't a problem, it could be on a compiler that thinks zero is true or something). And yes, exceptions are generally the best way to go, since they force you to do the error checking, as well as allowing you to save your return value for actual data.
Topic archived. No new replies allowed.