天天看点

webkit代码规范本文最新地址:http://exbrowser.com/?p=501

本文最新地址: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"