本文最新位址:http://exbrowser.com/?p=501
1. WebKit代碼規範
1.1 縮進
1. 縮進用空格不采用Tab。Tab字元僅僅出現在根據語義環境必須的情況下,如Makefile。
2. 一般縮進4個空格。
正确:
int main()
{
return 0;
}
錯誤:
int main()
{
return 0;
}
3. 在頭檔案中,在命名空間内的代碼應該縮進。
正确:
// Document.h
namespace WebCore {
class Document {
Document();
...
};
} // namespace WebCore
錯誤:
// Document.h
namespace WebCore {
class Document {
Document();
...
};
} // namespace WebCore
4. 在實作檔案(已.cpp, .c 或者.mm為擴充名的檔案)中包含在命名空間中的代碼不需要縮進。
正确:
// Document.cpp
namespace WebCore {
Document::Document()
{
...
}
} // namespace WebCore
錯誤:
// Document.cpp
namespace WebCore {
Document::Document()
{
...
}
} // namespace WebCore
5. case語句與switch語句左對齊,不需要縮進。
正确:
switch (condition) {
case fooCondition:
case barCondition:
i++;
break;
default:
i--;
}
錯誤:
switch (condition) {
case fooCondition:
case barCondition:
i++;
break;
default:
i--;
}
6. 相同嵌套層次的布爾表達式用多行表示時,應将運算符放在行的左側而不是右側。
正确:
return attr->name()== srcAttr
|| attr->name() == lowsrcAttr
|| (attr->name() == usemapAttr);
錯誤:
return attr->name()== srcAttr ||
attr->name() == lowsrcAttr ||
(attr->name() == usemapAttr);
1.2 空格
1. 不要在一進制運算符周圍加空格。
正确:
i++;
錯誤:
i ++;
2. 二進制運算符和三元運算符周圍加空格。
正确:
y = m * x + b;
f(a, b);
c = a | b;
return condition ? 1 :0;
錯誤:
y=m*x+b;
f(a,b);
c = a|b;
return condition ? 1:0;
3. 調價語句和括号之間加空格。
正确:
if (condition)
doIt();
錯誤:
if(condition)
doIt();
4. 不要在函數名和括号之間以及括号和内容之間加空格。
正确:
f(a, b);
錯誤:
f (a, b);
f( a, b );
1.3 換行
1. 每條語句應獨占一行。
正确:
x++;
y++;
if (condition)
doIt();
錯誤:
x++; y++;
if (condition) doIt();
2. 每條else語句應該和前一個大括号在一行。
正确:
if (condition) {
...
} else {
...
}
錯誤:
if (condition) {
...
}
else {
...
}
3. 目前一個if語句塊中包含return語句,則後續的else if語句應該當做if語句書寫。
正确:
if (condition) {
...
return someValue;
}
if (condition) {
...
}
錯誤:
if (condition) {
...
return someValue;
} else if (condition) {
...
}
1.4 括号
1. 函數定義:大括号應該單獨占一行。
正确:
int main()
{
...
}
錯誤:
int main() {
...
}
2. 其他大括号:在語句塊的換行前加起始的大括号;結束大括号放在單獨一行。
正确:
class MyClass {
...
};
namespace WebCore {
...
}
for (int i = 0; i <10; i++) {
...
}
錯誤:
class MyClass
{
...
};
3. 隻有一行的條件語句不要使用大括号。
正确:
if (condition)
doIt();
錯誤:
if (condition) {
doIt();
}
4. 沒有語句塊的條件語句采用空的大括号。
正确:
for ( ; current; current= current->next) { }
錯誤:
for ( ; current; current= current->next);
1.5 其他nullfalse和0
1. C++語言中空指針應當書寫為0。C語言中應當作為書寫成NULL。Objective-C和Objective-C++也分别遵循C和C++規則,但是nil表示空的Objective-C對象;
2. C++和C中布爾變量的值應當書寫成true和false。Objective-C中BOOL值應當是YES和NO。
3. 判斷true/false,null/non-null和zero/non-zero應當不需要等效的比較操作符。
正确:
if (condition)
doIt();
if (!ptr)
return;
if (!count)
return;
錯誤:
if (condition == true)
doIt();
if (ptr == NULL)
return;
if (count == 0)
return;
4. 在Objective-C中,執行個體變量自動被初始化為0。在初始化方法ini中,不必顯示地初始化成nil或者NO。
1.6 命名
1. 采用駱駝命名法(CamelCase)。對于所有的類、結構體、協定或者命名空間的名稱都大寫首字母;所有的變量和函數名都小寫首字母。
正确:
struct Data;
size_t bufferSize;
class HTMLDocument;
String mimeType();
錯誤:
struct data;
size_t buffer_size;
class HtmlDocument;
String MIMEType();
2. 除了少數縮寫更加典型和更加易于了解以外,名稱都采用全詞;
正确:
size_t characterSize;
size_t length;
short tabIndex; // morecanonical
錯誤:
size_t charSize;
size_t len;
short tabulationIndex;// bizarre
3. 類的資料成員的字首采用m_ ;
正确:
class String {
...
short m_length;
};
錯誤:
class String {
...
short length;
};
4. Object-C執行個體變量的字首采用 _ ;
正确:
@class String
...
short _length;
@end
錯誤:
@class String
...
short length;
@end
5. 布爾變量用is和did作為字首;
正确:
bool isValid;
bool didSendData;
錯誤:
bool valid;
bool sentData;
6. 對于資料成員的setter前加set作為字首,但是getter前不需要加任何字首,而且setter和getter名稱應該比對被操作的變量名;
正确:
void setCount(size_t);// sets m_count
size_t count(); //returns m_count
錯誤:
void setCount(size_t);// sets m_theCount
size_t getCount();
7. 函數名裡采用描述動作的動詞;
正确:
bool convertToASCII(short*,size_t);
錯誤:
bool toASCII(short*,size_t);
8. 函數聲明中去掉無意義的變量名稱;
正确:
void setCount(size_t);
錯誤:
void setCount(size_tcount);
9. Objective-C方法名采用Cocoa命名樣式—讀起來像一個句子,以小寫字母開始,以後的每個詞的首字母大寫。
10. 枚舉類型的成員應該将每個詞的首字母大寫。
11. 用const修飾#define定義的常量和用inline函數代替宏代碼更好。
12. #define定義的常量應該全部大寫所有字元,每個詞之間用下劃線分割。
13. 展開為函數調用或者廢常量的代碼的宏的命名要像函數一樣,即使沒有任何參數,名稱末尾需要帶有小括号(特殊情況是ASSERT宏)。注意采用inline函數代替宏更為合理。
正确:
#defineWBStopButtonTitle() /
NSLocalizedString(@"Stop",@"Stop button title")
錯誤:
#defineWB_STOP_BUTTON_TITLE /
NSLocalizedString(@"Stop",@"Stop button title")
#defineWBStopButtontitle /
NSLocalizedString(@"Stop",@"Stop button title")
14. 為了防止重複包含的采用的#define, #ifdef等預處理指令中的宏的名稱必須和頭檔案名完全一緻,并且用下劃線代替逗号。
正确:
// HTMLDocument.h
#ifndef HTMLDocument_h
#define HTMLDocument_h
錯誤:
// HTMLDocument.h
#ifndef_HTML_DOCUMENT_H_
#define_HTML_DOCUMENT_H_
1.7 其他标點
1. C++類型的構造函數中需要按照C++初始化文法初始化其所有成員。每個成員(和其超類)需要縮進一行,并且将冒号和逗号作為在行的起始。
正确:
MyClass::MyClass(Document*doc)
: MySuperClass()
, m_myMember(0)
, m_doc(doc)
{
}
MyOtherClass::MyOtherClass()
: MySuperClass()
{
}
錯誤:
MyClass::MyClass(Document*doc) : MySuperClass()
{
m_myMember = 0;
m_doc = doc;
}
MyOtherClass::MyOtherClass(): MySuperClass() {}
2. 非C++代碼指針類型--指針類型應該在類型和*之間加一空格(*臨近緊接着标示符)。
3. C++代碼指針和應用類型—指針類型和應用類型和*與&之間不需要空格。
正确:
Image*SVGStyledElement::doSomething(PaintInfo& paintInfo)
{
SVGStyledElement* element =static_cast<SVGStyledElement*>(node());
const KCDashArray& dashes = dashArray();
錯誤:
Image*SVGStyledElement::doSomething(PaintInfo &paintInfo)
{
SVGStyledElement *element =static_cast<SVGStyledElement *>(node());
const KCDashArray &dashes =dashArray();
1.8 include語句
1. 所有實作檔案必須首先包含config.h。頭檔案中不要包含config.h。
正确:
// RenderLayer.h
#include"Node.h"
#include"RenderObject.h"
#include"RenderView.h"
錯誤:
// RenderLayer.h
#include"config.h"
#include"RenderObject.h"
#include"RenderView.h"
#include"Node.h"
2. 所有的實作檔案必須在包含config.h之後包含主的頭檔案。例如,Node.cpp應該包含Node.h,而且必須在包含其他頭檔案之前config.h之後。這保證能完整驗證頭檔案,而且每個頭檔案能夠單獨編譯不需要其他頭檔案。
3. 其他頭檔案應該按照一定順序(大小寫區分,如用指令行排序工具排序或者Xocde排序後選擇)。不要以邏輯順序組織包含順序。
正确:
// HTMLDivElement.cpp
#include"config.h"
#include"HTMLDivElement.h"
#include"Attribute.h"
#include"HTMLElement.h"
#include"QualifiedName.h"
錯誤:
// HTMLDivElement.cpp
#include"HTMLElement.h"
#include"HTMLDivElement.h"
#include"QualifiedName.h"
#include"Attribute.h"