2 $title="WebKit Coding Style Guidelines";
3 include("../header.inc");
7 <h2>WebKit Coding Style Guidelines</h2>
11 <li> Use spaces to indent. Tabs should not appear in code files (with the exception of files that require tabs, e.g. Makefiles).
12 We have a Subversion pre-commit script that enforces this rule for most
13 source files, preventing commits of files that don't follow this rule.
15 <li> The indent size is 4 spaces.</li>
16 <li> Code editors should be configured to expand tabs that you type to 4 spaces.</li>
21 <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:<br />
23 <h4 style="color: #008000 !important !important">Right:</h4>
24 <pre style="background-color: #F2F2F2">
31 <h4 style="color: #ff0000 !important">Wrong:</h4>
32 <pre style="background-color: #F2F2F2">
38 <li> Other braces, including for, while, do, switch statements and class definitions — the open brace should go on the same line as the as the control structure.<br />
40 <h4 style="color: #008000 !important">Right:</h4>
41 <pre style="background-color: #F2F2F2">
42 for (int i = 0; i < 10; i++) {
47 <h4 style="color: #ff0000 !important">Wrong:</h4>
49 <pre style="background-color: #F2F2F2">
50 for (int i = 0; i < 10; i++)
55 <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.
56 Also, one-line if or else clauses should not get braces.<br />
57 <h4 style="color: #008000 !important">Right:</h4>
58 <pre style="background-color: #F2F2F2">
59 if (timeToGetCoffee) {
60 buyCoffee(&coffee);
62 } else if (timeToGoHome)
63 // comment on else case
67 <h4 style="color: #ff0000 !important">Wrong:</h4>
68 <pre style="background-color: #F2F2F2">
71 buyCoffee(&coffee);
73 // comment on else case
74 } else if (timeToGoHome)
79 if (timeToGetCoffee) {
83 // comment on else case
95 <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.
96 Do use a single space after commas that separate arguments.<br />
98 <h4 style="color: #008000 !important">Right:</h4>
99 <pre style="background-color: #F2F2F2">
100 int myFunction(int arg1, float arg2);
102 void noArgFunction(); // for C++ or Objective-C++
104 void noArgFunction(void); // for C or Objective-C
107 <h4 style="color: #ff0000 !important">Wrong:</h4>
108 <pre style="background-color: #F2F2F2">
109 int myFunction (int arg1, float arg2);
111 int myFunction( int arg1 , float arg2 );
113 void noArgFunction ();
117 <li>Control structures, such as if, while, do and switch — use a single space before the open paren, but no spaces inside the parentheses.
122 <h3>Null, false and 0</h3>
124 <li>In C++, the null pointer value should be written as <code>0</code>. In C it should be written as <code>NULL</code>. In Objective-C, it should be written as <code>nil</code> if it is being used as a null pointer of type <code>id</code> or another ObjC object type, otherwise <code>NULL</code>.</li>
125 <li>True and false values of type <code>bool</code> (common in C and C++), or just generic true/false values, should be written as <code>true</code> and <code>false</code>. Values of the Objective-C <code>BOOL</code> type should be written as <code>YES</code> and <code>NO</code>.</li>
126 <li>Tests for null pointers, false values and 0 values should all be done diretly, not through an inqueality or equality comparison.<br />
128 <h4 style="color: #008000 !important">Right:</h4>
129 <pre style="background-color: #F2F2F2">
132 if (foo->isSomething()) {
137 if (!foo->isSomething()) {
162 <h4 style="color: #ff0000 !important">Wrong:</h4>
163 <pre style="background-color: #F2F2F2">
164 if (foo->isSomething() == true) {
168 if (foo->isSomething() != false) {
189 <li>General Rule: With very few exceptions, prefer embedded capitals instead of underscores for class, function and variable names.</p></li>
190 <li>C++ and Objective-C classes, interfaces and protocols, and other type names &emdash; these names should start with a capital letter and use InterCaps.<br />
192 <h4 style="color: #008000 !important">Right:</h4>
193 <pre style="background-color: #F2F2F2">
194 class MyImportantClass;
197 <h4 style="color: #ff0000 !important">Wrong:</h4>
198 <pre style="background-color: #F2F2F2">
199 class My_important_class;
201 class myImportantClass;
205 <li>Local variables should use interCaps, but the first word should start with a lowercase letter, like this:<br />
207 <h4 style="color: #008000 !important">Right:</h4>
208 <pre style="background-color: #F2F2F2">
212 <h4 style="color: #ff0000 !important">Wrong:</h4>
213 <pre style="background-color: #F2F2F2">
220 <li>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?)</li>
222 <li>C++ data members should be named like local variables, but with a prefix of m_.</li>
223 <li>C++ member functions should follow the same naming convention as free functions.</li>
224 <li>Objective-C methods should follow the usual Cocoa naming style —
225 they should read like a phrase or sentence and each piece of the selector should start with a lowercase letter and use intercaps.</li>
226 <li>Objective-C instance variables should be named like local variables but starting with an underscore.</li>
227 <li>Enum members should user InterCaps with an initial capital letter.</li>
228 <li>#defined constants should use all uppercase names with words separated by underscores.</li>
229 <li> 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). Note that usually it is preferrable to use an inline function in such cases instead of a macro.<br />
231 <h4 style="color: #008000 !important">Right:</h4>
232 <pre style="background-color: #F2F2F2">
233 #define WBStopButtonTitle() \
234 NSLocalizedString(@\"Stop\", @\"Stop button title\")
237 <h4 style="color: #ff0000 !important">Wrong:</h4>
238 <pre style="background-color: #F2F2F2">
239 #define WB_STOP_BUTTON_TITLE \
240 NSLocalizedString(@\"Stop\", @\"Stop button\")
242 #define WBStopButtontitle \
243 NSLocalizedString(@\"Stop\", @\"Stop button\")
247 <li> Acronyms in names: If an identifier includes an acronym, make the acronym all-uppercase
248 or all-lowercase, depending on whether a word in that position would be capitalized or not.<br />
250 <h4 style="color: #008000 !important">Right:</h4>
251 <pre style="background-color: #F2F2F2">
256 <h4 style="color: #ff0000 !important">Wrong:</h4>
257 <pre style="background-color: #F2F2F2">
264 <h3>Other Punctuation</h3>
268 <li>Pointer and reference types in C++ code — Both pointer types and reference types
269 should be written with no space between the type name and the * or &.
271 <li>Pointer types in non-C++ code — Pointer types should be written with a space between the
272 type and the * (so the * is adjacent to the following identifier if any).
276 <h3>Include Statements</h3>
280 <li>All files must #include "config.h" first.
282 <li>All files must #include the primary header second, just after "config.h".
283 So for example, Node.cpp should include Node.h first, before other files.
284 This guarantees that each header's completeness is tested, to make sure it
285 can be compiled without requiring any other header files be included first.
287 <li>Other #include statements should be in sorted order (case sensitive, as
288 done by the command-line sort tool or the Xcode sort selection command).
289 Don't bother to organize them in a logical order.
294 include("../footer.inc");