Post WebKit code syle

Source: Internet
Author: User
Tags case statement
ArticleDirectory
    • Indentation
    • Spacing
    • Line breaking
    • Braces
    • Null, false and 0
    • Names
    • Other punctuation
    • # Include statements

It can be used with Dr. Lin's specifications. Haha!

 

WebKit coding style guidelinesindentation
  1. Use spaces, not tabs. Tabs shoshould only appear in files that require them for semantic meaning, like makefiles.
  2. The indent size is 4 spaces.
    Right:
    Int main () {return 0 ;}
    Wrong:
    Int main () {return 0 ;}
  3. In a header, code inside a namespace shoshould be indented.
    Right:
    // Document. hnamespace WebCore {class document {document ();...}; // namespace WebCore
    Wrong:
    // Document. hnamespace WebCore {class document {document ();...}; // namespace WebCore
  4. In an implementation file (files with the extension. cpp,. C or. mm), code inside a namespace shocouldNotBe indented.
    Right:
    // Document. cppnamespace WebCore {document: Document () {...} // namespace WebCore
    Wrong:
    // Document. cppnamespace WebCore {document: Document () {...} // namespace WebCore
  5. A case label shocould line up with its switch statement. The case statement is indented.
    Right:
    Switch (condition) {Case foocondition: Case barcondition: I ++; break; default: I --;}
    Wrong:
    Switch (condition) {Case foocondition: Case barcondition: I ++; break; default: I --;}
  6. Boolean expressions at the same nesting level that span multiple lines shocould have their operators on the left side of the line instead of the right side.
    Right:
    Return ATTR-> name () = srcattr | ATTR-> name () = lowsrcattr | (ATTR-> name () = usemapattr & ATTR-> value (). domstring () [0]! = '#');
    Wrong:
    Return ATTR-> name () = srcattr | ATTR-> name () = lowsrcattr | (ATTR-> name () = usemapattr & ATTR-> value (). domstring () [0]! = '#');
Spacing
    1. Do not place spaces around und unary operators.

      Right:
      I ++;
      Wrong:
      I ++;
    2. DoPlace spaces around binary and ternary operators.
      Right:
      Y = m * x + B; F (a, B); C = A | B; return condition? 1: 0;
      Wrong:
      Y = m * x + B; F (a, B); C = A | B; return condition? 1:0;
    3. Place spaces between control statements and their parentheses.
      Right:
      If (condition) doit ();
      Wrong:
      If (condition) doit ();
    4. Do not place spaces between a Function and Its parentheses, or between a parenthesis and its content.
      Right:
      F (a, B );
      Wrong:
      F (a, B); F (A, B );
Line breaking
    1. Each statement shocould get its own line.

      Right:
      X ++; y ++; If (condition) doit ();
      Wrong:
      X ++; y ++; If (condition) doit ();
    2. AnElseStatement shoshould go on the same line as a preceding close brace.
      Right:
      If (condition) {...} else {...}
      Wrong:
      If (condition) {...} else {...}
    3. AnElse ifStatement shoshould be written asIfStatement when the priorIfConcludes withReturnStatement.
      Right:
      If (condition) {... return somevalue;} If (condition ){...}
      Wrong:
      If (condition) {... return somevalue;} else if (condition ){...}
Braces
    1. Function definitions: place each brace on its own line.

      Right:
      Int main (){...}
      Wrong:
      Int main (){...}
    2. Other braces: place the open brace on the line preceding the code block; place the close brace on its own line.
      Right:
      Class myclass {...}; namespace WebCore {...} For (INT I = 0; I <10; I ++ ){...}
      Wrong:
      Class myclass {...};
    3. One-line control clauses shocould not use braces
      Right:
      If (condition) doit ();
      Wrong:
      If (condition) {doit ();}
    4. Control krases without a body shoshould use empty braces:
      Right:
      For (; current = Current-> next ){}
      Wrong:
      For (; current = Current-> next );
Null, false and 0
    1. in C ++, the NULL pointer value shoshould be written as 0 . in C, it shoshould be written as null . in objective-C and objective-C ++, follow the guideline for C or C ++, respectively, but use nil to represent a null objective-C object.
    2. C ++ and C bool values shoshould be written as true and false . objective-C bool values shoshould be written as Yes and NO .
    3. tests for true/false, null/non-null, and zero/non-zero shoshould all be done without affinity ity comparisons.
      right:
       If (condition) doit (); If (! PTR) return; If (! Count) return; 
      wrong:
       If (condition = true) doit (); If (PTR = NULL) return; if (COUNT = 0) return; 
    4. in objective-C, instance variables are initialized to zero automatically. Don't add explicit initializations to nil or no in an init method.
Names
  1. Use camelcase. capitalize the first letter, including all letters in an acronym, in a class, struct, protocol, or namespace name. lower-case the first letter, including all letters in an acronym, In a variable or function name.

    Right:
    Struct data; size_t buffersize; Class htmldocument; string mimetype ();
    Wrong:
    Struct data; size_t buffer_size; Class htmldocument; string mimetype ();
  2. Use full words, should T in the rare case where an abbreviation wocould be more canonical and easier to understand.
    Right:
    Size_t charactersize; size_t length; short tabindex; // more canonical
    Wrong:
    Size_t charsize; size_t Len; short tabulationindex; // bizarre
  3. Prefix C ++ data members with "M _".
    Right:
    Class string {... short m_length ;};
    Wrong:
    Class string {... short length ;};
  4. Prefix objective-C instance variables "_".
    Right:
    @ Class string... short _ length; @ end
    Wrong:
    @ Class string... short length; @ end
  5. Precede boolean values with words like "is" and "did ".
    Right:
    Bool isvalid; bool didsenddata;
    Wrong:
    Bool valid; bool sentdata;
  6. Precede setters with the word "set". Use bare words for getters. setter and getter names shocould match the names of the variables being set/gotten.
    Right:
    Void setcount (size_t); // sets m_countsize_t count (); // returns m_count
    Wrong:
    Void setcount (size_t); // sets m_thecountsize_t getcount ();
  7. Use descriptive verbs in function names.
    Right:
    Bool converttoascii (short *, size_t );
    Wrong:
    Bool toascii (short *, size_t );
  8. Leave meaningless variable names out of function declarations.
    Right:
    Void setcount (size_t );
    Wrong:
    Void setcount (size_t count );
  9. Objective-C method names shocould follow the cocoa naming guidelines-They shoshould read like a phrase and each piece of the selector shocould start with a lowercase letter and use intercaps.
  10. Enum members shocould user intercaps with an initial capital letter.
  11. Prefer const to # define. Prefer inline functions to macros.
  12. # Defined constants shocould use all uppercase names with words separated by underscores.
  13. Macros that expand to function CILS or other non-constant computation: These shocould be named like functions, and shocould have parentheses at the end, even if they take no arguments (with the exception of some special macros like assert ). note that usually it is preferable to use an inline function in such cases instead of a macro.
    Right:
    # Define wbstopbuttontitle () \ nslocalizedstring (@ "stop", @ "STOP button title ")
    Wrong:
    # Define wb_stop_button_title \ nslocalizedstring (@ "stop", @ "STOP button title") # define wbstopbuttontitle \ nslocalizedstring (@ "stop", @ "STOP button title ")
  14. # Define, # ifdef "header guards" shocould be named exactly the same as the file (including case), replacing the '.' with '_'.
    Right:
    // Htmldocument. h # ifndef htmldocument_h # define htmldocument_h
    Wrong:
    // Htmldocument. h # ifndef _ html_document_h _ # DEFINE _ html_document_h _
Other punctuation
  1. Constructors for C ++ classes shold Initialize all of their members using C ++ initializer syntax. each member (and superclass) shocould be indented on a separate line, with the colon or comma preceding the member on that line.

    Right:
    Myclass: myclass (document * DOC): mysuperclass (), m_mymember (0), m_doc (DOC) {} mytherclass: mytherclass (): mysuperclass (){}
    Wrong:
    Myclass: myclass (document * DOC): mysuperclass () {m_mymember = 0; m_doc = Doc;} mytherclass: mytherclass (): mysuperclass (){}
  2. Pointer types in non-C ++ code-pointer types shoshould be written with a space between the type and the * (so the * is adjacent to the following identifier if any ).
  3. Pointer and reference types in C ++ code-both pointer types and reference types shoshould be written with no space between the type name and the * or &.
    Right:
    Image * svgstyledelement: dosomething (paintinfo & paintinfo) {svgstyledelement * element = static_cast <svgstyledelement *> (node (); const kcdasharray & dash = dasharray ();
    Wrong:
    Image * svgstyledelement: dosomething (paintinfo & paintinfo) {svgstyledelement * element = static_cast <svgstyledelement *> (node (); const kcdasharray & dash = dasharray ();
# Include statements
  1. All implementation files must # include "config. H" first. header files shoshould never include "config. H ".

    Right:
    // Renderlayer. h # include "node. H" # include "renderobject. H" # include "renderview. H"
    Wrong:
    // Renderlayer. h # include "config. H" # include "renderobject. H" # include "renderview. H" # include "node. H"
  2. All implementation files must # include the primary header second, just after "config. H ". so for example, node. CPP shoshould include node. h first, before other files. this guarantees that each header's completeness is tested. this also assures that each header can be compiled without requiring any other header files must ded first.
  3. Other # include statements shoshould be in sorted order (case sensitive, as done by the command-line sort tool or the xcode sort selection command ). don't bother to organize them in a logical order.
    right:
     // htmldivelement. CPP # include "config. H "# include" htmldivelement. H "# include" attribute. H "# include" htmlelement. H "# include" qualifiedname. H "
    wrong:
     // htmldivelement. CPP # include "htmlelement. H "# include" htmldivelement. H "# include" qualifiedname. H "# include" attribute. H "
    • Home
    • Surfin 'safari blog
    • planet WebKit
    • project goals
    • keeping in touch
    • TRAC
    • working with the Code
    • installing developer tools
    • getting the Code
    • building WebKit
    • running WebKit
    • debugging WebKit
    • contributing Code
    • commit and review policy
    • documentation
    • wiki
    • projects
    • code style guidelines
    • Web Inspector
    • Web Developer resources
    • Dom interfaces
    • testing
    • regression testing
    • Leak Hunting
    • writing new tests
    • getting a crash log
    • bugs
    • reporting bugs
    • bug report guidelines
    • bug prioritization
    • test case ction
    • bug Life Cycle
WebKit is open source software with portions licensed under the lgpl and BSD licenses. complete license and copyright information can be found within the code. hosting provided by Mac OS forge. use of this site is subject to the Mac OS forge Terms of Use.

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.