WebKit Coding Style Guidelines

Indenting

  1. Use spaces to indent. Tabs should not appear in code files (with the exception of files that require them e.g. Makefiles).
  2. The indent size is 4 spaces.
  3. Code editors should be configured to expand tabs that you type to 4 spaces.

Braces

  1. Function definitions - open and close braces should be on lines by themselves. Do not put the open brace on the same line as the function signature. For example:
        RIGHT:
        
            void foo()
            {
            }
            
            
        WRONG:
        
            void foo() {
            }
    
    
  2. Loop control structures, including for, while and do statements - the open brace should go on the same line as the as the control structure.
        RIGHT:
        
            for (int i = 0; i < 10; i++) {
            }
                    
        WRONG:
        
            for (int i = 0; i < 10; i++) 
            {
            }
    
    
  3. If/else statements - as above, but if there is an else clause, the close brace should go on the same line as the else.
        RIGHT:
    
            if (timeToGetCoffee) {
    	    buyCoffee(&coffee);
                chugIt(coffee);
            } else if (timeToGoHome)
                outtaHere = true;
    
    
        WRONG:
    
            if (timeToGetCoffee)
            {
    	    buyCoffee(&coffee);
                chugIt(coffee);
            } else if (timeToGoHome) 
            {
                outtaHere = true;
            }
            
            if (timeToGetCoffee) {
            } 
    	else if (timeToGoHome) 
                outtaHere = true;
    
    

Parentheses

  1. Function declarations and calls - do not use any spaces between the name and the open paren, inside the parentheses, or before commas that separate arguments. Do use a single space after commas that separate arguments.
        RIGHT:
    
            int myFunction(int arg1, float arg2);
            void noArgFunction();
    
        WRONG:
            int myFunction (int arg1, float arg2);
            int myFunction( int arg1 , float arg2 );
            void noArgFunction ();
    
  2. Control structures, such as if, while, do and switch - use a single space before the open paren, but no spaces inside the parentheses.

Names

  1. General Rule: With very few exceptions, prefer embedded capitals instead of underscores for class, function and variable names.
  2. C++ and Objective-C classes, interfaces and protocols, and other type names - these names should start with a capital letter and use InterCaps.
        RIGHT:
        
            class MyImportantClass
            
        WRONG:
        
            class My_important_class
            class myImportantClass
    
  3. Local variables should use interCaps, but the first word should start with a lowercase letter, like this:
        RIGHT:
        
            int myInt;
            
        WRONG:
        
            int MyInt;
            int my_int;
    
  4. Free function names in C++ should follow the same naming conventions as local variables. Most functions should be named to sound like verb phrases, like "openDoor" or "walkAroundTheBlock". (getters, setters, predicates?)
  5. C++ data members should be named like local variables, but with a prefix of m_.
  6. C++ member functions should follow the same naming convention as free functions.
  7. Objective-C methods should follow the usual Cocoa naming style - they should read like a phrase or sentence and each piece of the selector should start with a lowercase letter and use intercaps.
  8. Objective-C instance variables should be named like local variables but starting with an underscore.
  9. Pointer and reference types - pointer types should be written with a space between the type name and the * (so the * is adjacent to the following identifier if any). For reference types, the & goes next to the type name.
  10. Enum members should user InterCaps with an initial capital letter.
  11. #defined constants should use all uppercase names with words separated by underscores.
  12. Macros that expand to function calls or other non-constant computation: these should be named like functions, and should have parentheses at the end, even if they take no arguments (with the exception of some special macros like ASSERT):
        RIGHT:
            #define WBStopButtonTitle()  NSLocalizedString(@"Stop", @"Go/Stop button title when busy")
    
        WRONG:
            #define WB_STOP_BUTTON_TITLE  NSLocalizedString(@"Stop", @"Go/Stop button title when busy")
            #define WBStopButtontitle  NSLocalizedString(@"Stop", @"Go/Stop button title when busy")
    
  13. Acronyms in names: If an identifier includes an acronym, make the acronym all-uppercase or all-lowercase, depending on whether a word in that position would be capitalized or not.
        RIGHT:
    
            urlVariable
            myURLAccessor:
    
        WRONG:
    
            uRLVariable
            myUrlAccessor: