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;
28 include("../header.inc");
31 <h2>WebKit Coding Style Guidelines</h2>
32 <h3 id="indentation">Indentation</h3>
35 <li id="indentation-no-tabs"> Use spaces, not tabs. Tabs should only appear in files that require them for semantic meaning, like Makefiles.
37 <li id="indentation-4-spaces"> The indent size is 4 spaces.
38 <h4 class="right">Right:</h4>
46 <h4 class="wrong">Wrong:</h4>
54 <li id="indentation-namespace">The contents of an outermost <code>namespace</code> block (and any nested namespaces with the same scope)
55 should not be indented. The contents of other nested namespaces should be indented.
56 <h4 class="right">Right:</h4>
66 namespace NestedNamespace {
70 } // namespace WebCore
80 } // namespace WebCore
83 <h4 class="wrong">Wrong:</h4>
93 namespace NestedNamespace {
97 } // namespace WebCore
107 } // namespace WebCore
111 <li id="indentation-case-label">A case label should line up with its switch statement. The case statement is indented.
112 <h4 class="right">Right:</h4>
124 <h4 class="wrong">Wrong:</h4>
137 <li id="indentation-wrap-bool-op">Boolean expressions at the same nesting level that span multiple lines should
138 have their operators on the left side of the line instead of the right side.
140 <h4 class="right">Right:</h4>
142 if (attr->name() == srcAttr
143 || attr->name() == lowsrcAttr
144 || (attr->name() == usemapAttr && attr->value().domString()[0] != '#'))
148 <h4 class="wrong">Wrong:</h4>
150 if (attr->name() == srcAttr ||
151 attr->name() == lowsrcAttr ||
152 (attr->name() == usemapAttr && attr->value().domString()[0] != '#'))
159 <h3 id="spacing">Spacing</h3>
161 <li id="spacing-unary-op">Do not place spaces around unary operators.
162 <h4 class="right">Right:</h4>
167 <h4 class="wrong">Wrong:</h4>
173 <li id="spacing-binary-ternary-op"><em>Do</em> place spaces around binary and ternary operators.
174 <h4 class="right">Right:</h4>
179 return condition ? 1 : 0;
182 <h4 class="wrong">Wrong:</h4>
187 return condition ? 1:0;
191 <li id="spacing-comma-semicolon">Do not place spaces before comma and semicolon.
192 <h4 class="right">Right:</h4>
194 for (int i = 0; i < 10; ++i)
200 <h4 class="wrong">Wrong:</h4>
202 for (int i = 0 ; i < 10 ; ++i)
209 <li id="spacing-control-paren">Place spaces between control statements and their parentheses.
210 <h4 class="right">Right:</h4>
216 <h4 class="wrong">Wrong:</h4>
223 <li id="spacing-function-paren">Do not place spaces between a function and its parentheses, or between a parenthesis and its content.
224 <h4 class="right">Right:</h4>
229 <h4 class="wrong">Wrong:</h4>
237 <h3 id="linebreaking">Line breaking</h3>
239 <li id="linebreaking-multiple-statements">Each statement should get its own line.
240 <h4 class="right">Right:</h4>
248 <h4 class="wrong">Wrong:</h4>
251 if (condition) doIt();
255 <li id="linebreaking-else-braces">An <code>else</code> statement should go on the same line as a preceding close brace if one is present,
256 else it should line up with the <code>if</code> statement.
257 <h4 class="right">Right:</h4>
277 <h4 class="wrong">Wrong:</h4>
286 if (condition) doSomething(); else doSomethingElse();
288 if (condition) doSomething(); else {
294 <li id="linebreaking-else-if">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.
295 <h4 class="right">Right:</h4>
306 <h4 class="wrong">Wrong:</h4>
311 } else if (condition) {
318 <h3 id="braces">Braces</h3>
320 <li id="braces-function"> Function definitions: place each brace on its own line.
322 <h4 class="right">Right:</h4>
330 <h4 class="wrong">Wrong:</h4>
337 <li id="braces-blocks"> Other braces: place the open brace on the line preceding the code block; place the close brace on its own line.
339 <h4 class="right">Right:</h4>
349 for (int i = 0; i < 10; ++i) {
354 <h4 class="wrong">Wrong:</h4>
361 <li id="braces-one-line">One-line control clauses should not use braces unless comments are included
362 or a single statement spans multiple lines.
363 <h4 class="right">Right:</h4>
374 myFunction(reallyLongParam1, reallyLongParam2, ...
379 <h4 class="wrong">Wrong:</h4>
390 myFunction(reallyLongParam1, reallyLongParam2, ...
395 <li id="braces-empty-block">Control clauses without a body should use empty braces:
396 <h4 class="right">Right:</h4>
398 for ( ; current; current = current->next) { }
401 <h4 class="wrong">Wrong:</h4>
403 for ( ; current; current = current->next);
408 <h3 id="zero">Null, false and 0</h3>
410 <li id="zero-null">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>
412 <li id="zero-bool">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>
414 <li id="zero-comparison">Tests for true/false, null/non-null, and zero/non-zero should all be done without equality comparisons.<br>
416 <h4 class="right">Right:</h4>
428 <h4 class="wrong">Wrong:</h4>
430 if (condition == true)
441 <li id="zero-objc-variables">In Objective-C, instance variables are initialized to zero automatically. Don't add explicit initializations to nil or NO in an init method.</li>
444 <h3 id="float">Floating point literals</h3>
446 <li id="float-suffixes">Unless required in order to force floating point math, do not append
447 <code>.0</code>, <code>.f</code> and <code>.0f</code> to floating point
450 <h4 class="right">Right:</h4>
452 const double duration = 60;
454 void setDiameter(float diameter)
456 radius = diameter / 2;
461 const int framesPerSecond = 12;
462 double frameDuration = 1.0 / framesPerSecond;
465 <h4 class="wrong">Wrong:</h4>
467 const double duration = 60.0;
469 void setDiameter(float diameter)
471 radius = diameter / 2.f;
476 const int framesPerSecond = 12;
477 double frameDuration = 1 / framesPerSecond; // integer division
482 <h3 id="names">Names</h3>
484 <li id="names-basic">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.
485 <h4 class="right">Right:</h4>
493 <h4 class="wrong">Wrong:</h4>
502 <li id="names-full-words">Use full words, except in the rare case where an abbreviation would be more canonical and easier to understand.
503 <h4 class="right">Right:</h4>
505 size_t characterSize;
507 short tabIndex; // more canonical
510 <h4 class="wrong">Wrong:</h4>
514 short tabulationIndex; // bizarre
518 <li id="names-data-members">Data members in C++ classes should be private. Static data members should be prefixed by "s_". Other data members should be prefixed by "m_".
519 <h4 class="right">Right:</h4>
530 <h4 class="wrong">Wrong:</h4>
541 <li id="names-objc-instance-variables">Prefix Objective-C instance variables with "_".
542 <h4 class="right">Right:</h4>
550 <h4 class="wrong">Wrong:</h4>
559 <li id="names-bool">Precede boolean values with words like "is" and "did".
560 <h4 class="right">Right:</h4>
566 <h4 class="wrong">Wrong:</h4>
573 <li id="names-setter-getter">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.
574 <h4 class="right">Right:</h4>
576 void setCount(size_t); // sets m_count
577 size_t count(); // returns m_count
580 <h4 class="wrong">Wrong:</h4>
582 void setCount(size_t); // sets m_theCount
587 <li id="names-out-argument">Precede getters that return values through out arguments with the word "get".
588 <h4 class="right">Right:</h4>
590 void getInlineBoxAndOffset(InlineBox*&, int& caretOffset) const;
593 <h4 class="wrong">Wrong:</h4>
595 void inlineBoxAndOffset(InlineBox*&, int& caretOffset) const;
599 <li id="names-verb">Use descriptive verbs in function names.
600 <h4 class="right">Right:</h4>
602 bool convertToASCII(short*, size_t);
605 <h4 class="wrong">Wrong:</h4>
607 bool toASCII(short*, size_t);
611 <li id="names-variable-name-in-function-decl">Leave meaningless variable names out of function declarations. A good rule of thumb is if the parameter type name contains the parameter name (without trailing numbers or pluralization), then the parameter name isn't needed. Usually, there should be a parameter name for bools, strings, and numerical types.
612 <h4 class="right">Right:</h4>
614 void setCount(size_t);
616 void doSomething(ScriptExecutionContext*);
619 <h4 class="wrong">Wrong:</h4>
621 void setCount(size_t count);
623 void doSomething(ScriptExecutionContext* context);
627 <li id="names-enum-to-bool">Prefer enums to bools on function parameters if callers are likely to be
628 passing constants, since named constants are easier to read at the call
629 site. An exception to this rule is a setter function, where the name of the
630 function already makes clear what the boolean is.
631 <h4 class="right">Right:</h4>
633 doSomething(something, AllowFooBar);
634 paintTextWithShadows(context, ..., textStrokeWidth > 0, isHorizontal());
638 <h4 class="wrong">Wrong:</h4>
640 doSomething(something, false);
641 setResizable(NotResizable);
645 <li id="names-objc-methods">Objective-C method names should follow the Cocoa naming guidelines —
646 they should read like a phrase and each piece of the selector should
647 start with a lowercase letter and use intercaps.</li>
649 <li id="names-enum-members">Enum members should user InterCaps with an initial capital letter.</li>
651 <li id="names-const-to-define">Prefer const to #define. Prefer inline functions to macros.</li>
653 <li id="names-define-constants">#defined constants should use all uppercase names with words separated by underscores.</li>
655 <li id="names-define-non-const"> Macros that expand to function calls or other non-constant computation: these
656 should be named like functions, and should have parentheses at the end, even if
657 they take no arguments (with the exception of some special macros like ASSERT).
658 Note that usually it is preferable to use an inline function in such cases instead of a macro.<br>
660 <h4 class="right">Right:</h4>
662 #define WBStopButtonTitle() \
663 NSLocalizedString(@"Stop", @"Stop button title")
666 <h4 class="wrong">Wrong:</h4>
668 #define WB_STOP_BUTTON_TITLE \
669 NSLocalizedString(@"Stop", @"Stop button title")
671 #define WBStopButtontitle \
672 NSLocalizedString(@"Stop", @"Stop button title")
676 <li id="names-header-guards">#define, #ifdef "header guards" should be named exactly the same as the file (including case), replacing the '.' with a '_'.
677 <h4 class="right">Right:</h4>
680 #ifndef HTMLDocument_h
681 #define HTMLDocument_h
684 <h4 class="wrong">Wrong:</h4>
687 #ifndef _HTML_DOCUMENT_H_
688 #define _HTML_DOCUMENT_H_
693 <h3 id="punctuation">Other Punctuation</h3>
697 <li id="punctuation-member-init">Constructors for C++ classes should initialize all of their members using C++
698 initializer syntax. Each member (and superclass) should be indented on a separate
699 line, with the colon or comma preceding the member on that line.
701 <h4 class="right">Right:</h4>
703 MyClass::MyClass(Document* doc)
710 MyOtherClass::MyOtherClass()
716 <h4 class="wrong">Wrong:</h4>
718 MyClass::MyClass(Document* doc) : MySuperClass()
724 MyOtherClass::MyOtherClass() : MySuperClass() {}
727 <li id="punctuation-vector-index">Prefer index over iterator in Vector iterations for a terse, easier-to-read code.
729 <h4 class="right">Right:</h4>
731 size_t frameViewsCount = frameViews.size();
732 for (size_t i = i; i < frameViewsCount; ++i)
733 frameViews[i]->updateLayoutAndStyleIfNeededRecursive();
736 <h4 class="wrong">Wrong:</h4>
738 const Vector<RefPtr<FrameView> >::iterator end = frameViews.end();
739 for (Vector<RefPtr<FrameView> >::iterator it = frameViews.begin(); it != end; ++it)
740 (*it)->updateLayoutAndStyleIfNeededRecursive();
744 <h3 id="pointers">Pointers and References</h3>
748 <li id="pointers-non-cpp">Pointer types in non-C++ code — Pointer types should be written with a space between the
749 type and the * (so the * is adjacent to the following identifier if any).
751 <li id="pointers-cpp">Pointer and reference types in C++ code — Both pointer types and reference types
752 should be written with no space between the type name and the * or &.
754 <h4 class="right">Right:</h4>
756 Image* SVGStyledElement::doSomething(PaintInfo& paintInfo)
758 SVGStyledElement* element = static_cast<SVGStyledElement*>(node());
759 const KCDashArray& dashes = dashArray();
762 <h4 class="wrong">Wrong:</h4>
764 Image *SVGStyledElement::doSomething(PaintInfo &paintInfo)
766 SVGStyledElement *element = static_cast<SVGStyledElement *>(node());
767 const KCDashArray &dashes = dashArray();
770 <li id="pointers-out-argument">An out argument of a function should be passed by reference except rare cases where
771 it is optional in which case it should be passed by pointer.
773 <h4 class="right">Right:</h4>
775 void MyClass::getSomeValue(OutArgumentType& outArgument) const
777 outArgument = m_value;
780 void MyClass::doSomething(OutArgumentType* outArgument) const
784 *outArgument = m_value;
788 <h4 class="wrong">Wrong:</h4>
790 void MyClass::getSomeValue(OutArgumentType* outArgument) const
792 *outArgument = m_value;
798 <h3 id="include">#include Statements</h3>
802 <li id="include-config-h">All implementation files must #include "config.h" first. Header
803 files should never include "config.h".
805 <h4 class="right">Right:</h4>
809 #include "RenderObject.h"
810 #include "RenderView.h"
813 <h4 class="wrong">Wrong:</h4>
818 #include "RenderObject.h"
819 #include "RenderView.h"
823 <li id="include-primary">All implementation files must #include the primary header second,
824 just after "config.h". So for example, Node.cpp should include Node.h first,
825 before other files. This guarantees that each header's completeness is tested.
826 This also assures that each header can be compiled without requiring any other
827 header files be included first.
829 <li id="include-others">Other #include statements should be in sorted order (case sensitive, as
830 done by the command-line sort tool or the Xcode sort selection command).
831 Don't bother to organize them in a logical order.
833 <h4 class="right">Right:</h4>
835 // HTMLDivElement.cpp
837 #include "HTMLDivElement.h"
839 #include "Attribute.h"
840 #include "HTMLElement.h"
841 #include "QualifiedName.h"
845 <h4 class="wrong">Wrong:</h4>
847 // HTMLDivElement.cpp
848 #include "HTMLElement.h"
849 #include "HTMLDivElement.h"
850 #include "QualifiedName.h"
851 #include "Attribute.h"
854 <li id="include-system">Includes of system headers must come after includes of other headers.
856 <h4 class="right">Right:</h4>
859 #include "ArgumentEncoder.h"
860 #include "ProcessLauncher.h"
861 #include "WebPageProxyMessageKinds.h"
862 #include "WorkItem.h"
863 #include <QApplication>
864 #include <QLocalServer>
865 #include <QLocalSocket>
868 <h4 class="wrong">Wrong:</h4>
871 #include "ArgumentEncoder.h"
872 #include "ProcessLauncher.h"
873 #include <QApplication>
874 #include <QLocalServer>
875 #include <QLocalSocket>
876 #include "WebPageProxyMessageKinds.h"
877 #include "WorkItem.h"
882 <h3 id="using">"using" Statements</h3>
886 <li id="using-in-headers">In header files, do not use "using" statements in namespace
889 <h4 class="right">Right:</h4>
902 <h4 class="wrong">Wrong:</h4>
918 <li id="using-wtf">In header files in the WTF sub-library, however, it is acceptable
919 to use "using" declarations at the end of the file to import one
920 or more names in the WTF namespace into the global scope.
922 <h4 class="right">Right:</h4>
932 <h4 class="wrong">Wrong:</h4>
942 <h4 class="wrong">Wrong:</h4>
944 // runtime/JSObject.h
950 using WTF::PlacementNewAdopt;
954 <li id="using-in-cpp">In C++ implementation files, do not use "using" declarations
955 of any kind to import names in the standard template library. Directly qualify
956 the names at the point they're used instead.
958 <h4 class="right">Right:</h4>
960 // HTMLBaseElement.cpp
965 c = std::numeric_limits<int>::max()
967 } // namespace WebCore
969 <h4 class="wrong">Wrong:</h4>
971 // HTMLBaseElement.cpp
979 } // namespace WebCore
981 <h4 class="wrong">Wrong:</h4>
983 // HTMLBaseElement.cpp
991 } // namespace WebCore
995 <li id="using-nested-namespaces">In implementation files, if a "using namespace" statement is
996 for a nested namespace whose parent namespace is defined in the file,
997 put the statement inside that namespace definition.
999 <h4 class="right">Right:</h4>
1001 // HTMLBaseElement.cpp
1005 using namespace HTMLNames;
1007 } // namespace WebCore
1010 <h4 class="wrong">Wrong:</h4>
1012 // HTMLBaseElement.cpp
1014 using namespace WebCore::HTMLNames;
1018 } // namespace WebCore
1022 <li id="using-position">In implementation files, put all other "using" statements
1023 at the beginning of the file, before any namespace definitions and
1024 after any "include" statements.
1026 <h4 class="right">Right:</h4>
1028 // HTMLSelectElement.cpp
1030 using namespace other;
1034 } // namespace WebCore
1037 <h4 class="wrong">Wrong:</h4>
1039 // HTMLSelectElement.cpp
1043 using namespace other;
1045 } // namespace WebCore
1051 <h3 id="types">Types</h3>
1054 <li id="types-unsigned">
1055 Omit "int" when using "unsigned" modifier. Do not use "signed" modifier. Use "int" by itself instead.
1056 <h4 class="right">Right:</h4>
1062 <h4 class="wrong">Wrong:</h4>
1064 unsigned int a; // Doesn't omit "int".
1065 signed b; // Uses "singed" instead of "int".
1066 signed int c; // Doesn't omit "singed".
1071 <h3 id="classes">Classes</h3>
1074 <li id="classes-explicit">
1075 Use a constructor to do an implicit conversion when the argument is reasonably thought of as a type conversion and the type conversion is fast. Otherwise, use the explicit keyword or a function returning the type. This only applies to single argument constructors.
1076 <h4 class="right">Right:</h4>
1085 explicit Vector(int size); // Not a type conversion.
1086 PassOwnPtr<Vector> create(Array); // Costly conversion.
1091 <h4 class="wrong">Wrong:</h4>
1095 Task(ScriptExecutionContext*); // Not a type conversion.
1096 explicit Task(); // No arguments.
1097 explicit Task(ScriptExecutionContext*, Other); // More than one argument.
1103 <h3 id="comments">Comments</h3>
1105 <li id="comments-eol">Use only <i>one</i> space before end of line comments and in between sentences in comments.
1106 <h4 class="right">Right:</h4>
1108 f(a, b); // This explains why the function call was done. This is another sentence.
1111 <h4 class="wrong">Wrong:</h4>
1113 int i; // This is a comment with several spaces before it, which is a non-conforming style.
1114 double f; // This is another comment. There are two spaces before this sentence which is a non-conforming style.
1118 <li id="comments-sentences">
1119 Make comments look like sentences by starting with a capital letter and ending with a period (punctation). One exception may be end of line comments like this "if (x == y) // false for NaN".
1122 <li id="comments-fixme">
1123 Use FIXME: (without attribution) to denote items that need to be addressed in the future.
1124 <h4 class="right">Right:</h4>
1126 drawJpg(); // FIXME: Make this code handle jpg in addition to the png support.
1129 <h4 class="wrong">Wrong:</h4>
1131 drawJpg(); // FIXME(joe): Make this code handle jpg in addition to the png support.
1134 drawJpg(); // TODO: Make this code handle jpg in addition to the png support.
1141 var listItems = document.getElementsByTagName('li');
1143 for (var i = 0; i < listItems.length; ++i) {
1144 var item = listItems[i];
1147 var button = document.createElement('span');
1148 button.className = 'idbutton';
1149 button.setAttribute('data-target-id', listItems[i].id);
1150 button.appendChild(document.createTextNode(' [' + listItems[i].id + ']'));
1151 button.addEventListener('click', function(event) {
1152 window.location.hash = '#' + event.currentTarget.getAttribute('data-target-id');
1154 listItems[i].appendChild(button);
1155 if (idsUsed[item.id])
1156 alert('The id ' + item.id + ' is used more than once in this document.');
1157 idsUsed[item.id] = 1;
1163 include("../footer.inc");