Naming clause for error code enumerations

I am writing a simple parser to read a configuration file. The config.h interface has only three main functions; they are briefly described below.

config_init(); config_dinit(); config_parse(); config_read_value(); 

My question is that these functions will emit different types of errors, for example,

 config_init() emit , FILE_NOT_FOUND,FILE_EOF_ERROR,FILE_OPEN_ERROR, ... config_dinit() emit , NOT_INIT_ERROR , config_parse() emit , PARSE_ERROR, OVERFLOW_ERROR, INVALID_CHARACTER_FOUND_ERROR,... config_read_value() emit, SECTION_NOT_FOUND,KEYWORD_NOT_FOUND,OVERFLOW_ERROR,NOT_INITIALIZED_ERROR,INVALID_STATE_ERROR,... etc. Then I create enums for each function, for by using these names , enum Config_ParseError{...} , enum Config_InitError{...} ,enum Config_ReadValueError{..} etc. 

Some enumeration values โ€‹โ€‹overlap and also remove the "compiler error". like OVERFLOW_ERROR,

I open your suggestions,

and I did a little research on Google and found that the most popular IRC client source code identified listings like this,

 enum { CMDERR_OPTION_UNKNOWN = -3, /* unknown -option */ CMDERR_OPTION_AMBIGUOUS = -2, /* ambiguous -option */ CMDERR_OPTION_ARG_MISSING = -1, /* argument missing for -option */ CMDERR_UNKNOWN, /* unknown command */ CMDERR_AMBIGUOUS, /* ambiguous command */ CMDERR_ERRNO, /* get the error from errno */ CMDERR_NOT_ENOUGH_PARAMS, /* not enough parameters given */ CMDERR_NOT_CONNECTED, /* not connected to server */ CMDERR_NOT_JOINED, /* not joined to any channels in this window */ CMDERR_CHAN_NOT_FOUND, /* channel not found */ CMDERR_CHAN_NOT_SYNCED, /* channel not fully synchronized yet */ CMDERR_ILLEGAL_PROTO, /* requires different chat protocol than the active server */ CMDERR_NOT_GOOD_IDEA, /* not good idea to do, -yes overrides this */ CMDERR_INVALID_TIME, /* invalid time specification */ CMDERR_INVALID_CHARSET, /* invalid charset specification */ CMDERR_EVAL_MAX_RECURSE, /* eval hit recursion limit */ CMDERR_PROGRAM_NOT_FOUND /* program not found */ }; 

it defines an enumeration without any name, is that a good style? Then why what are the reasons for what?

Seriously need some more good naming solutions. Please do not hurt me, I just start reading the book "write a beautiful book C".

Thanks in advance. Sandun.

+6
source share
3 answers

I am usually a fan of one set of errors for the entire library. Thus, consumers do not have to worry about โ€œbeing a -1 failed entry for X or could not connect to Yโ€.

I am also a fan of the E_ prefixes, but in fact everyone will do:

 enum _config_error { E_SUCCESS = 0, E_INVALID_INPUT = -1, E_FILE_NOT_FOUND = -2, /* consider some way of returning the OS error too */ ... }; /* type to provide in your API */ typedef _config_error error_t; /* use this to provide a perror style method to help consumers out */ struct _errordesc { int code; char *message; } errordesc[] = { { E_SUCCESS, "No error" }, { E_INVALID_INPUT, "Invalid input" }, { E_FILE_NOT_FOUND, "File not found" }, ... }; 
+18
source

I think this is a good style. The CMDERR_ prefix combines the associated error codes (provided that they are associated with some kind of "invocation / execution of the command")

Since all your examples seem to be related to your configuration functions, I would just go with one enum definition using the CONFIG_ (or CFG_ for short) prefix.

 enum Config_Errors { CONFIG_FILE_NOT_FOUND, CONFIG_FILE_EOF_ERROR, CONFIG_FILE_OPEN_ERROR, //etc. }; 

The rationale for the general prefix is โ€‹โ€‹that when using an enumerated type, you want to make it clear that members of this type belong to the same group.

+2
source

The CMDERR_ prefix in the IRC client source code is a good style, but defining an enumeration without a name is not a good style . Not good, because you cannot say that it is an enumeration type, only an integer type, as shown below:

 CMDERR function1(); int function1(); // actually returning CMDERR unnamed enum 

and you cannot define a variable using an enum type, as shown below:

 CMDERR errResult; int errResult; // actually errResult is CMDERR unnamed enum 
+2
source

Source: https://habr.com/ru/post/890103/


All Articles