I am designing a C API which among other things has to provide a way to set some double-valued options. To identify the options I use the following enum:
typedef enum
{
OptionA,
OptionB,
...
} Option;
Is it a good idea to use Option
as a parameter type in a public API function:
int set_option(Option opt, double value);
or is it better to use int
instead:
int set_option(int opt, double value);
considering that I may need to add more options in the future?
Also are there any good examples of existing APIs that demonstrate either approach?
By using an enum
you are effectively helping the user of your function to know what valid values are available.
Of course the drawback is that whenever a new option is added you will need to modify the header and thus the user may need to recompile his code whereas if it is an int
he may not have to do that.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With