google c++ style by examples

详细参考 https://google.github.io/styleguide/cppguide.html

文件名

my_useful_class.cc
my-useful-class.cc
myusefulclass.cc
myusefulclass_test.cc // _unittest and _regtest are  deprecated.

类名

class UrlTable { ...
class UrlTableTester { ...
struct UrlTableProperties { ...

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

// using aliases
using PropertiesMap = hash_map<UrlTableProperties *, string>;

// enums
enum UrlTableErrors { ...

变量名

int a_local_variable;
int a_struct_data_member;
int a_class_data_member_;
string tableName; // 不好

成员变量

class TableInfo {
  ...
 private:
  string table_name_;  // OK - underscore at end.
  string tablename_;   // OK.
  static Pool<TableInfo>* pool_;  // OK.
};
struct UrlTableProperties {
  string name;
  int num_entries;
  static Pool<UrlTableProperties>* pool;
};

常量

const int kDaysInAWeek = 7;

函数名

  • good

    void StartRpc() {}
    
  • bad

    void StartRpc() {}
    

缩写不需要