blob: 48eea05ac37f4d8be2efe97653071edb7b7ea508 [file] [log] [blame]
<?php
$title="WebKit Coding Style Guidelines";
include("../header.inc");
?>
<style type="text/css">
pre .code {
background-color: #F2F2F2;
}
.right {
color: #080 !important;
}
.wrong {
color: #f00 !important;
}
</style>
<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>In a header, code inside a namespace should be indented.
<h4 class="right">Right:</h4>
<pre class="code">
// Document.h
namespace WebCore {
class Document {
Document();
...
};
} // namespace WebCore
</pre>
<h4 class="wrong">Wrong:</h4>
<pre class="code">
// Document.h
namespace WebCore {
class Document {
Document();
...
};
} // namespace WebCore
</pre>
</li>
<li>In an implementation file, code inside a namespace should <em>not</em> be indented.
<h4 class="right">Right:</h4>
<pre class="code">
// Document.cpp
namespace WebCore {
Document::Document()
{
...
}
} // namespace WebCore
</pre>
<h4 class="wrong">Wrong:</h4>
<pre class="code">
// 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>
</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>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 else statement should go on the same line as a preceding close brace.
<h4 class="right">Right:</h4>
<pre class="code">
if (condition) {
...
} else {
...
}
</pre>
<h4 class="wrong">Wrong:</h4>
<pre class="code">
if (condition) {
...
}
else {
...
}
</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
<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>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>Names</h3>
<ol>
<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.
<h4 class="right">Right:</h4>
<pre class="code">
struct Data;
size_t bufferSize;
class HTMLDocument;
</pre>
<h4 class="wrong">Wrong:</h4>
<pre class="code">
struct data;
size_t buffer_size;
class HtmlDocument;
</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>Prefix C++ data members with "m_".
<h4 class="right">Right:</h4>
<pre class="code">
class String {
...
short m_length;
};
</pre>
<h4 class="wrong">Wrong:</h4>
<pre class="code">
class String {
...
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.
<h4 class="right">Right:</h4>
<pre class="code">
void setCount(size_t);
</pre>
<h4 class="wrong">Wrong:</h4>
<pre class="code">
void setCount(size_t count);
</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 files must #include "config.h" first.
<li>All 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, to make sure it
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.
</ol>
<?php
include("../footer.inc");
?>