【问题标题】:C naming suggestion for Error Code enums错误代码枚举的 C 命名建议
【发布时间】:2011-06-09 00:37:15
【问题描述】:

我正在编写一个简单的解析器来读取配置文件。config.h 接口只有三个 它们的主要功能简述如下,

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

我的问题是这些函数会发出不同类型的错误,例如,

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.

一些枚举值相互重叠并且也出现“编译器错误”。像 溢出错误,

我愿意接受你的建议,

我在 google 上做了一个快速的研究,发现最流行的 IRC 客户端 源代码已经定义了这样的枚举,

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 */
};

它定义了没有任何名字的枚举,这是一种很好的风格吗?那为什么是什么原因呢? 那?

真的需要一些更好的命名决策。请不要伤害我我只是 开始阅读“编写漂亮的 C 代码”一书。

提前致谢。 三墩。

【问题讨论】:

    标签: c enums naming


    【解决方案1】:

    我通常喜欢为整个库返回一组错误。这样消费者就不必担心“X 的输入错误是 -1 还是无法连接到 Y”

    我也是E_ 前缀的粉丝,但实际上任何人都会这样做:

    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 enum _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" },
        ....
    };
    

    【讨论】:

      【解决方案2】:

      我认为这是一种很好的风格。 CMDERR_ 前缀将相关的错误代码组合在一起(假设它们与某种“命令调用/执行”有关)

      由于您的所有示例似乎都与您的配置函数相关,因此我将使用一个枚举定义,使用 CONFIG_ 前缀(或 CFG_ 为简洁起见)。

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

      通用前缀背后的原因是,在使用枚举类型时,您希望明确该类型的成员都属于同一个组。

      【讨论】:

        【解决方案3】:

        IRC 客户端源代码中的CMDERR_ 前缀是一个不错的样式,但是定义没有任何名称的枚举不是一个不错的样式。不好,因为你不能说它是枚举类型,只能说是整数类型,如下所示:

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

        你不能使用枚举类型定义变量,如下所示:

        CMDERR errResult;
        
        int errResult; // actually errResult is CMDERR unnamed enum
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-03-05
          • 1970-01-01
          • 2013-11-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多