- 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.
Right:
struct Data;
size_t bufferSize;
class HTMLDocument;
Wrong:
struct data;
size_t buffer_size;
class HtmlDocument;
- Use full words, except in the rare case where an abbreviation would be more canonical and easier to understand.
Right:
size_t characterSize;
size_t length;
short tabIndex; // more canonical
Wrong:
size_t charSize;
size_t len;
short tabulationIndex; // bizarre
- Prefix C++ data members with "m_".
Right:
class String {
...
short m_length;
};
Wrong:
class String {
...
short length;
};
- Prefix Objective-C instance variables with "_".
Right:
@class String
...
short _length;
@end
Wrong:
@class String
...
short length;
@end
- Precede boolean values with words like "is" and "did".
Right:
bool isValid;
bool didSendData;
Wrong:
bool valid;
bool sentData;
- 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();
- Use descriptive verbs in function names.
Right:
bool convertToASCII(short*, size_t);
Wrong:
bool toASCII(short*, size_t);
- Leave meaningless variable names out of function declarations.
Right:
void setCount(size_t);
Wrong:
void setCount(size_t count);
- 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.
- Enum members should user InterCaps with an initial capital letter.
- Prefer const to #define. Prefer inline functions to macros.
- #defined constants should use all uppercase names with words separated by underscores.
- 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")
- #define, #ifdef "header guards" should be named exactly the same as the file (including case), replacing the '.' with a '_'.
Right:
// HTMLDocument.h
#ifndef HTMLDocument_h
#define HTMLDocument_h
Wrong:
// HTMLDocument.h
#ifndef _HTML_DOCUMENT_H_
#define _HTML_DOCUMENT_H_