WebCore:
[webkit/qt.git] / WebKitSite / coding / coding-style.html
blob2ea6627beaaaab01b37db2056f2b983944af7eb8
1 <?php
2 $title="WebKit Coding Style Guidelines";
3 include("../header.inc");
4 ?>
6 <style type="text/css">
7 pre .code {
8 background-color: #F2F2F2;
11 .right {
12 color: #080 !important;
15 .wrong {
16 color: #f00 !important;
18 </style>
21 <h2>WebKit Coding Style Guidelines</h2>
22 <h3>Indentation</h3>
24 <ol>
25 <li> Use spaces, not tabs. Tabs should only appear in files that require them for semantic meaning, like Makefiles.
26 </li>
27 <li> The indent size is 4 spaces.
28 <h4 class="right">Right:</h4>
29 <pre class="code">
30 int main()
32 return 0;
34 </pre>
36 <h4 class="wrong">Wrong:</h4>
37 <pre class="code">
38 int main()
40 return 0;
42 </pre>
43 </li>
44 <li>In a header, code inside a namespace should be indented.
45 <h4 class="right">Right:</h4>
46 <pre class="code">
47 // Document.h
48 namespace WebCore {
50 class Document {
51 Document();
52 ...
55 } // namespace WebCore
56 </pre>
58 <h4 class="wrong">Wrong:</h4>
59 <pre class="code">
60 // Document.h
61 namespace WebCore {
63 class Document {
64 Document();
65 ...
68 } // namespace WebCore
69 </pre>
70 </li>
72 <li>In an implementation file, code inside a namespace should <em>not</em> be indented.
73 <h4 class="right">Right:</h4>
74 <pre class="code">
75 // Document.cpp
76 namespace WebCore {
78 Document::Document()
80 ...
83 } // namespace WebCore
84 </pre>
86 <h4 class="wrong">Wrong:</h4>
87 <pre class="code">
88 // Document.cpp
89 namespace WebCore {
91 Document::Document()
93 ...
96 } // namespace WebCore
97 </pre>
98 </li>
99 <li>A case label should line up with its switch statement. The case statement is indented.
100 <h4 class="right">Right:</h4>
101 <pre class="code">
102 switch (condition) {
103 case fooCondition:
104 case barCondition:
105 i++;
106 break;
107 default:
108 i--;
110 </pre>
112 <h4 class="wrong">Wrong:</h4>
113 <pre class="code">
114 switch (condition) {
115 case fooCondition:
116 case barCondition:
117 i++;
118 break;
119 default:
120 i--;
122 </pre>
123 </li>
125 <li>Boolean expressions at the same nesting level that span multiple lines should
126 have their operators on the left side of the line instead of the right side.
128 <h4 class="right">Right:</h4>
129 <pre class="code">
130 return attr->name() == srcAttr
131 || attr->name() == lowsrcAttr
132 || (attr->name() == usemapAttr && attr->value().domString()[0] != '#');
133 </pre>
135 <h4 class="wrong">Wrong:</h4>
136 <pre class="code">
137 return attr->name() == srcAttr ||
138 attr->name() == lowsrcAttr ||
139 (attr->name() == usemapAttr && attr->value().domString()[0] != '#');
140 </pre>
141 </li>
143 </ol>
145 <h3>Spacing</h3>
146 <ol>
147 <li>Do not place spaces around unary operators.
148 <h4 class="right">Right:</h4>
149 <pre class="code">
150 i++;
151 </pre>
153 <h4 class="wrong">Wrong:</h4>
154 <pre class="code">
155 i ++;
156 </pre>
157 </li>
159 <li><em>Do</em> place spaces around binary and ternary operators.
160 <h4 class="right">Right:</h4>
161 <pre class="code">
162 y = m * x + b;
163 f(a, b);
164 c = a | b;
165 return condition ? 1 : 0;
166 </pre>
168 <h4 class="wrong">Wrong:</h4>
169 <pre class="code">
170 y=m*x+b;
171 f(a,b);
172 c = a|b;
173 return condition ? 1:0;
174 </pre>
175 </li>
177 <li>Place spaces between control statements and their parentheses.
178 <h4 class="right">Right:</h4>
179 <pre class="code">
180 if (condition)
181 doIt();
182 </pre>
184 <h4 class="wrong">Wrong:</h4>
185 <pre class="code">
186 if(condition)
187 doIt();
188 </pre>
189 </li>
191 <li>Do not place spaces between a function and its parentheses, or between a parenthesis and its content.
192 <h4 class="right">Right:</h4>
193 <pre class="code">
194 f(a, b);
195 </pre>
197 <h4 class="wrong">Wrong:</h4>
198 <pre class="code">
199 f (a, b);
200 f( a, b );
201 </pre>
202 </li>
203 </ol>
205 <h3>Line breaking</h3>
206 <ol>
207 <li>Each statement should get its own line.
208 <h4 class="right">Right:</h4>
209 <pre class="code">
210 x++;
211 y++;
212 if (condition)
213 doIt();
214 </pre>
216 <h4 class="wrong">Wrong:</h4>
217 <pre class="code">
218 x++; y++;
219 if (condition) doIt();
220 </pre>
221 </li>
223 <li>An <code>else</code> statement should go on the same line as a preceding close brace.
224 <h4 class="right">Right:</h4>
225 <pre class="code">
226 if (condition) {
228 } else {
231 </pre>
233 <h4 class="wrong">Wrong:</h4>
234 <pre class="code">
235 if (condition) {
238 else {
241 </pre>
242 </li>
244 <li>An <code>else if</code> statement should be written as an <code>if</code> statement when the prior <code>if</code> concludes with a <code>return</code> statement.
245 <h4 class="right">Right:</h4>
246 <pre class="code">
247 if (condition) {
249 return someValue;
251 if (condition) {
254 </pre>
256 <h4 class="wrong">Wrong:</h4>
257 <pre class="code">
258 if (condition) {
260 return someValue;
261 } else if (condition) {
264 </pre>
265 </li>
266 </ol>
268 <h3>Braces</h3>
269 <ol>
270 <li> Function definitions: place each brace on its own line.
272 <h4 class="right">Right:</h4>
273 <pre class="code">
274 int main()
278 </pre>
280 <h4 class="wrong">Wrong:</h4>
281 <pre class="code">
282 int main() {
285 </pre>
286 </li>
287 <li> Other braces: place the open brace on the line preceding the code block; place the close brace on its own line.
289 <h4 class="right">Right:</h4>
290 <pre class="code">
291 class MyClass {
295 namespace WebCore {
299 for (int i = 0; i &lt; 10; i++) {
302 </pre>
304 <h4 class="wrong">Wrong:</h4>
305 <pre class="code">
306 class MyClass
310 </pre>
311 <li>One-line control clauses should not use braces
312 <h4 class="right">Right:</h4>
313 <pre class="code">
314 if (condition)
315 doIt();
316 </pre>
318 <h4 class="wrong">Wrong:</h4>
319 <pre class="code">
320 if (condition) {
321 doIt();
323 </pre>
324 </li>
326 <li>Control clauses without a body should use empty braces:
327 <h4 class="right">Right:</h4>
328 <pre class="code">
329 for ( ; current; current = current->next) { }
330 </pre>
332 <h4 class="wrong">Wrong:</h4>
333 <pre class="code">
334 for ( ; current; current = current->next);
335 </pre>
336 </li>
337 </ol>
339 <h3>Null, false and 0</h3>
340 <ol>
341 <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 and Objective-C++, follow the guideline for C or C++, respectively, but use <code>nil</code> to represent a null Objective-C object.</li>
342 <li>C++ and C <code>bool</code> values should be written as <code>true</code> and <code>false</code>. Objective-C <code>BOOL</code> values should be written as <code>YES</code> and <code>NO</code>.</li>
343 <li>Tests for true/false, null/non-null, and zero/non-zero should all be done without equality comparisons.<br>
345 <h4 class="right">Right:</h4>
346 <pre class="code">
347 if (condition)
348 doIt();
350 if (!ptr)
351 return;
353 if (!count)
354 return;
355 </pre>
357 <h4 class="wrong">Wrong:</h4>
358 <pre class="code">
359 if (condition == true)
360 doIt();
362 if (ptr == NULL)
363 return;
365 if (count == 0)
366 return;
367 </pre>
368 </li>
369 <li>In Objective-C, instance variables are initialized to zero automatically. Don't add explicit initializations to nil or NO in an init method.</li>
370 </ol>
372 <h3>Names</h3>
373 <ol>
374 <li>Use CamelCase. Capitalize the first letter of a class, struct, protocol, or namespace name. Lower-case the first letter of a variable or function name. Fully capitalize acronyms.
375 <h4 class="right">Right:</h4>
376 <pre class="code">
377 struct Data;
378 size_t bufferSize;
379 class HTMLDocument;
380 </pre>
382 <h4 class="wrong">Wrong:</h4>
383 <pre class="code">
384 struct data;
385 size_t buffer_size;
386 class HtmlDocument;
387 </pre>
388 </li>
390 <li>Use full words, except in the rare case where an abbreviation would be more canonical and easier to understand.
391 <h4 class="right">Right:</h4>
392 <pre class="code">
393 size_t characterSize;
394 size_t length;
395 short tabIndex; // more canonical
396 </pre>
398 <h4 class="wrong">Wrong:</h4>
399 <pre class="code">
400 size_t charSize;
401 size_t len;
402 short tabulationIndex; // bizarre
403 </pre>
404 </li>
406 <li>Prefix C++ data members with "m_".
407 <h4 class="right">Right:</h4>
408 <pre class="code">
409 class String {
411 short m_length;
413 </pre>
415 <h4 class="wrong">Wrong:</h4>
416 <pre class="code">
417 class String {
419 short length;
421 </pre>
422 </li>
424 <li>Prefix Objective-C instance variables with "_".
425 <h4 class="right">Right:</h4>
426 <pre class="code">
427 @class String
429 short _length;
430 @end
431 </pre>
433 <h4 class="wrong">Wrong:</h4>
434 <pre class="code">
435 @class String
437 short length;
438 @end
439 </pre>
440 </li>
442 <li>Precede boolean values with words like "is" and "did".
443 <h4 class="right">Right:</h4>
444 <pre class="code">
445 bool isValid;
446 bool didSendData;
447 </pre>
449 <h4 class="wrong">Wrong:</h4>
450 <pre class="code">
451 bool valid;
452 bool sentData;
453 </pre>
454 </li>
456 <li>Precede setters with the word "set". Use bare words for getters. Setter and getter names should match the names of the variables being set/gotten.
457 <h4 class="right">Right:</h4>
458 <pre class="code">
459 void setCount(size_t); // sets m_count
460 size_t count(); // returns m_count
461 </pre>
463 <h4 class="wrong">Wrong:</h4>
464 <pre class="code">
465 void setCount(size_t); // sets m_theCount
466 size_t getCount();
467 </pre>
468 </li>
470 <li>Use descriptive verbs in function names.
471 <h4 class="right">Right:</h4>
472 <pre class="code">
473 bool convertToASCII(short*, size_t);
474 </pre>
476 <h4 class="wrong">Wrong:</h4>
477 <pre class="code">
478 bool toASCII(short*, size_t);
479 </pre>
480 </li>
482 <li>Leave meaningless variable names out of function declarations.
483 <h4 class="right">Right:</h4>
484 <pre class="code">
485 void setCount(size_t);
486 </pre>
488 <h4 class="wrong">Wrong:</h4>
489 <pre class="code">
490 void setCount(size_t count);
491 </pre>
492 </li>
494 <li>Objective-C method names should follow the Cocoa naming guidelines &mdash;
495 they should read like a phrase and each piece of the selector should
496 start with a lowercase letter and use intercaps.</li>
497 <li>Enum members should user InterCaps with an initial capital letter.</li>
498 <li>Prefer const to #define. Prefer inline functions to macros.</li>
499 <li>#defined constants should use all uppercase names with words separated by underscores.</li>
500 <li> Macros that expand to function calls or other non-constant computation: these
501 should be named like functions, and should have parentheses at the end, even if
502 they take no arguments (with the exception of some special macros like ASSERT).
503 Note that usually it is preferable to use an inline function in such cases instead of a macro.<br>
505 <h4 class="right">Right:</h4>
506 <pre class="code">
507 #define WBStopButtonTitle() \
508 NSLocalizedString(@"Stop", @"Stop button title")
509 </pre>
511 <h4 class="wrong">Wrong:</h4>
512 <pre class="code">
513 #define WB_STOP_BUTTON_TITLE \
514 NSLocalizedString(@"Stop", @"Stop button title")
516 #define WBStopButtontitle \
517 NSLocalizedString(@"Stop", @"Stop button title")
518 </pre>
519 </li>
520 <li>#define, #ifdef "header guards" should be named exactly the same as the file (including case), replacing the '.' with a '_'.
521 <h4 class="right">Right:</h4>
522 <pre class="code">
523 // HTMLDocument.h
524 #ifndef HTMLDocument_h
525 #define HTMLDocument_h
526 </pre>
528 <h4 class="wrong">Wrong:</h4>
529 <pre class="code">
530 // HTMLDocument.h
531 #ifndef _HTML_DOCUMENT_H_
532 #define _HTML_DOCUMENT_H_
533 </pre>
534 </li>
535 </ol>
537 <h3>Other Punctuation</h3>
539 <ol>
541 <li>Constructors for C++ classes should initialize all of their members using C++
542 initializer syntax. Each member (and superclass) should be indented on a separate
543 line, with the colon or comma preceding the member on that line.
545 <h4 class="right">Right:</h4>
546 <pre class="code">
547 MyClass::MyClass(Document* doc)
548 : MySuperClass()
549 , m_myMember(0)
550 , m_doc(doc)
554 MyOtherClass::MyOtherClass()
555 : MySuperClass()
558 </pre>
560 <h4 class="wrong">Wrong:</h4>
561 <pre class="code">
562 MyClass::MyClass(Document* doc) : MySuperClass()
564 m_myMember = 0;
565 m_doc = doc;
568 MyOtherClass::MyOtherClass() : MySuperClass() {}
569 </pre>
571 <li>Pointer types in non-C++ code &mdash; Pointer types should be written with a space between the
572 type and the * (so the * is adjacent to the following identifier if any).
574 <li>Pointer and reference types in C++ code &mdash; Both pointer types and reference types
575 should be written with no space between the type name and the * or &amp;.
577 <h4 class="right">Right:</h4>
578 <pre class="code">
579 Image* SVGStyledElement::doSomething(PaintInfo&amp; paintInfo)
581 SVGStyledElement* element = static_cast&lt;SVGStyledElement*>(node());
582 const KCDashArray&amp; dashes = dashArray();
583 </pre>
585 <h4 class="wrong">Wrong:</h4>
586 <pre class="code">
587 Image *SVGStyledElement::doSomething(PaintInfo &amp;paintInfo)
589 SVGStyledElement *element = static_cast&lt;SVGStyledElement *>(node());
590 const KCDashArray &amp;dashes = dashArray();
591 </pre>
593 </ol>
595 <h3>#include Statements</h3>
597 <ol>
599 <li>All files must #include "config.h" first.
601 <li>All files must #include the primary header second, just after "config.h".
602 So for example, Node.cpp should include Node.h first, before other files.
603 This guarantees that each header's completeness is tested, to make sure it
604 can be compiled without requiring any other header files be included first.
606 <li>Other #include statements should be in sorted order (case sensitive, as
607 done by the command-line sort tool or the Xcode sort selection command).
608 Don't bother to organize them in a logical order.
610 </ol>
612 <?php
613 include("../footer.inc");