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([email protected]): 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.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章