1 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
4 <meta content="text/html; charset=ISO-8859-1"
5 http-equiv="content-type">
6 <title>WebKit Coding Style Guidelines</title>
7 <link rel=stylesheet href="../webkitdev.css">
11 <iframe id="sidebar" src="../sidebar.html"></iframe>
14 <h1 id="banner">WebKit Coding Style Guidelines</h1>
22 <li> Use spaces to indent. Tabs should not appear in code files (with
23 the exception of files that require tabs, e.g. Makefiles). We have
24 a Subversion pre-commit script that enforces this rule for most
25 source files, preventing commits of files that don't follow this rule.
27 <li> The indent size is 4 spaces.
29 <li> Code editors should be configured to expand tabs that you type to 4
36 <li> 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:
49 <li> Loop control structures, including for, while and do statements — the open brace should go on the same line as the as the control structure.
53 for (int i = 0; i < 10; i++) {
57 for (int i = 0; i < 10; i++)
62 <li> If/else statements — as above, but if there is an else clause, the close brace should go on the same line as the else.
66 if (timeToGetCoffee) {
69 } else if (timeToGoHome)
77 } else if (timeToGoHome)
82 if (timeToGetCoffee) {
84 else if (timeToGoHome)
93 <li>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.
97 int myFunction(int arg1, float arg2);
101 int myFunction (int arg1, float arg2);
102 int myFunction( int arg1 , float arg2 );
103 void noArgFunction ();
106 <li>Control structures, such as if, while, do and switch — use a single space before the open paren, but no spaces inside the parentheses.
112 <ol><li>General Rule: With very few exceptions, prefer embedded
113 capitals instead of underscores for class, function and variable
116 <li>C++ and Objective-C classes, interfaces and protocols, and other
117 type names — these names should start with a capital letter and use
122 class MyImportantClass
125 class My_important_class
126 class myImportantClass
129 <li>Local variables should use interCaps, but the first word should
130 start with a lowercase letter, like this:
141 <li>Free function names in C++ should follow the same naming
142 conventions as local variables. Most functions should be named to
143 sound like verb phrases, like "openDoor" or
144 "walkAroundTheBlock". (getters, setters, predicates?)
146 <li>C++ data members should be named like local variables, but with a
149 <li>C++ member functions should follow the same naming convention as
152 <li>Objective-C methods should follow the usual Cocoa naming style —
153 they should read like a phrase or sentence and each piece of the
154 selector should start with a lowercase letter and use intercaps.
156 <li>Objective-C instance variables should be named like local
157 variables but starting with an underscore.
159 <li>Enum members should user InterCaps with an initial capital letter.
161 <li>#defined constants should use all uppercase names with words
162 separated by underscores.
164 <li> Macros that expand to function calls or other non-constant
165 computation: these should be named like functions, and should
166 have parentheses at the end, even if they take no arguments (with
167 the exception of some special macros like ASSERT):
171 #define WBStopButtonTitle() NSLocalizedString(@"Stop", @"Go/Stop button title when busy")
174 #define WB_STOP_BUTTON_TITLE NSLocalizedString(@"Stop", @"Go/Stop button title when busy")
175 #define WBStopButtontitle NSLocalizedString(@"Stop", @"Go/Stop button title when busy")
178 <li> Acronyms in names: If an identifier includes an acronym, make the
179 acronym all-uppercase or all-lowercase, depending on whether a
180 word in that position would be capitalized or not.
193 <h3>Other Punctuation</h3>
197 <li>Pointer and reference types in C++ code — Both pointer types and reference types
198 should be written with no space between the type name and the * or &.
200 <li>Pointer types in non-C++ code — Pointer types should be written with a space between the
201 type and the * (so the * is adjacent to the following identifier if any).
205 <h3>Include Statements</h3>
209 <li>All files must #include "config.h" first.
211 <li>All files must #include the primary header second, just after "config.h".
212 So for example, Node.cpp should include Node.h first, before other files.
213 This guarantees that each header's completeness is tested, to make sure it
214 can be compiled without requiring any other header files be included first.
216 <li>Other #include statements should be in sorted order (case sensitive, as
217 done by the command-line sort tool or the Xcode sort selection command).
218 Don't bother to organize them in a logical order.