Link to home
Start Free TrialLog in
Avatar of sukhoi35
sukhoi35

asked on

Efficient way of defining error codes

I am currently working on a big existing software architecture in C++. It has error codes in the header file defined something like this:

static const int ERROR_ABC -1;
static const int ERROR_PQR -2;
static const int ERROR_XYZ -3;
.
.
.
.


Since -1, -2, etc values are clashing with some numerical results after the code was modified recently, I have been asked to come up with a more efficient method to handle error codes. Please give your suggestions (preferably with examples) to solve this problem. One idea I got was to change -1, -2 etc in the header file to something bigger like 100001, 100002, etc. Not sure if this is really a good way.
SOLUTION
Avatar of Infinity08
Infinity08
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
what results are they clashing with, and how are those results obtained?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of sukhoi35
sukhoi35

ASKER

Hi Experts,
Thank you very much for all your responses. The main reason for me to do this little refactoring of error handling is that the current code is kind of old and some changes were done to fit a customer requirement.

static const int ERROR_ABC -1;
static const int ERROR_PQR -2;
static const int ERROR_XYZ -3;

void func()
{
           nRet = callFunc();

           if(nRet==ERROR_ABC)
                 return nRet;
          else if(nRet==ERROR_PQR)
                  return nRet;
          else
          return nRet;
}

In the earlier code callFunc() returned a min value of 0 only. Because of some unplanned modifications that were done, now the minimum value that it can return is -1 which incidentally equals ERROR_ABC.

Will award points and close the question soon
The example code you gave does not make much sense. You're always returning nRet, so you might as well have left out all checks :

        int func() {
            return callFunc();
        }

which still seems unnecessary. But I assume a few things got lost when you posted that code here.



>> Because of some unplanned modifications that were done, now the minimum value that it can return is -1

I think the advice already given covers that :)
Yes you are right, I have not really pasted the original code here (since I am not supposed to) but typed it myself in a hurry. so, many things are not there above.

I anyway got the solution, so am closing the question. Thanks Everyone  :)
Thanks!