views:

84

answers:

4

I have three functions that return integer error codes, e.g.

int my_function_1(const int my_int_param);
int my_function_2(const int my_int_param);
int my_function_3(const int my_int_param);

I want to assign and test for error at the same time for brevity. Will the following work and be portable?

int error=0;
...
if ( error ||
     (error = my_function_1(val1) ||
      error = my_function_2(val2) ||
      error = my_function_3(val3)) ) {
   std::cout << "AN ERROR OCCURRED!!!" << std::endl;
}

Thanks!

A: 

error is initialized to 0.So the && will always evaluates to false. So other parts of the if condition are never evaluated. So this code will not work. If you remove the && condition the code should work portably as the standard guarantees the order of the evaluation in this case.

Naveen
Good point. I change this to an or. Sorry about that. I just include that initial error conditional there to test if a previous error occurred. Is the statement now correct?
Jason R. Mick
Yes..it makes sense now..`if` condition will be entered only if one of the method returns a non-zero value.
Naveen
+2  A: 

I don't understand why you have the error && at the beginning of the function, but the rest should do what you want. Short circuit evaluation of the || operators is guaranteed by the standard. I would consider it bad style though.

Edit: Based on your comment, you would need to replace error && with error ||. I will also add that this is a good reason to use exceptions rather than error codes, it makes your code so much easier to read.

Mark Ransom
I have that to short circuit it if a previous error occurred (i.e. if the error occurred, don't make these function calls...). Is that bad style?
Jason R. Mick
@Jason- If someone has to ask why you wrote the code like that, it's more than likely bad style. Well-written code should be easy to read and understand. For that reason, brevity is not always a favorable quality of C++ code. Why not print out the error message inside the functions themselves? If that's the only thing that you are doing differently in the error case, then this code would become much more simple and readable.
bta
No, there's more... I need to short circuit all the later file io logic as well. As to whether its bad style, I essentially taught myself C++ so I imagine my style is quite bad! Any suggested resources? Other than bugging people on stack overflow? :P
Jason R. Mick
+4  A: 

Why not throw an exception?

void my_function_1(const int my_int_param);
void my_function_2(const int my_int_param);
void my_function_3(const int my_int_param);

try {
    my_function_1(...);
    my_function_2(...);
    my_function_3(...);
} catch(std::exception& e) {
    std::cout << "An error occurred! It is " << e.what() << "\n";
}
DeadMG
Where would I throw the exception? By adding a conditional after my function calls? Or inside the functions themselves?
Jason R. Mick
Inside the functions themselves. Exceptions exist because error codes suck. When you encounter an error, you just `throw std::runtime_error("some error string")`. You don't have to deal with error codes or any crap like that.
DeadMG
Good thought. I'll probably revise the code to use exceptions. PS you're one of the more helpful and less snarky commenters here. Some (i.e. Mark Ransom, below) can't seem to respond to earnest questions by self-taught/beginning coders without interjecting sarcasm/snark. Granted I understand people wanting to tell you if you're coding style is poor, but at least explain why. Your informative and straightforward responses are appreciated and hopefully others will follow your leadership...
Jason R. Mick
A: 

Yes, after the minor change of && with || it will work. But it's just too confusing (using = in tests is confusing) with not much benefit.

You could go for exception line another poster suggested, or simply put your checked code inside function and do like below.

int checked(){
    int error = 0;
    error = my_function_1(val1); if (error) return error;
    error = my_function_2(val1); if (error) return error;
    error = my_function_3(val1); if (error) return error;
    return error;
}

I believe any programmer will easily understand what is done here.

kriss