blob: 307cfddd4300a955608389da7c30a7c81e24e591 [file] [log] [blame]
<?php
$title="WebKit Coding Style Guidelines";
$extra_head_content = <<<END
<style type="text/css">
pre .code {
background-color: #F2F2F2;
}
.right {
color: #080 !important;
}
.wrong {
color: #f00 !important;
}
</style>
END;
include("../header.inc");
?>
<h2>WebKit Coding Style Guidelines</h2>
<h3>Indentation</h3>
<ol>
<li> Use spaces, not tabs. Tabs should only appear in files that require them for semantic meaning, like Makefiles.
</li>
<li> The indent size is 4 spaces.
<h4 class="right">Right:</h4>
<pre class="code">
int main()
{
return 0;
}
</pre>
<h4 class="wrong">Wrong:</h4>
<pre class="code">
int main()
{
return 0;
}
</pre>
</li>
<li>The contents of an outermost <code>namespace</code> block (and any nested namespaces with the same scope)
should not be indented. The contents of other nested namespaces should be indented.
<h4 class="right">Right:</h4>
<pre class="code">
// Document.h
namespace WebCore {
class Document {
Document();
...
};
namespace NestedNamespace {
...
}
} // namespace WebCore
// Document.cpp
namespace WebCore {
Document::Document()
{
...
}
} // namespace WebCore
</pre>
<h4 class="wrong">Wrong:</h4>
<pre class="code">
// Document.h
namespace WebCore {
class Document {
Document();
...
};
namespace NestedNamespace {
...
}
} // namespace WebCore
// Document.cpp
namespace WebCore {
Document::Document()
{
...
}
} // namespace WebCore
</pre>
</li>
<li>A case label should line up with its switch statement. The case statement is indented.
<h4 class="right">Right:</h4>
<pre class="code">
switch (condition) {
case fooCondition:
case barCondition:
i++;
break;
default:
i--;
}
</pre>
<h4 class="wrong">Wrong:</h4>
<pre class="code">
switch (condition) {
case fooCondition:
case barCondition:
i++;
break;
default:
i--;
}
</pre>
</li>
<li>Boolean expressions at the same nesting level that span multiple lines should
have their operators on the left side of the line instead of the right side.
<h4 class="right">Right:</h4>
<pre class="code">
if (attr->name() == srcAttr
|| attr->name() == lowsrcAttr
|| (attr->name() == usemapAttr && attr->value().domString()[0] != '#'))
return;
</pre>
<h4 class="wrong">Wrong:</h4>
<pre class="code">
if (attr->name() == srcAttr ||
attr->name() == lowsrcAttr ||
(attr->name() == usemapAttr && attr->value().domString()[0] != '#'))
return;
</pre>
</li>
</ol>
<h3>Spacing</h3>
<ol>
<li>Do not place spaces around unary operators.
<h4 class="right">Right:</h4>
<pre class="code">
i++;
</pre>
<h4 class="wrong">Wrong:</h4>
<pre class="code">
i ++;
</pre>
</li>
<li><em>Do</em> place spaces around binary and ternary operators.
<h4 class="right">Right:</h4>
<pre class="code">
y = m * x + b;
f(a, b);
c = a | b;
return condition ? 1 : 0;
</pre>
<h4 class="wrong">Wrong:</h4>
<pre class="code">
y=m*x+b;
f(a,b);
c = a|b;
return condition ? 1:0;
</pre>
</li>
<li>Do not place spaces before comma and semicolon.
<h4 class="right">Right:</h4>
<pre class="code">
for (int i = 0; i &lt; 10; ++i)
doSomething();
f(a, b);
</pre>
<h4 class="wrong">Wrong:</h4>
<pre class="code">
for (int i = 0 ; i &lt; 10 ; ++i)
doSomething();
f(a , b) ;
</pre>
</li>
<li>Place spaces between control statements and their parentheses.
<h4 class="right">Right:</h4>
<pre class="code">
if (condition)
doIt();
</pre>
<h4 class="wrong">Wrong:</h4>
<pre class="code">
if(condition)
doIt();
</pre>
</li>
<li>Do not place spaces between a function and its parentheses, or between a parenthesis and its content.
<h4 class="right">Right:</h4>
<pre class="code">
f(a, b);
</pre>
<h4 class="wrong">Wrong:</h4>
<pre class="code">
f (a, b);
f( a, b );
</pre>
</li>
</ol>
<h3>Line breaking</h3>
<ol>
<li>Each statement should get its own line.
<h4 class="right">Right:</h4>
<pre class="code">
x++;
y++;
if (condition)
doIt();
</pre>
<h4 class="wrong">Wrong:</h4>
<pre class="code">
x++; y++;
if (condition) doIt();
</pre>
</li>
<li>An <code>else</code> statement should go on the same line as a preceding close brace if one is present,
else it should line up with the <code>if</code> statement.
<h4 class="right">Right:</h4>
<pre class="code">
if (condition) {
...
} else {
...
}
if (condition)
doSomething();
else
doSomethingElse();
if (condition)
doSomething();
else {
...
}
</pre>
<h4 class="wrong">Wrong:</h4>
<pre class="code">
if (condition) {
...
}
else {
...
}
if (condition) doSomething(); else doSomethingElse();
if (condition) doSomething(); else {
...
}
</pre>
</li>
<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.
<h4 class="right">Right:</h4>
<pre class="code">
if (condition) {
...
return someValue;
}
if (condition) {
...
}
</pre>
<h4 class="wrong">Wrong:</h4>
<pre class="code">
if (condition) {
...
return someValue;
} else if (condition) {
...
}
</pre>
</li>
</ol>
<h3>Braces</h3>
<ol>
<li> Function definitions: place each brace on its own line.
<h4 class="right">Right:</h4>
<pre class="code">
int main()
{
...
}
</pre>
<h4 class="wrong">Wrong:</h4>
<pre class="code">
int main() {
...
}
</pre>
</li>
<li> Other braces: place the open brace on the line preceding the code block; place the close brace on its own line.
<h4 class="right">Right:</h4>
<pre class="code">
class MyClass {
...
};
namespace WebCore {
...
}
for (int i = 0; i &lt; 10; ++i) {
...
}
</pre>
<h4 class="wrong">Wrong:</h4>
<pre class="code">
class MyClass
{
...
};
</pre>
<li>One-line control clauses should not use braces unless comments are included
or a single statement spans multiple lines.
<h4 class="right">Right:</h4>
<pre class="code">
if (condition)
doIt();
if (condition) {
// Some comment
doIt();
}
if (condition) {
myFunction(reallyLongParam1, reallyLongParam2, ...
reallyLongParam5);
}
</pre>
<h4 class="wrong">Wrong:</h4>
<pre class="code">
if (condition) {
doIt();
}
if (condition)
// Some comment
doIt();
if (condition)
myFunction(reallyLongParam1, reallyLongParam2, ...
reallyLongParam5);
</pre>
</li>
<li>Control clauses without a body should use empty braces:
<h4 class="right">Right:</h4>
<pre class="code">
for ( ; current; current = current->next) { }
</pre>
<h4 class="wrong">Wrong:</h4>
<pre class="code">
for ( ; current; current = current->next);
</pre>
</li>
</ol>
<h3>Null, false and 0</h3>
<ol>
<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>
<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>
<li>Tests for true/false, null/non-null, and zero/non-zero should all be done without equality comparisons.<br>
<h4 class="right">Right:</h4>
<pre class="code">
if (condition)
doIt();
if (!ptr)
return;
if (!count)
return;
</pre>
<h4 class="wrong">Wrong:</h4>
<pre class="code">
if (condition == true)
doIt();
if (ptr == NULL)
return;
if (count == 0)
return;
</pre>
</li>
<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>
</ol>
<h3>Floating point literals</h3>
<ol>
<li>Unless required in order to force floating point math, do not append
<code>.0</code>, <code>.f</code> and <code>.0f</code> to floating point
literals.
<h4 class="right">Right:</h4>
<pre class="code">
const double duration = 60;
void setDiameter(float diameter)
{
radius = diameter / 2;
}
setDiameter(10);
const int framesPerSecond = 12;
double frameDuration = 1.0 / framesPerSecond;
</pre>
<h4 class="wrong">Wrong:</h4>
<pre class="code">
const double duration = 60.0;
void setDiameter(float diameter)
{
radius = diameter / 2.f;
}
setDiameter(10.f);
const int framesPerSecond = 12;
double frameDuration = 1 / framesPerSecond; // integer division
</pre>
</li>
</ol>
<h3>Names</h3>
<ol>
<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.
<h4 class="right">Right:</h4>
<pre class="code">
struct Data;
size_t bufferSize;
class HTMLDocument;
String mimeType();
</pre>
<h4 class="wrong">Wrong:</h4>
<pre class="code">
struct data;
size_t buffer_size;
class HtmlDocument;
String MIMEType();
</pre>
</li>
<li>Use full words, except in the rare case where an abbreviation would be more canonical and easier to understand.
<h4 class="right">Right:</h4>
<pre class="code">
size_t characterSize;
size_t length;
short tabIndex; // more canonical
</pre>
<h4 class="wrong">Wrong:</h4>
<pre class="code">
size_t charSize;
size_t len;
short tabulationIndex; // bizarre
</pre>
</li>
<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_".
<h4 class="right">Right:</h4>
<pre class="code">
class String {
public:
...
private:
short m_length;
};
</pre>
<h4 class="wrong">Wrong:</h4>
<pre class="code">
class String {
public:
...
short length;
};
</pre>
</li>
<li>Prefix Objective-C instance variables with "_".
<h4 class="right">Right:</h4>
<pre class="code">
@class String
...
short _length;
@end
</pre>
<h4 class="wrong">Wrong:</h4>
<pre class="code">
@class String
...
short length;
@end
</pre>
</li>
<li>Precede boolean values with words like "is" and "did".
<h4 class="right">Right:</h4>
<pre class="code">
bool isValid;
bool didSendData;
</pre>
<h4 class="wrong">Wrong:</h4>
<pre class="code">
bool valid;
bool sentData;
</pre>
</li>
<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.
<h4 class="right">Right:</h4>
<pre class="code">
void setCount(size_t); // sets m_count
size_t count(); // returns m_count
</pre>
<h4 class="wrong">Wrong:</h4>
<pre class="code">
void setCount(size_t); // sets m_theCount
size_t getCount();
</pre>
</li>
<li>Use descriptive verbs in function names.
<h4 class="right">Right:</h4>
<pre class="code">
bool convertToASCII(short*, size_t);
</pre>
<h4 class="wrong">Wrong:</h4>
<pre class="code">
bool toASCII(short*, size_t);
</pre>
</li>
<li>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.
<h4 class="right">Right:</h4>
<pre class="code">
void setCount(size_t);
void doSomething(ScriptExecutionContext*);
</pre>
<h4 class="wrong">Wrong:</h4>
<pre class="code">
void setCount(size_t count);
void doSomething(ScriptExecutionContext* context);
</pre>
</li>
<li>Objective-C method names should follow the Cocoa naming guidelines &mdash;
they should read like a phrase and each piece of the selector should
start with a lowercase letter and use intercaps.</li>
<li>Enum members should user InterCaps with an initial capital letter.</li>
<li>Prefer const to #define. Prefer inline functions to macros.</li>
<li>#defined constants should use all uppercase names with words separated by underscores.</li>
<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 preferable to use an inline function in such cases instead of a macro.<br>
<h4 class="right">Right:</h4>
<pre class="code">
#define WBStopButtonTitle() \
NSLocalizedString(@"Stop", @"Stop button title")
</pre>
<h4 class="wrong">Wrong:</h4>
<pre class="code">
#define WB_STOP_BUTTON_TITLE \
NSLocalizedString(@"Stop", @"Stop button title")
#define WBStopButtontitle \
NSLocalizedString(@"Stop", @"Stop button title")
</pre>
</li>
<li>#define, #ifdef "header guards" should be named exactly the same as the file (including case), replacing the '.' with a '_'.
<h4 class="right">Right:</h4>
<pre class="code">
// HTMLDocument.h
#ifndef HTMLDocument_h
#define HTMLDocument_h
</pre>
<h4 class="wrong">Wrong:</h4>
<pre class="code">
// HTMLDocument.h
#ifndef _HTML_DOCUMENT_H_
#define _HTML_DOCUMENT_H_
</pre>
</li>
</ol>
<h3>Other Punctuation</h3>
<ol>
<li>Constructors for C++ classes should initialize all of their members using C++
initializer syntax. Each member (and superclass) should be indented on a separate
line, with the colon or comma preceding the member on that line.
<h4 class="right">Right:</h4>
<pre class="code">
MyClass::MyClass(Document* doc)
: MySuperClass()
, m_myMember(0)
, m_doc(doc)
{
}
MyOtherClass::MyOtherClass()
: MySuperClass()
{
}
</pre>
<h4 class="wrong">Wrong:</h4>
<pre class="code">
MyClass::MyClass(Document* doc) : MySuperClass()
{
m_myMember = 0;
m_doc = doc;
}
MyOtherClass::MyOtherClass() : MySuperClass() {}
</pre>
<li>Pointer types in non-C++ code &mdash; Pointer types should be written with a space between the
type and the * (so the * is adjacent to the following identifier if any).
<li>Pointer and reference types in C++ code &mdash; Both pointer types and reference types
should be written with no space between the type name and the * or &amp;.
<h4 class="right">Right:</h4>
<pre class="code">
Image* SVGStyledElement::doSomething(PaintInfo&amp; paintInfo)
{
SVGStyledElement* element = static_cast&lt;SVGStyledElement*>(node());
const KCDashArray&amp; dashes = dashArray();
</pre>
<h4 class="wrong">Wrong:</h4>
<pre class="code">
Image *SVGStyledElement::doSomething(PaintInfo &amp;paintInfo)
{
SVGStyledElement *element = static_cast&lt;SVGStyledElement *>(node());
const KCDashArray &amp;dashes = dashArray();
</pre>
</ol>
<h3>#include Statements</h3>
<ol>
<li>All implementation files must #include "config.h" first. Header
files should never include "config.h".
<h4 class="right">Right:</h4>
<pre class="code">
// RenderLayer.h
#include "Node.h"
#include "RenderObject.h"
#include "RenderView.h"
</pre>
<h4 class="wrong">Wrong:</h4>
<pre class="code">
// RenderLayer.h
#include "config.h"
#include "RenderObject.h"
#include "RenderView.h"
#include "Node.h"
</pre>
<li>All implementation files must #include the primary header second,
just after "config.h". So for example, Node.cpp should include Node.h first,
before other files. This guarantees that each header's completeness is tested.
This also assures that each header can be compiled without requiring any other
header files be included first.
<li>Other #include statements should be in sorted order (case sensitive, as
done by the command-line sort tool or the Xcode sort selection command).
Don't bother to organize them in a logical order.
<h4 class="right">Right:</h4>
<pre class="code">
// HTMLDivElement.cpp
#include "config.h"
#include "HTMLDivElement.h"
#include "Attribute.h"
#include "HTMLElement.h"
#include "QualifiedName.h"
</pre>
<h4 class="wrong">Wrong:</h4>
<pre class="code">
// HTMLDivElement.cpp
#include "HTMLElement.h"
#include "HTMLDivElement.h"
#include "QualifiedName.h"
#include "Attribute.h"
</pre>
<li>Includes of system headers must come after includes of other headers.
<h4 class="right">Right:</h4>
<pre class="code">
// ConnectionQt.cpp
#include "ArgumentEncoder.h"
#include "ProcessLauncher.h"
#include "WebPageProxyMessageKinds.h"
#include "WorkItem.h"
#include &lt;QApplication&gt;
#include &lt;QLocalServer&gt;
#include &lt;QLocalSocket&gt;
</pre>
<h4 class="wrong">Wrong:</h4>
<pre class="code">
// ConnectionQt.cpp
#include "ArgumentEncoder.h"
#include "ProcessLauncher.h"
#include &lt;QApplication&gt;
#include &lt;QLocalServer&gt;
#include &lt;QLocalSocket&gt;
#include "WebPageProxyMessageKinds.h"
#include "WorkItem.h"
</pre>
</li>
</ol>
<h3>"using" Statements</h3>
<ol>
<li>In header files, do not use "using" statements in namespace
(or global) scope.
<h4 class="right">Right:</h4>
<pre class="code">
// wtf/Vector.h
namespace WTF {
class VectorBuffer {
using std::min;
...
};
} // namespace WTF
</pre>
<h4 class="wrong">Wrong:</h4>
<pre class="code">
// wtf/Vector.h
namespace WTF {
using std::min;
class VectorBuffer {
...
};
} // namespace WTF
</pre>
</li>
<li>In header files in the WTF sub-library, however, it is acceptable
to use "using" declarations at the end of the file to import one
or more names in the WTF namespace into the global scope.
<h4 class="right">Right:</h4>
<pre class="code">
// wtf/Vector.h
namespace WTF {
} // namespace WTF
using WTF::Vector;
</pre>
<h4 class="wrong">Wrong:</h4>
<pre class="code">
// wtf/Vector.h
namespace WTF {
} // namespace WTF
using namespace WTF;
</pre>
<h4 class="wrong">Wrong:</h4>
<pre class="code">
// runtime/UString.h
namespace WTF {
} // namespace WTF
using WTF::PlacementNewAdopt;
</pre>
</li>
<li>In C++ implementation files, do not use statements of the form
"using std::foo" to import names in the standard template library.
Use "using namespace std" instead.
<h4 class="right">Right:</h4>
<pre class="code">
// HTMLBaseElement.cpp
using namespace std;
namespace WebCore {
} // namespace WebCore
</pre>
<h4 class="wrong">Wrong:</h4>
<pre class="code">
// HTMLBaseElement.cpp
using std::swap;
namespace WebCore {
} // namespace WebCore
</pre>
</li>
<li>In implementation files, if a "using namespace" statement is
for a nested namespace whose parent namespace is defined in the file,
put the statement inside that namespace definition.
<h4 class="right">Right:</h4>
<pre class="code">
// HTMLBaseElement.cpp
namespace WebCore {
using namespace HTMLNames;
} // namespace WebCore
</pre>
<h4 class="wrong">Wrong:</h4>
<pre class="code">
// HTMLBaseElement.cpp
using namespace WebCore::HTMLNames;
namespace WebCore {
} // namespace WebCore
</pre>
</li>
<li>In implementation files, put all other "using" statements
at the beginning of the file, before any namespace definitions and
after any "include" statements.
<h4 class="right">Right:</h4>
<pre class="code">
// HTMLSelectElement.cpp
using namespace std;
namespace WebCore {
} // namespace WebCore
</pre>
<h4 class="wrong">Wrong:</h4>
<pre class="code">
// HTMLSelectElement.cpp
namespace WebCore {
using namespace std;
} // namespace WebCore
</pre>
</li>
</ol>
<h3>Classes</h3>
<ol>
<li>
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.
<h4 class="right">Right:</h4>
<pre class="code">
class LargeInt {
public:
LargeInt(int);
...
class Vector {
public:
explicit Vector(int size); // Not a type conversion.
PassOwnPtr&lt;Vector&gt; create(Array); // Costly conversion.
...
</pre>
<h4 class="wrong">Wrong:</h4>
<pre class="code">
class Task {
public:
Task(ScriptExecutionContext*); // Not a type conversion.
explicit Task(); // No arguments.
explicit Task(ScriptExecutionContext*, Other); // More than one argument.
...
</pre>
</li>
</ol>
<h3>Comments</h3>
<ol>
<li>Use only <i>one</i> space before end of line comments and in between sentences in comments.
<h4 class="right">Right:</h4>
<pre class="code">
f(a, b); // This explains why the function call was done. This is another sentence.
</pre>
<h4 class="wrong">Wrong:</h4>
<pre class="code">
int i; // This is a comment with several spaces before it, which is a non-conforming style.
double f; // This is another comment. There are two spaces before this sentence which is a non-conforming style.
</pre>
</li>
<li>
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".
</li>
<li>
Use FIXME: (without attribution) to denote items that need to be addressed in the future.
</li>
<h4 class="right">Right:</h4>
<pre class="code">
drawJpg(); // FIXME: Make this code handle jpg in addition to the png support.
</pre>
<h4 class="wrong">Wrong:</h4>
<pre class="code">
drawJpg(); // FIXME(joe): Make this code handle jpg in addition to the png support.
</pre>
<pre class="code">
drawJpg(); // TODO: Make this code handle jpg in addition to the png support.
</pre>
</ol>
<?php
include("../footer.inc");
?>