Google Coding Style 重点

C++代码

参见Google cpp style


Background


One way in which we keep the code base manageable is by enforcing consistency

注意一致性




Header Files


The #define Guard

应在文件头中使用

#ifndef FOO_BAR_BAZ_H_
#define FOO_BAR_BAZ_H_
...
#endif  // FOO_BAR_BAZ_H

define guard wiki详解


Header File Dependencies

如果不需要访问某个类的函数,则不要引入这个类


Names and Order of Includes

标准顺序为:

C library, C++ library, other libraries' .h, your project's .h.




Scoping


局部变量


声明注意

声明和定义的写法注意

int i;
i = f();      // Bad -- initialization separate from declaration.
int j = g();  // Good -- declaration has initialization.


声明位置的注意

// Inefficient implementation:
for (int i = 0; i < 1000000; ++i) {
  Foo f;  // My ctor and dtor get called 1000000 times each.
  f.DoSomething(i);
}

Foo f;  // My ctor and dtor get called once each.
for (int i = 0; i < 1000000; ++i) {
  f.DoSomething(i);
}



结构体应尽量少用,只有数据时才用结构体

Use a struct only for passive objects that carry data; everything else is a class.


声明顺序

public: before private:, methods before data members (variables), etc.

start with its public: section, followed by its protected: section and then its private: section.


段内顺序

Typedefs and Enums
Constants (static const data members)
Constructors
Destructor
Methods, including static methods
Data Members (except static const data members)


函数长度应尽量短

 If a function exceeds about 40 lines, think about whether it can be broken up without harming the structure of the program.




命名

Constant Names
Use a k followed by mixed case: kDaysInAWeek.




注释


法律声明和作者信息

版权声明

//Copyright 2008 Google Inc

许可版本

//Apache 2.0, BSD, LGPL, GPL

作者信息

//Original Author
//Other author who change the file

文件内容注释

在法律声明和作者信息下,描述文件主要内容

A .h file will describe the classes that are declared in the file with an overview of what they are for and how they are used.

A .cc file should contain more information about implementation details or discussions of tricky algorithms. 


函数声明内容

What the inputs and outputs are.
For class member functions: whether the object remembers reference arguments beyond the duration of the method call, and whether it will free them or not.
If the function allocates memory that the caller must free.
Whether any of the arguments can be NULL.
If there are any performance implications of how a function is used.
If the function is re-entrant. What are its synchronization assumptions?


如果传空指针,最好加声明或者定义名称时暗示是何意思

bool success = CalculateSomething(interesting_value,
                                  10,
                                  false,
                                  NULL);  // What are these arguments??

versus:

bool success = CalculateSomething(interesting_value,
                                  10,     // Default base value.
                                  false,  // Not the first time we're calling this.
                                  NULL);  // No callback.

Or alternatively, constants or self-describing variables:

const int kDefaultBaseValue = 10;
const bool kFirstTimeCalling = false;
Callback *null_callback = NULL;
bool success = CalculateSomething(interesting_value,
                                  kDefaultBaseValue,
                                  kFirstTimeCalling,
                                  null_callback);


Use TODO comments for code that is temporary, a short-term solution, or good-enough but not perfect.

// TODO(kl@gmail.com): Use a "*" here for concatenation operator.
// TODO(Zeke) change this to use relations.




格式


行长度

Each line of text in your code should be at most 80 characters long.


80 characters is the maximum.


Exception: if a comment line contains an example command or a literal URL longer than 80 characters, that line may be longer than 80 characters for ease of cut and paste.


Exception: an #include statement with a long path may exceed 80 columns. Try to avoid situations where this becomes necessary.


Exception: you needn't be concerned about header guards that exceed the maximum length.


条件语句

This is not allowed when the if statement has an else:

// Not allowed - IF statement on one line when there is an ELSE clause
if (x) DoThis();
else DoThat();

如果有else语句那么if和else必须都添加{},如果没有else ,if语句可不添加{}


switch语句

 If the default case should never execute, simply assert:

switch (var) {
  case 0: {  // 2 space indent
    ...      // 4 space indent
    break;
  }
  case 1: {
    ...
    break;
  }
  default: {
    assert(false);
  }
}

Empty loop bodies should use {} or continue, but not a single semicolon.

while (condition) {
  // Repeat test until it returns false.
}
for (int i = 0; i < kSomeNumber; ++i) {}  // Good - empty body.
while (condition) continue;  // Good - continue indicates no logic.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值