原文:
http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml#Structs_vs._Classes
有關struct和class之間的觀念,成大SCREAM Lab的這篇文章寫的很好:
http://screamlab-ncku-2008.blogspot.tw/2009/10/c-struct-class-keywords.html
coherence 發表在
痞客邦
留言(0)
人氣()
原文:
http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml#Nonmember,_Static_Member,_and_Global_Functions
在軟體開發的過程中,有些時候有必要定義一個不和任何class instance有關聯的非class member function。此時我們應盡量少用global function,如果真的非要使用不可,應該將之放在namespace裡面,或是使用class的static member function作為替代方案。
因為使用completely global function(指未放在namespace裡的global function)會汙染全域的命名空間(global namespace)。比方說你自定義了一個global function名為sort(),而你#include進來的3rd party library中也定義了另一個sort(),此時就會造成命名碰撞的問題。
coherence 發表在
痞客邦
留言(0)
人氣()
原文:
http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml#Write_Short_Functions
寫程式時很多時候都在追求所謂的code reuse,code的reusability越高代表程式中的duplicate code越少,而duplicate code常是bug的根源之一。回顧語言的發展,像是繼承(inheritance)、多型(polymorphism)、模板(template)...等language features的產生,design pattern、STL的使用,都提高了code的reusability。
傾向撰寫短小的函式,也是提高code reusability的方法之一。Google C++ Style Guide建議當一個函式超過40行(也有其他文獻建議是75行),你可以考慮在不影響程式結構的情況下拆解該函數,使之成為若干個小函數。當然40這個數字並不是個硬性規定,只是個參考值。
coherence 發表在
痞客邦
留言(0)
人氣()
原文:
http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml#Explicit_Constructors
當一個class的ctor只有一個parameter時,例如:
class CFoo
{
public:
CFoo(int n)
{
}
};
此時隱式轉換(implicit conversion)可能在下列情況產生:
void Bar(CFoo f)
{
}
void main()
{
int n = 15;
CFoo foo(10);
foo = n;
Bar(13);
}
以上這段程式可以通過編譯,不會出現Error。透過隱式轉換可以把整數n賦值給foo,也可以把13傳給函式Bar(),這樣的隱式轉換雖然很方便,但是卻是錯誤的根源;programmer可能只是一時不察,無意作這樣的轉換,但是這樣的隱式轉換卻不會出現任何Error,有可能會因此造成bug。
coherence 發表在
痞客邦
留言(0)
人氣()
原文:
http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml#Doing_Work_in_Constructors
在ctor中只作class data member的賦值,或一些簡單的初始化動作;避免去做太複雜的初始化動作。若有複雜的初始化動作要完成,應該另外開一個public的member function Init(),再將複雜的初始化放在Init()裡。
雖然在ctor中作初始化很方便,但是會造成很多問題:
1. ctor沒有return value,難以用簡單的方式回傳ctor中產生的錯誤,雖然可以透過throw exception將錯誤傳出去,但是Google C++ Style Guide禁止使用exception。
2. ctor中如果出現一個不至於造成crash的錯誤,此時程式雖然沒有終止,卻處在一個錯誤的state。
3. 再ctor中呼叫virtual function會出問題,virtual function call不會呼叫到subclass中的virtual function,因為此時subclass尚未被建構出來。
4. 如果該class的instance為一個global variable(不建議使用global variable,但是還是有可能會存在global variable),此時該class的ctor會再main()之前被呼叫,有可能會破壞ctor code中的一些假設。
coherence 發表在
痞客邦
留言(0)
人氣()
原文:
http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml#Names_and_Order_of_Includes
C\C++在Compile時,是以每個.cpp file為單位(稱為translation unit),透過預處理指令#include把所有需要的symbol都引進來;預處理指令#include "Foo.h",只是單純的把header file Foo.h中的內容,原封不動地貼過來而以。
每個translation unit會被compile成一個.obj檔(以binary型式存在),再透過linker收集互相參考的binary code(有可能是其他的.obj檔,或是另外的.lib檔),並填上參考的位址,最後將所有的binary code合併為.exe檔。
規定#include header file的順序,可以增加可讀性。假若你有一個foo.cpp,Google C++ Style Guide規定#include的順序為以下幾個段落:
1. foo.h
2. C system files
3. C++ system files
4. Other libraries' .h files
5. Your project's .h files
這樣的好處是,當你的foo.h在編譯階段出現錯誤時,會馬上停止編譯,並顯示Error Message,而不會讓你誤以為錯誤是由其他無辜的header file所產生。
coherence 發表在
痞客邦
留言(0)
人氣()
原文:
http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml#Local_Variables
變數的scope越小越好。若scope越大,意味著越多地方可以修改到這個變數;也意味著此變數造成的bug影響的範圍也會越大。
coherence 發表在
痞客邦
留言(0)
人氣()
原文:
http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml#Forward_Declarations
可使用forward declaration來宣告classes,以避免不必要的#include。
(Note: 你可以forward declare一個class、function或template,但Google Style Guide只建議在必要時,使用forward declaration來宣告classes)
coherence 發表在
痞客邦
留言(0)
人氣()
原文:
http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml#Function_Parameter_Ordering
規定函數參數的順序: 先input後output
這樣可提升程式的可讀性;未來新增函數的參數時,要記得不要直接把新的參數加在函數的最後面。
Google Style Guide也規定,output的參數必須為non-const的指標(Google並不建議使用reference作為函數的output參數)
這個規定並沒有要求你一定得遵守它,因為有些參數既是input也是output,所以必須視情況來調整這項規定。
coherence 發表在
痞客邦
留言(0)
人氣()
原文:
http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml#The__define_Guard
旨在防止multiple inclusion的問題,當然使用#pragma once預處理指令也可以得到相同的效果,但是#pragma once是compiler related;而#define Guard是所有compiler都可以使用。
為了保證#define Guard不會重覆,#define Guard由該header file的full path所組成。
Google定義#define Guard的格式為:
<PROJECT>_<PATH>_<FILE>_H_
coherence 發表在
痞客邦
留言(0)
人氣()