2 $title="WebKit Coding Style Guidelines";
4 $extra_head_content = <<<END
5 <style type="text/css">
7 background-color: #F2F2F2;
11 color: #080 !important;
15 color: #f00 !important;
22 include("../header.inc");
25 <h2>WebKit Coding Style Guidelines</h2>
29 <li> Use spaces, not tabs. Tabs should only appear in files that require them for semantic meaning, like Makefiles.
31 <li> The indent size is 4 spaces.
32 <h4 class="right">Right:</h4>
40 <h4 class="wrong">Wrong:</h4>
48 <li>The contents of an outermost <code>namespace</code> block (and any nested namespaces with the same scope)
49 should not be indented. The contents of other nested namespaces should be indented.
50 <h4 class="right">Right:</h4>
60 namespace NestedNamespace {
64 } // namespace WebCore
74 } // namespace WebCore
77 <h4 class="wrong">Wrong:</h4>
87 namespace NestedNamespace {
91 } // namespace WebCore
101 } // namespace WebCore
105 <li>A case label should line up with its switch statement. The case statement is indented.
106 <h4 class="right">Right:</h4>
118 <h4 class="wrong">Wrong:</h4>
131 <li>Boolean expressions at the same nesting level that span multiple lines should
132 have their operators on the left side of the line instead of the right side.
134 <h4 class="right">Right:</h4>
136 if (attr->name() == srcAttr
137 || attr->name() == lowsrcAttr
138 || (attr->name() == usemapAttr && attr->value().domString()[0] != '#'))
142 <h4 class="wrong">Wrong:</h4>
144 if (attr->name() == srcAttr ||
145 attr->name() == lowsrcAttr ||
146 (attr->name() == usemapAttr && attr->value().domString()[0] != '#'))
155 <li>Do not place spaces around unary operators.
156 <h4 class="right">Right:</h4>
161 <h4 class="wrong">Wrong:</h4>
167 <li><em>Do</em> place spaces around binary and ternary operators.
168 <h4 class="right">Right:</h4>
173 return condition ? 1 : 0;
176 <h4 class="wrong">Wrong:</h4>
181 return condition ? 1:0;
185 <li>Do not place spaces before comma and semicolon.
186 <h4 class="right">Right:</h4>
188 for (int i = 0; i < 10; i++)
194 <h4 class="wrong">Wrong:</h4>
196 for (int i = 0 ; i < 10 ; i++)
203 <li>Place spaces between control statements and their parentheses.
204 <h4 class="right">Right:</h4>
210 <h4 class="wrong">Wrong:</h4>
217 <li>Do not place spaces between a function and its parentheses, or between a parenthesis and its content.
218 <h4 class="right">Right:</h4>
223 <h4 class="wrong">Wrong:</h4>
231 <h3>Line breaking</h3>
233 <li>Each statement should get its own line.
234 <h4 class="right">Right:</h4>
242 <h4 class="wrong">Wrong:</h4>
245 if (condition) doIt();
249 <li>An <code>else</code> statement should go on the same line as a preceding close brace if one is present,
250 else it should line up with the <code>if</code> statement.
251 <h4 class="right">Right:</h4>
271 <h4 class="wrong">Wrong:</h4>
280 if (condition) doSomething(); else doSomethingElse();
282 if (condition) doSomething(); else {
288 <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.
289 <h4 class="right">Right:</h4>
300 <h4 class="wrong">Wrong:</h4>
305 } else if (condition) {
314 <li> Function definitions: place each brace on its own line.
316 <h4 class="right">Right:</h4>
324 <h4 class="wrong">Wrong:</h4>
331 <li> Other braces: place the open brace on the line preceding the code block; place the close brace on its own line.
333 <h4 class="right">Right:</h4>
343 for (int i = 0; i < 10; i++) {
348 <h4 class="wrong">Wrong:</h4>
355 <li>One-line control clauses should not use braces unless comments are included
356 or a single statement spans multiple lines.
357 <h4 class="right">Right:</h4>
368 myFunction(reallyLongParam1, reallyLongParam2, ...
373 <h4 class="wrong">Wrong:</h4>
384 myFunction(reallyLongParam1, reallyLongParam2, ...
389 <li>Control clauses without a body should use empty braces:
390 <h4 class="right">Right:</h4>
392 for ( ; current; current = current->next) { }
395 <h4 class="wrong">Wrong:</h4>
397 for ( ; current; current = current->next);
402 <h3>Null, false and 0</h3>
404 <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>
405 <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>
406 <li>Tests for true/false, null/non-null, and zero/non-zero should all be done without equality comparisons.<br>
408 <h4 class="right">Right:</h4>
420 <h4 class="wrong">Wrong:</h4>
422 if (condition == true)
432 <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>
435 <h3>Floating point literals</h3>
437 <li>Unless required in order to force floating point math, do not append
438 <code>.0</code>, <code>.f</code> and <code>.0f</code> to floating point
441 <h4 class="right">Right:</h4>
443 const double duration = 60;
445 void setDiameter(float diameter)
447 radius = diameter / 2;
452 const int framesPerSecond = 12;
453 double frameDuration = 1.0 / framesPerSecond;
456 <h4 class="wrong">Wrong:</h4>
458 const double duration = 60.0;
460 void setDiameter(float diameter)
462 radius = diameter / 2.f;
467 const int framesPerSecond = 12;
468 double frameDuration = 1 / framesPerSecond; // integer division
475 <li>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.
476 <h4 class="right">Right:</h4>
484 <h4 class="wrong">Wrong:</h4>
493 <li>Use full words, except in the rare case where an abbreviation would be more canonical and easier to understand.
494 <h4 class="right">Right:</h4>
496 size_t characterSize;
498 short tabIndex; // more canonical
501 <h4 class="wrong">Wrong:</h4>
505 short tabulationIndex; // bizarre
509 <li>Data members in C++ classes should be private. Static data members should be prefixed by "s_". Other data members should be prefixed by "m_".
510 <h4 class="right">Right:</h4>
521 <h4 class="wrong">Wrong:</h4>
532 <li>Prefix Objective-C instance variables with "_".
533 <h4 class="right">Right:</h4>
541 <h4 class="wrong">Wrong:</h4>
550 <li>Precede boolean values with words like "is" and "did".
551 <h4 class="right">Right:</h4>
557 <h4 class="wrong">Wrong:</h4>
564 <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.
565 <h4 class="right">Right:</h4>
567 void setCount(size_t); // sets m_count
568 size_t count(); // returns m_count
571 <h4 class="wrong">Wrong:</h4>
573 void setCount(size_t); // sets m_theCount
578 <li>Use descriptive verbs in function names.
579 <h4 class="right">Right:</h4>
581 bool convertToASCII(short*, size_t);
584 <h4 class="wrong">Wrong:</h4>
586 bool toASCII(short*, size_t);
590 <li>Leave meaningless variable names out of function declarations.
591 <h4 class="right">Right:</h4>
593 void setCount(size_t);
596 <h4 class="wrong">Wrong:</h4>
598 void setCount(size_t count);
602 <li>Objective-C method names should follow the Cocoa naming guidelines —
603 they should read like a phrase and each piece of the selector should
604 start with a lowercase letter and use intercaps.</li>
605 <li>Enum members should user InterCaps with an initial capital letter.</li>
606 <li>Prefer const to #define. Prefer inline functions to macros.</li>
607 <li>#defined constants should use all uppercase names with words separated by underscores.</li>
608 <li> Macros that expand to function calls or other non-constant computation: these
609 should be named like functions, and should have parentheses at the end, even if
610 they take no arguments (with the exception of some special macros like ASSERT).
611 Note that usually it is preferable to use an inline function in such cases instead of a macro.<br>
613 <h4 class="right">Right:</h4>
615 #define WBStopButtonTitle() \
616 NSLocalizedString(@"Stop", @"Stop button title")
619 <h4 class="wrong">Wrong:</h4>
621 #define WB_STOP_BUTTON_TITLE \
622 NSLocalizedString(@"Stop", @"Stop button title")
624 #define WBStopButtontitle \
625 NSLocalizedString(@"Stop", @"Stop button title")
628 <li>#define, #ifdef "header guards" should be named exactly the same as the file (including case), replacing the '.' with a '_'.
629 <h4 class="right">Right:</h4>
632 #ifndef HTMLDocument_h
633 #define HTMLDocument_h
636 <h4 class="wrong">Wrong:</h4>
639 #ifndef _HTML_DOCUMENT_H_
640 #define _HTML_DOCUMENT_H_
645 <h3>Other Punctuation</h3>
649 <li>Constructors for C++ classes should initialize all of their members using C++
650 initializer syntax. Each member (and superclass) should be indented on a separate
651 line, with the colon or comma preceding the member on that line.
653 <h4 class="right">Right:</h4>
655 MyClass::MyClass(Document* doc)
662 MyOtherClass::MyOtherClass()
668 <h4 class="wrong">Wrong:</h4>
670 MyClass::MyClass(Document* doc) : MySuperClass()
676 MyOtherClass::MyOtherClass() : MySuperClass() {}
679 <li>Pointer types in non-C++ code — Pointer types should be written with a space between the
680 type and the * (so the * is adjacent to the following identifier if any).
682 <li>Pointer and reference types in C++ code — Both pointer types and reference types
683 should be written with no space between the type name and the * or &.
685 <h4 class="right">Right:</h4>
687 Image* SVGStyledElement::doSomething(PaintInfo& paintInfo)
689 SVGStyledElement* element = static_cast<SVGStyledElement*>(node());
690 const KCDashArray& dashes = dashArray();
693 <h4 class="wrong">Wrong:</h4>
695 Image *SVGStyledElement::doSomething(PaintInfo &paintInfo)
697 SVGStyledElement *element = static_cast<SVGStyledElement *>(node());
698 const KCDashArray &dashes = dashArray();
703 <h3>#include Statements</h3>
707 <li>All implementation files must #include "config.h" first. Header
708 files should never include "config.h".
710 <h4 class="right">Right:</h4>
714 #include "RenderObject.h"
715 #include "RenderView.h"
718 <h4 class="wrong">Wrong:</h4>
723 #include "RenderObject.h"
724 #include "RenderView.h"
728 <li>All implementation files must #include the primary header second,
729 just after "config.h". So for example, Node.cpp should include Node.h first,
730 before other files. This guarantees that each header's completeness is tested.
731 This also assures that each header can be compiled without requiring any other
732 header files be included first.
734 <li>Other #include statements should be in sorted order (case sensitive, as
735 done by the command-line sort tool or the Xcode sort selection command).
736 Don't bother to organize them in a logical order.
738 <h4 class="right">Right:</h4>
740 // HTMLDivElement.cpp
742 #include "HTMLDivElement.h"
744 #include "Attribute.h"
745 #include "HTMLElement.h"
746 #include "QualifiedName.h"
750 <h4 class="wrong">Wrong:</h4>
752 // HTMLDivElement.cpp
753 #include "HTMLElement.h"
754 #include "HTMLDivElement.h"
755 #include "QualifiedName.h"
756 #include "Attribute.h"
759 <li>Includes of system headers must come after includes of other headers.
761 <h4 class="right">Right:</h4>
764 #include "ArgumentEncoder.h"
765 #include "ProcessLauncher.h"
766 #include "WebPageProxyMessageKinds.h"
767 #include "WorkItem.h"
768 #include <QApplication>
769 #include <QLocalServer>
770 #include <QLocalSocket>
773 <h4 class="wrong">Wrong:</h4>
776 #include "ArgumentEncoder.h"
777 #include "ProcessLauncher.h"
778 #include <QApplication>
779 #include <QLocalServer>
780 #include <QLocalSocket>
781 #include "WebPageProxyMessageKinds.h"
782 #include "WorkItem.h"
787 <h3>"using" Statements</h3>
791 <li>In header files, do not use "using" statements in namespace
794 <h4 class="right">Right:</h4>
807 <h4 class="wrong">Wrong:</h4>
823 <li>In header files in the WTF sub-library, however, it is acceptable
824 to use "using" declarations at the end of the file to import one
825 or more names in the WTF namespace into the global scope.
827 <h4 class="right">Right:</h4>
837 <h4 class="wrong">Wrong:</h4>
847 <h4 class="wrong">Wrong:</h4>
855 using WTF::PlacementNewAdopt;
859 <li>In C++ implementation files, do not use statements of the form
860 "using std::foo" to import names in the standard template library.
861 Use "using namespace std" instead.
863 <h4 class="right">Right:</h4>
865 // HTMLBaseElement.cpp
871 } // namespace WebCore
874 <h4 class="wrong">Wrong:</h4>
876 // HTMLBaseElement.cpp
882 } // namespace WebCore
886 <li>In implementation files, if a "using namespace" statement is
887 for a nested namespace whose parent namespace is defined in the file,
888 put the statement inside that namespace definition.
890 <h4 class="right">Right:</h4>
892 // HTMLBaseElement.cpp
896 using namespace HTMLNames;
898 } // namespace WebCore
901 <h4 class="wrong">Wrong:</h4>
903 // HTMLBaseElement.cpp
905 using namespace WebCore::HTMLNames;
909 } // namespace WebCore
913 <li>In implementation files, put all other "using" statements
914 at the beginning of the file, before any namespace definitions and
915 after any "include" statements.
917 <h4 class="right">Right:</h4>
919 // HTMLSelectElement.cpp
925 } // namespace WebCore
928 <h4 class="wrong">Wrong:</h4>
930 // HTMLSelectElement.cpp
936 } // namespace WebCore
943 include("../footer.inc");