×
׿ǿ Zhuo, Qiang
Hi,
I would like to have someone comments on what's the best practice
defining error codes in C.
Here's what I think:
solution A:
using enum
pros: type safe. better for debug (some debugger will show the name
not only the value)
cons: enum can not be forward declared which makes all error codes
couples together with the error code type ( enum )
Solution B:
using #define
pros: decouple, error codes could be defined in different .h file
cons: macro is bad. no type safe
Solution C:
typedef struct Error
{
int value;
} Error;
static Error const ERROR_OUT_OF_SPACE = { 123 };
pros: type safe. decouple, the error code type is no longer bound with
all the error definition
cons: I don't know any one doing it this way so I'm not sure if it has
some drawbacks or is it bad for (runtime/space) performance.
If using pure C, user could not compare the error value directly but
have to compare the inner "value" member which is not convenience.
Thanks for your help
Qiang
I would like to have someone comments on what's the best practice
defining error codes in C.
Here's what I think:
solution A:
using enum
pros: type safe. better for debug (some debugger will show the name
not only the value)
cons: enum can not be forward declared which makes all error codes
couples together with the error code type ( enum )
Solution B:
using #define
pros: decouple, error codes could be defined in different .h file
cons: macro is bad. no type safe
Solution C:
typedef struct Error
{
int value;
} Error;
static Error const ERROR_OUT_OF_SPACE = { 123 };
pros: type safe. decouple, the error code type is no longer bound with
all the error definition
cons: I don't know any one doing it this way so I'm not sure if it has
some drawbacks or is it bad for (runtime/space) performance.
If using pure C, user could not compare the error value directly but
have to compare the inner "value" member which is not convenience.
Thanks for your help
Qiang