Google C++ Style Guide

Google C++ Style Guide

Head files

The #define Guard

All header files should have #define guards to prevent multiple inclusion. The format of the symbol name should be H.

#ifndef FOO_BAR_BAZ_H_
#define FOO_BAR_BAZ_H_

...

#endif  // FOO_BAR_BAZ_H_

Include What You Use

If a source or header file refers to a symbol defined elsewhere, the file should directly include a header file which properly intends to provide a declaration or definition of that symbol. It should not include header files for any other reason.
不要依赖传递包含,如果文件main.cc 需要bar.h 中符号,那main.cc 就要包含bar.h, 不管是否有包含的foo.h已经包含了bar.h

inline functions

定义内联函数的条件是函数在十行以下或者更少。
定义内联函数可以产生非常有效的代码,常用的是将存取和设置函数定义为内联函数。

命名规则

文件名

文件名都用小写,可以包含下划线或者短横线。可以接受的文件名如下

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

源文件以.cc 结尾,头文件以.h结尾
Do not use filenames that already exist in /usr/include, such as db.h.
In general, make your filenames very specific. For example, use http_server_logs.h rather than logs.h. A very common case is to have a pair of files called, e.g., foo_bar.h and foo_bar.cc, defining a class called FooBar.

类型名

Type names start with a capital letter and have a capital letter for each new word, with no underscores: MyExcitingClass, MyExcitingEnum.
The names of all types — classes, structs, type aliases, enums, and type template parameters — have the same naming convention. Type names should start with a capital letter and have a capital letter for each new word. No underscores.

// classes and structs
class UrlTable { ...
class UrlTableTester { ...
struct UrlTableProperties { ...

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

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

// enums
enum class UrlTableError { ...

变量名

变量名包括class和struct成员名都是小写,可以包含下划线。

一般变量名
std::string table_name;  // OK - lowercase with underscore.
std::string tableName;   // Bad - mixed case.
类数据成员

Data members of classes, both static and non-static, are named like ordinary nonmember variables, but with a trailing underscore.

class TableInfo {
  ...
 private:
  std::string table_name_;  // OK - underscore at end.
  static Pool<TableInfo>* pool_;  // OK.
};
结构数据成员

Data members of structs, both static and non-static, are named like ordinary nonmember variables. They do not have the trailing underscores that data members in classes have. 结构数据成员命名与一般变量相同。

struct UrlTableProperties {
  std::string name;
  int num_entries;
  static Pool<UrlTableProperties>* pool;
};
常量

常量以k开头,后边跟驼峰命名。下划线可以用,但很少用到。

const int kDaysInAWeek = 7;
const int kAndroid8_0_0 = 24;  // Android 8.0.0
函数名

函数名使用驼峰命名法,存取和设置函数使用普通变量命名。

AddTableEntry()
DeleteUrl()
OpenFileOrDie()
宏命名

c++ 中一般不要使用宏,若要使用的的话,它的命名规则是全部大写,可以使用下划线。

#define ROUND(x) ...
#define PI_ROUNDED 3.0
枚举命名

枚举应该像常量一样命名。不要跟宏一样的命名。

enum class UrlTableError {
  kOk = 0,
  kOutOfMemory,
  kMalformedInput,
};

2009年之前的枚举跟宏命名方式相同,这导致了一些错误。因此枚举使用常量的方法进行。

注释

变量注释

类数据成员注释
private:
 // Used to bounds-check table accesses. -1 means
 // that we don't yet know how many entries the table has.
 int num_total_entries_;
全局变量注释
// The total number of test cases that we run through in this regression test.
const int kNumTestCases = 6;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值