WebKit代碼規範

來源:互聯網
上載者:User
文章目錄
  • 縮排
  • 空格
  • 換行
  • 大括弧
  • Null,False和0
  • 命名
  • 其它標點
  • #include語句
縮排
  1. 使用空格而不是Tab。Tab只應該出現在有語義的檔案裡,如makefile檔案。
  2. 縮排大小為4個空格。
    正確:

     

    int main(){    return 0;}

    錯誤:

    int main(){        return 0;}
  3. 在標頭檔中,命名空間中的代碼應該被縮排。
    正確:

     

    // Document.hnamespace WebCore {     class Document {        Document();        ...    }; } // namespace WebCore

    錯誤:

    // Document.h namespace WebCore {class Document {     Document();     ... }; } // namespace WebCore
  4. 在實現檔案中,命名空間中的代碼不應該縮排。
    正確:

     

    // Document.cppnamespace WebCore {Document::Document(){    ...}} // namespace WebCore

    錯誤:

    // Document.cppnamespace WebCore {     Document::Document()    {        ...    } } // namespace WebCore
  5. case標籤應該和switch對齊,case下的語句要縮排。
    正確:

     

    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 && attr->value().domString()[0] != '#');

    錯誤:

    return attr->name() == srcAttr ||    attr->name() == lowsrcAttr ||    (attr->name() == usemapAttr && attr->value().domString()[0] != '#');
空格
  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. 每個語句佔用一行。
    正確:

     

    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. 行數定義:每個大括弧佔用一行。
    正確:

     

    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);
Null,False和0
  1. C++語言中,null 指標被寫成0,而在C語言中,null 指標被寫成NULL。在Objective-C和Objective-C++中,使用nil表示一個空Objective-C對象。
  2. C和C++的bool值應該被寫作true和false。Objective-C的BOOL值則用YES和NO表示。
  3. 檢查真/假空/非空以及零/非零不應該使用等式。
    正確:

     

    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,不要再初始化函數中額外的初始化為nil或NO。
命名
  1. 採用駱駝命名法(CamelCase,依靠單詞的大小寫拼字複合詞的做法)。class,struct,protocol,namespace名字的第一個字母大寫;變數和函數名的第一個單詞小寫;縮寫應完全大寫。
    正確:

     

    struct Data;size_t bufferSize;class HTMLDocument;

    錯誤:

    struct data;size_t buffer_size;class HtmlDocument;
  2. 使用完整的單詞,除非特殊情況下縮寫更規範和更容易理解。
    正確:

     

    size_t characterSize;size_t length;short tabIndex; // more canonical

    錯誤:

    size_t charSize;size_t len;short tabulationIndex; // bizarre
  3. C++中類的資料成員採用“m_”首碼。
    正確:

     

    class String {    ...    short m_length;};

    錯誤:

    class String {    ...    short length;};
  4. Objective-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_countsize_t count(); // returns m_count

    錯誤:

    void setCount(size_t); // sets m_theCountsize_t getCount();
  7. 在函數名字中採用說明性的動詞。
    正確:

     

    bool convertToASCII(short*, size_t);

    錯誤:

    bool toASCII(short*, size_t);
  8. 在函數的聲明中丟棄無意義的參數變數名。
    正確:

     

    void setCount(size_t);

    錯誤:

    void setCount(size_t count);
  9. Objective-C的方法命名應遵循Cocoa命名規則——讀起來像短語,並且each piece of the selector should start with a lowercase letter and use intercaps(沒看懂:()。
  10. Enum members should user InterCaps with an initial capital letter。(也沒看懂,就是這個InterCaps不知道什麼意思)。
  11. 常量優先使用#define,宏優先使用inline函數。
  12. #defined定義的常量的所有字母都大寫,單詞間用底線分隔。
  13. 對於展開為函數調用或者其它非常量計算的宏:命名方法和函數一致,並且不管有沒有參數都應該以一對圓括弧結束(一些特殊的宏例外,如ASSERT)。注意在這種情況,採用inline函數代替宏也許是更好的辦法。
    正確:

     

    #define WBStopButtonTitle() /        NSLocalizedString(@"Stop", @"Stop button title")

    錯誤:

    #define WB_STOP_BUTTON_TITLE /        NSLocalizedString(@"Stop", @"Stop button title") #define WBStopButtontitle /        NSLocalizedString(@"Stop", @"Stop button title")
  14. #ifndef,#define“標頭檔衛士”應該嚴格按照檔案名稱來命名(大小寫也要一致),將檔案名稱中的“.”替換成“_”。
    正確:

     

    // HTMLDocument.h#ifndef HTMLDocument_h#define HTMLDocument_h

    錯誤:

    // HTMLDocument.h#ifndef _HTML_DOCUMENT_H_#define _HTML_DOCUMENT_H_
其它標點
  1. 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(node());    const KCDashArray& dashes = dashArray();

    錯誤:

    Image *SVGStyledElement::doSomething(PaintInfo &paintInfo){    SVGStyledElement *element = static_cast(node());    const KCDashArray &dashes = dashArray();
#include語句
  1. 所有檔案必須首先#include “config.h”。
  2. 緊跟”config.h”之後應該包含主標頭檔,例如,Node.cpp應該在包含其它標頭檔前包含Node.h。這將保證每一個標頭檔的完整性會經過測試,不依賴於其它任何標頭檔就可以通過編譯。
  3. 其它所有#include語句應該以一定順序給出(區分大小寫,可利用命令列工具或編輯器的排序功能),不要煞費苦心的以一定的邏輯排序。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.