WebKit code specifications

Source: Internet
Author: User
Document directory
  • Indent
  • Space
  • Line feed
  • Braces
  • Null, false, and 0
  • Name
  • Other punctuation
  • # Include statement
Indent
  1. Use spaces instead of tabs. Tab only appears in a semantic file, such as makefile.
  2. The indent size is 4 spaces.
    Correct:

     

    int main(){    return 0;}

    Error:

    int main(){        return 0;}
  3. In the header file, the code in the namespace should be indented.
    Correct:

     

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

    Error:

    // Document.h namespace WebCore {class Document {     Document();     ... }; } // namespace WebCore
  4. In the implementation file, the code in the namespaceNoIndent.
    Correct:

     

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

    Error:

    // Document.cppnamespace WebCore {     Document::Document()    {        ...    } } // namespace WebCore
  5. The case label should be aligned with the switch, and the statements in the case should be indented.
    Correct:

     

    switch (condition) {case fooCondition:case barCondition:    i++;    break;default:    i--;}

    Error:

    switch (condition) {    case fooCondition:    case barCondition:        i++;        break;    default:        i--;}
  6. When a Boolean expression at the same level spans multiple rows, the Boolean operator should be placed at the beginning of the row rather than behind it.
    Correct:

     

    return attr->name() == srcAttr    || attr->name() == lowsrcAttr    || (attr->name() == usemapAttr && attr->value().domString()[0] != '#');

    Error:

    return attr->name() == srcAttr ||    attr->name() == lowsrcAttr ||    (attr->name() == usemapAttr && attr->value().domString()[0] != '#');
Space
  1. No space is added before the unary operator.
    Correct:

     

    i++;

    Error:

    i ++;
  2. Spaces should be added before and after the binary and ternary operators.
    Correct:

     

    y = m * x + b;f(a, b);c = a | b;return condition ? 1 : 0;

    Error:

    y=m*x+b;f(a,b);c = a|b;return condition ? 1:0;
  3. A space must be added between the control statement and parentheses.
    Correct:

     

    if (condition)    doIt();

    Error:

    if(condition)    doIt();
  4. When a function is called, no space is left between the name and the parentheses, and between the parentheses and the content.
    Correct:

     

    f(a, b);

    Error:

    f (a, b);f( a, b );
Line feed
  1. Each statement occupies one row.
    Correct:

     

    x++;y++;if (condition)    doIt();

    Error:

    x++; y++;if (condition) doIt();
  2. The else keyword should be in one line with the braces.
    Correct:

     

    if (condition) {    ...} else {    ...}

    Error:

    if (condition) {    ...}else {    ...}
  3. When the current if statement is terminated by the Return Statement, the else if statement should be written into multiple if statements.
    Correct:

     

    if (condition) {    ...    return someValue;}if (condition) {    ...}

    Error:

    if (condition) {    ...    return someValue;} else if (condition) {    ...}
Braces
  1. Number of rows: Each braces occupies one row.
    Correct:

     

    int main(){    ...}

    Error:

    int main() {    ...}
  2. Other braces: The left braces are placed after the previous statement, and the right braces occupy a single row.
    Correct:

     

    class MyClass {    ...}; namespace WebCore {    ...} for (int i = 0; i < 10; i++) {    ...}

    Error:

    class MyClass{    ...};
  3. Conditional Control statements with only one row do not use braces.
    Correct:

     

    if (condition)    doIt();

    Error:

    if (condition) {    doIt();}
  4. When the Condition Clause is null, braces cannot be omitted.
    Correct:

     

    for ( ; current; current = current->next) { }

    Error:

    for ( ; current; current = current->next);
Null, false, and 0
  1. In C ++, null pointers are written as 0, while in C, null pointers are written as null. In objective-C and objective-C ++, Nil is used to represent an empty objective-C object.
  2. The bool values of C and C ++ should be written as true and false. The bool values of objective-C are represented by yes and no.
  3. CheckTrue/false,Null/non-emptyAndZero/non-zeroEquality should not be used.
    Correct:

     

    if (condition)    doIt(); if (!ptr)    return; if (!count)    return;

    Error:

    if (condition == true)    doIt(); if (ptr == NULL)    return; if (count == 0)    return;
  4. In objective-C, the instance variable is automatically initialized to 0. Do not initialize nil or no in the function.
Name
  1. Use the camelcase method (camelcase, which relies on the combination of uppercase and lowercase spelling of words ). The first letter of the class, struct, protocol, and namespace name is in upper case; the first word of the variable and function name is in lower case; the abbreviation should be in upper case.
    Correct:

     

    struct Data;size_t bufferSize;class HTMLDocument;

    Error:

    struct data;size_t buffer_size;class HtmlDocument;
  2. Use complete words unless the abbreviations are more standardized and easier to understand in special cases.
    Correct:

     

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

    Error:

    size_t charSize;size_t len;short tabulationIndex; // bizarre
  3. Data members of classes in C ++ use the "M _" prefix.
    Correct:

     

    class String {    ...    short m_length;};

    Error:

    class String {    ...    short length;};
  4. The objective-C instance variable uses "_" as the prefix.
    Correct:

     

    @class String    ...    short _length;@end

    Error:

    @class String    ...    short length;@end
  5. Add a prefix such as is or did before a Boolean variable.
    Correct:

     

    bool isValid;bool didSendData;

    Error:

    bool valid;bool sentData;
  6. The setter method starts with set, while the getter method uses the "Bare character ". The names of setter and getter methods should match those of the variables to be stored.
    Correct:

     

    void setCount(size_t); // sets m_countsize_t count(); // returns m_count

    Error:

    void setCount(size_t); // sets m_theCountsize_t getCount();
  7. Use descriptive verbs in the function name.
    Correct:

     

    bool convertToASCII(short*, size_t);

    Error:

    bool toASCII(short*, size_t);
  8. Discard meaningless parameter variable names in function declaration.
    Correct:

     

    void setCount(size_t);

    Error:

    void setCount(size_t count);
  9. The method naming of objective-C should follow the cocoa naming rules-read like a phrase, and each piece of the selector shocould start with a lowercase letter and use intercaps (not understanding :().
  10. Enum members shoshould user intercaps with an initial capital letter. (This is what intercaps doesn't know ).
  11. # Define is preferred for constants, and inline is used for macro optimization.
  12. # All letters of a defined constant are uppercase, and words are separated by underscores.
  13. For macros that expand as function calls or other non-constant computations: The naming method is consistent with the function, and should end with a pair of parentheses (except for some special macros, such as assert ). Note that in this case, it may be better to use the inline function to replace macros.
    Correct:

     

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

    Error:

    #define WB_STOP_BUTTON_TITLE /        NSLocalizedString(@"Stop", @"Stop button title") #define WBStopButtontitle /        NSLocalizedString(@"Stop", @"Stop button title")
  14. # Ifndef, # define "header file guard" should be named strictly according to the file name (the case must be the same), replace "." In the file name with "_".
    Correct:

     

    // HTMLDocument.h#ifndef HTMLDocument_h#define HTMLDocument_h

    Error:

    // HTMLDocument.h#ifndef _HTML_DOCUMENT_H_#define _HTML_DOCUMENT_H_
Other punctuation
  1. The constructor of the C ++ class should initialize all member variables. Each member variable (and parent class) must be indented and each member occupies one row. Each row starts with a colon and a comma.
    Correct:

     

    MyClass::MyClass(Document* doc)    : MySuperClass()    , m_myMember(0)    , m_doc(doc){} MyOtherClass::MyOtherClass()    : MySuperClass(){}

    Error:

    MyClass::MyClass(Document* doc) : MySuperClass(){    m_myMember = 0;    m_doc = doc;} MyOtherClass::MyOtherClass() : MySuperClass() {}
  2. Pointer type in non-C ++ code-there must be spaces between the type and * (* as close as possible to the next flag ).
  3. In C ++ Code, there is no space between pointer and reference type-type and * or.
    Correct:

     

    Image* SVGStyledElement::doSomething(PaintInfo& paintInfo){    SVGStyledElement* element = static_cast(node());    const KCDashArray& dashes = dashArray();

    Error:

    Image *SVGStyledElement::doSomething(PaintInfo &paintInfo){    SVGStyledElement *element = static_cast(node());    const KCDashArray &dashes = dashArray();
# Include statement
  1. All files must first # include "config. H ".
  2. Keep up with "config. H" and include the main header file. For example, node. cpp should include node. h before including other header files. This ensures that the integrity of each header file is tested and can be compiled without any other header files.
  3. All other # include statements should be given in a certain order (case sensitive, you can use the command line tool or the sorting function of the Editor). Do not sort them logically.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.