Google C++ Style Guide之命名规则

通用命名规则:
    函数名,变量名以及文件名应该是自描述的,应避免使用缩写。类型和变量应使用名词,而函数应包含动词。
    int num_errors;                  // Good.
    int num_completed_connections;   // Good.

    int n;                           // Bad - meaningless.
    int nerr;                        // Bad - ambiguous abbreviation.
    int n_comp_conns;                // Bad - ambiguous abbreviation.

    避免使用缩写,除非它们在你的项目外非常的通用。
    // Good
    // These show proper names with no abbreviations.
    int num_dns_connections;  // Most people know what "DNS" stands for.
    int price_count_reader;   // OK, price count. Makes sense.
    
    // Bad!
    // Abbreviations can be confusing or ambiguous outside a small group.
    int wgc_connections;  // Only your group knows what this stands for.
    int pc_reader;        // Lots of things can be abbreviated "pc".

    int error_count;  // Good.
    int error_cnt;    // Bad.

文件名:
    文件名应该包含小写字母以及下划线(_)或连字符(-)。建议使用下划线。
    以下是可以接受的文件名:
    my_useful_class.cc
    my-useful-class.cc
    myusefulclass.cc
    myusefulclass_test.cc // _unittest and _regtest are deprecated.
    
    内联函数(Inline function)必须包括在.h文件中。
    如果你的内联函数很小,你应该直接写到.h文件中。
    如果你的内联函数包括一定量的代码,则它们应该写到以-inl.h结尾的第三方文件中。
    如果你的类有很多的内联函数,则你的类应该有三个文件:
    url_table.h      // The class declaration.
    url_table.cc     // The class definition.
    url_table-inl.h  // Inline functions that include lots of code.
    
类型名:
    类型名(包括:类,结构体,类型定义,枚举)以大写字母开头且每个新单词都以大写字母开头,不包括下划线。
    // classes and structs
    class UrlTable { ...
    class UrlTableTester { ...
    struct UrlTableProperties { ...

    // typedefs
    typedef hash_map<UrlTableProperties *, string> PropertiesMap;

    // enums
    enum UrlTableErrors { ...
        
变量名:
    变量名只包含小写字母并以下划线分隔每个单词。类成员变量应该以下划线结尾。
    通用变量名:
    string table_name;  // OK - uses underscore.
    string tablename;   // OK - all lowercase.

    string tableName;   // Bad - mixed case.

    类成员变量:
    string table_name_;  // OK - underscore at end.
    string tablename_;   // OK.

    结构体成员变量:为了与类成员变量区分,结构体成员变量不以下划线结尾。
    struct UrlTableProperties {
      string name;
      int num_entries;
    }

    全局变量:与通用变量命名相同,以g_作为前缀。

常量名:
    使用k后面每个单词的首字母大写
    const int kDaysInAWeek = 7;

函数名:
    通用的函数名使用每个单词首字母大写的方式,对类成员变量存取的函数与类成员变量名匹配:MyExcitingFunction(), MyExcitingMethod(), my_exciting_member_variable(),
    set_my_exciting_member_variable().
    通用函数名:
    AddTableEntry()
    DeleteUrl()
    OpenFileOrDie()

    存取变量函数名:
    class MyClass {
     public:
      ...
      int num_entries() const { return num_entries_; }
      void set_num_entries(int num_entries) { num_entries_ = num_entries; }

     private:
      int num_entries_;
    };

命名空间:
    命名空间应都是小写字母且基于项目名称以及存放路径。

枚举命名:
    枚举应于常量或宏定义使用相同的命名方式,即kEnumName或ENUM_NAME
    enum UrlTableErrors {
      kOK = 0,
      kErrorOutOfMemory,
      kErrorMalformedInput,
    };
    enum AlternateUrlTableErrors {
      OK = 0,
      OUT_OF_MEMORY = 1,
      MALFORMED_INPUT = 2,
    };

宏命名:
    通常情况下宏不应被使用。如果真正需要时,要以全部大写以及下划线进行命名。
    #define ROUND(x) ...
    #define PI_ROUNDED 3.0
 
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值