webkit  2cdf99a9e3038c7e01b3c37e8ad903ecbe5eecf1
https://github.com/WebKit/webkit
String :: short:: short Class Reference

Detailed Description

```

Wrong:

```cpp

[](#names-bool) Precede boolean values with words like "is" and "did".
###### Right:
```cpp
bool isValid;
bool didSendData;
Wrong:
bool valid;
bool sentData;

[](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.

Right:
void setCount(size_t); // sets m_count
size_t count(); // returns m_count
Wrong:
void setCount(size_t); // sets m_theCount
size_t getCount();

[](names-out-argument) Precede getters that return values through out arguments with the word "get".

Right:
void getInlineBoxAndOffset(InlineBox*&, int& caretOffset) const;
Wrong:
void inlineBoxAndOffset(InlineBox*&, int& caretOffset) const;

[](names-verb) Use descriptive verbs in function names.

Right:
bool convertToASCII(short*, size_t);
Wrong:
bool toASCII(short*, size_t);

[](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.

Right:
void setCount(size_t);
void doSomething(ScriptExecutionContext*);
Wrong:
void setCount(size_t count);
void doSomething(ScriptExecutionContext* context);

[](names-enum-to-bool) Prefer enums to bools on function parameters if callers are likely to be passing constants, since named constants are easier to read at the call site. An exception to this rule is a setter function, where the name of the function already makes clear what the boolean is.

Right:
doSomething(something, AllowFooBar);
paintTextWithShadows(context, ..., textStrokeWidth > 0, isHorizontal());
setResizable(false);
Wrong:
doSomething(something, false);
setResizable(NotResizable);

[](names-objc-methods) Objective-C method names should follow the Cocoa naming guidelines — they should read like a phrase and each piece of the selector should start with a lowercase letter and use intercaps.

[](names-enum-members) Enum members should use InterCaps with an initial capital letter.

[](names-const-to-define) Prefer const to #define. Prefer inline functions to macros.

[](names-define-constants) #defined constants should use all uppercase names with words separated by underscores.

[](names-define-non-const) 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.

Right:
#define WBStopButtonTitle() \
NSLocalizedString(@"Stop", @"Stop button title")
Wrong:
#define WB_STOP_BUTTON_TITLE \
NSLocalizedString(@"Stop", @"Stop button title")
#define WBStopButtontitle \
NSLocalizedString(@"Stop", @"Stop button title")

[](header-guards) Use #pragma once instead of #define and #ifdef for header guards.

Right:
// HTMLDocument.h
#pragma once
Wrong:
// HTMLDocument.h
#ifndef HTMLDocument_h
#define HTMLDocument_h

[](names-protectors-this) Ref and RefPtr objects meant to protect this from deletion should be named "protectedThis".

Right:
RefPtr<Node> protectedThis(this);
Ref<Element> protectedThis(*this);
RefPtr<Widget> protectedThis = this;
Wrong:
RefPtr<Node> protector(this);
Ref<Node> protector = *this;
RefPtr<Widget> self(this);
Ref<Element> elementRef(*this);

[](names-protectors) Ref and RefPtr objects meant to protect variables other than this from deletion should be named either "protector", or "protected" combined with the capitalized form of the variable name.

Right:
RefPtr<Element> protector(&element);
RefPtr<Element> protector = &element;
RefPtr<Node> protectedNode(node);
RefPtr<Widget> protectedMainWidget(m_mainWidget);
RefPtr<Loader> protectedFontLoader = m_fontLoader;
Wrong:
RefPtr<Node> nodeRef(&rootNode);
Ref<Element> protect(*element);
RefPtr<Node> protectorNode(node);
RefPtr<Widget> protected = widget;

Other Punctuation

[](#punctuation-member-init) 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.

Right:
MyClass::MyClass(Document* document)
: MySuperClass()
, m_myMember(0)
, m_document(document)
{
}
MyOtherClass::MyOtherClass()
: MySuperClass()
{
}
Wrong:
MyClass::MyClass(Document* document) : MySuperClass()
{
m_myMember = 0;
m_document = document;
}
MyOtherClass::MyOtherClass() : MySuperClass() {}

[](#punctuation-vector-index) Prefer index over iterator in Vector iterations for terse, easier-to-read code.

Right:
for (auto& frameView : frameViews)
frameView->updateLayoutAndStyleIfNeededRecursive();

OK:

unsigned frameViewsCount = frameViews.size();
for (unsigned i = 0; i < frameViewsCount; ++i)
frameViews[i]->updateLayoutAndStyleIfNeededRecursive();
Wrong:
const Vector<RefPtr<FrameView> >::iterator end = frameViews.end();
for (Vector<RefPtr<FrameView> >::iterator it = frameViews.begin(); it != end; ++it)
(*it)->updateLayoutAndStyleIfNeededRecursive();

Pointers and References

[](#pointers-non-cpp) Pointer types in non-C++ code Pointer types should be written with a space between the type and the * (so the * is adjacent to the following identifier if any).

[](#pointers-cpp) Pointer and reference types in C++ code Both pointer types and reference types should be written with no space between the type name and the * or &.

Right:
Image* SVGStyledElement::doSomething(PaintInfo& paintInfo)
{
SVGStyledElement* element = static_cast<SVGStyledElement*>(node());
const KCDashArray& dashes = dashArray();
Wrong:
Image *SVGStyledElement::doSomething(PaintInfo &paintInfo)
{
SVGStyledElement *element = static_cast<SVGStyledElement *>(node());
const KCDashArray &dashes = dashArray();

[](#pointers-out-argument) An out argument of a function should be passed by reference except rare cases where it is optional in which case it should be passed by pointer.

Right:
void MyClass::getSomeValue(OutArgumentType& outArgument) const
{
outArgument = m_value;
}
void MyClass::doSomething(OutArgumentType* outArgument) const
{
doSomething();
if (outArgument)
*outArgument = m_value;
}
Wrong:
void MyClass::getSomeValue(OutArgumentType* outArgument) const
{
*outArgument = m_value;
}

include Statements

[](include-config-h) All implementation files must include config.h first. Header files should never include config.h.

Right:
// RenderLayer.h
#include "Node.h"
#include "RenderObject.h"
#include "RenderView.h"
Wrong:
// RenderLayer.h
#include "config.h"
#include "RenderObject.h"
#include "RenderView.h"
#include "Node.h"

[](include-primary) 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.

[](include-others) 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.

Right:
// HTMLDivElement.cpp
#include "config.h"
#include "HTMLDivElement.h"
#include "Attribute.h"
#include "HTMLElement.h"
#include "QualifiedName.h"
Wrong:
// HTMLDivElement.cpp
#include "HTMLElement.h"
#include "HTMLDivElement.h"
#include "QualifiedName.h"
#include "Attribute.h"

[](include-system) Includes of system headers must come after includes of other headers.

Right:
// ConnectionQt.cpp
#include "ArgumentEncoder.h"
#include "WebPageProxyMessageKinds.h"
#include "WorkItem.h"
#include <QApplication>
#include <QLocalServer>
#include <QLocalSocket>
Wrong:
// ConnectionQt.cpp
#include "ArgumentEncoder.h"
#include <QApplication>
#include <QLocalServer>
#include <QLocalSocket>
#include "WebPageProxyMessageKinds.h"
#include "WorkItem.h"

"using" Statements

[](#using-in-headers) In header files, do not use "using" statements in namespace (or global) scope.

Right:
// wtf/Vector.h
namespace WTF {
class VectorBuffer {
using std::min;
...
};
} // namespace WTF
Wrong:
// wtf/Vector.h
namespace WTF {
using std::min;
class VectorBuffer {
...
};
} // namespace WTF

[](#using-wtf) 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.

Right:
// wtf/Vector.h
namespace WTF {
} // namespace WTF
Wrong:
// wtf/Vector.h
namespace WTF {
} // namespace WTF
using namespace WTF;
Wrong:
// runtime/JSObject.h
namespace WTF {
} // namespace WTF
using WTF::PlacementNewAdopt;

[](#using-in-cpp) In C++ implementation files, do not use "using" declarations of any kind to import names in the standard template library. Directly qualify the names at the point they're used instead.

Right:
// HTMLBaseElement.cpp
namespace WebCore {
} // namespace WebCore
Wrong:
// HTMLBaseElement.cpp
using std::swap;
namespace WebCore {
swap(a, b);
} // namespace WebCore
Wrong:
// HTMLBaseElement.cpp
using namespace std;
namespace WebCore {
swap(a, b);
} // namespace WebCore

[](#using-nested-namespaces) 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.

Right:
// HTMLBaseElement.cpp
namespace WebCore {
using namespace HTMLNames;
} // namespace WebCore
Wrong:
// HTMLBaseElement.cpp
using namespace WebCore::HTMLNames;
namespace WebCore {
} // namespace WebCore

[](#using-position) In implementation files, put all other "using" statements at the beginning of the file, before any namespace definitions and after any "include" statements.

Right:
// HTMLSelectElement.cpp
using namespace other;
namespace WebCore {
} // namespace WebCore
Wrong:
// HTMLSelectElement.cpp
namespace WebCore {
using namespace other;
} // namespace WebCore

Types

[](types-unsigned) Omit "int" when using "unsigned" modifier. Do not use "signed" modifier. Use "int" by itself instead.

Right:
unsigned a;
int b;
Wrong:
unsigned int a; // Doesn't omit "int".
signed b; // Uses "signed" instead of "int".
signed int c; // Doesn't omit "signed".

Classes

[](#classes-explicit) 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.

Right:
class LargeInt {
public:
LargeInt(int);
...
class Vector {
public:
explicit Vector(int size); // Not a type conversion.
Vector create(Array); // Costly conversion.
...
Wrong:
class Task {
public:
Task(ScriptExecutionContext&); // Not a type conversion.
explicit Task(); // No arguments.
explicit Task(ScriptExecutionContext&, Other); // More than one argument.
...

Singleton pattern

[](#singleton-static-member) Use a static member function named "singleton()" to access the instance of the singleton.

Right:
class MySingleton {
public:
static MySingleton& singleton();
...
Wrong:
class MySingleton {
public:
static MySingleton& shared();
...
Wrong:
class MySingleton {
...
};
MySingleton& mySingleton(); // free function.

Comments

[](#comments-eol) Use only one space before end of line comments and in between sentences in comments.

Right:
f(a, b); // This explains why the function call was done. This is another sentence.
Wrong:
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.

[](#comments-sentences) 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.

[](#comments-fixme) Use FIXME: (without attribution) to denote items that need to be addressed in the future.

Right:
drawJpg(); // FIXME: Make this code handle jpg in addition to the png support.
Wrong:
drawJpg(); // FIXME(joe): Make this code handle jpg in addition to the png support.
drawJpg(); // TODO: Make this code handle jpg in addition to the png support.

Overriding Virtual Methods

[](#override-methods) The base level declaration of a virtual method inside a class must be declared with the virtual keyword. All subclasses of that class must either specify the override keyword when overriding the virtual method or the final keyword when overriding the virtual method and requiring that no further subclasses can override it. You never want to annotate a method with more than one of the virtual, override, or final keywords.

Right:
class Person {
public:
virtual String description() { ... };
}
class Student : public Person {
public:
String description() override { ... }; // This is correct because it only contains the "override" keyword to indicate that the method is overridden.
}
class Person {
public:
virtual String description() { ... };
}
class Student : public Person {
public:
String description() final { ... }; // This is correct because it only contains the "final" keyword to indicate that the method is overridden and that no subclasses of "Student" can override "description".
}
Wrong:
class Person {
public:
virtual String description() { ... };
}
class Student : public Person {
public:
virtual String description() override { ... }; // This is incorrect because it uses both the "virtual" and "override" keywords to indicate that the method is overridden. Instead, it should only use the "override" keyword.
}
class Person {
public:
virtual String description() { ... };
}
class Student : public Person {
public:
virtual String description() final { ... }; // This is incorrect because it uses both the "virtual" and "final" keywords to indicate that the method is overridden and final. Instead, it should only use the "final" keyword.
}
class Person {
public:
virtual String description() { ... };
}
class Student : public Person {
public:
virtual String description() { ... }; // This is incorrect because it uses the "virtual" keyword to indicate that the method is overridden.
}

The documentation for this class was generated from the following file: