- Use CamelCase. Capitalize the first letter of a class. Lower-case the first letter of a variable or function name. Fully capitalize acronyms.
Right:
@implementation Data : @implementation HTMLDocument :
Wrong:
@implementation data : @implementation HtmlDocument :
- Multiple var declarations should be collapsed with commas.
Right:
var index = 0,
count = 5;
Wrong:
var index = 0;
var count = 5;
- Variable declarations should be created as needed, rather than up front.
Right:
- (BOOL)doSomething:(id)aFoo
{
var importantVariable = [aFoo message];
if (!importantVariable)
return;
var index = [aFoo count];
while (index--)
{
var innerVariable = [aFoo objectAtIndex:index];
}
}
Wrong:
- (BOOL)doSomething:(id)aFoo
{
var importantVariable = [aFoo message],
index = [aFoo count],
innerVariable;
if (!importantVariable)
return;
while (index--)
{
innerVariable = [aFoo objectAtIndex:index];
}
}
- Use full words, except in the rare case where an abbreviation would be more canonical and easier to understand.
Right:
var characterSize,
length,
tabIndex;
Wrong:
var charSize,
len,
tabulationIndex;
- Prefix Objective-J instance variables with "_".
Right:
@implementation String
{
unsigned _length;
}
@end
Wrong:
@implementation String
{
unsigned length;
}
@end
- Precede boolean values with words like "is" and "did".
Right:
var isValid;
var didSendData;
- (BOOL)isEditable;
- (BOOL)didReceiveResponse;
Wrong:
var valid;
var sentData;
- (BOOL)editable;
- (BOOL)receivedResponse;
- 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:(unsigned)aCount; - (unsigned)count;
Wrong:
- (unsigned)getCount;
- Use descriptive verbs in function names, and place desired types in comments.
Right:
function convertToASCII(/*String*/ aString)
Wrong:
function toASCII(str)
- Use descriptive parameter names that are not abbreviated.
Right:
- (void)convertString:(CPString)aString toFormat:(Format)aFormat;
- (void)appendSubviews:(CPArray)subviews inOrder:(BOOL)shouldBeInOrder;
Wrong:
- (void)convertString:(CPString)str toFormat:(Format)f;
- (void)appendSubviews:(CPArray)s inOrder:(BOOL)flag;
- Use descriptive parameter types, despite not being fully supported in JavaScript. At some point we will be adding optional static typing, and even until then this serves as a much better indicator of what the method expects. Of course, if the method can truly take any input or return any output, it is perfectly acceptable to use "id", "CPObject", or "var".
Right:
- (char)characterAtIndex:(unsigned)anIndex;
- (void)insertObject:(id)anObject;
Wrong:
- (String)characterAtIndex:(var)index;
- Objective-J 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.
- #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).
Right:
#define StopButtonTitle() \
CPLocalizedString(@"Stop", @"Stop button title")
Wrong:
#define STOP_BUTTON_TITLE \
CPLocalizedString(@"Stop", @"Stop button title")
#define StopButtontitle \
CPLocalizedString(@"Stop", @"Stop button title")