Follow ALL Apple Naming Conventions!
- Spaces, not tabs.
- Comments should be hard-wrapped at 80 characters.
- Use
#pragma mark
s to categorize methods and protocol implementations.
- Never declare an ivar.
- Prefer exposing an immutable type for a property if it being mutable is an implementation detail.
- For instances when being mutable is an implementation detail, redeclare the property in a class extension.
- Declare properties
readonly
if they are only set once in-init
. - Use
@synthesize
regardless of whether the compiler requires it. - C function declarations should have no space before the opening parenthesis, and should be namespaced just like a class.
void MBAwesomeFunction(BOOL hasSomeArgs);
- Never use dot-syntax outside of structs.
- Use object literals and boxed expressions over the older, grosser alternatives.
- Comparisons should be explicit for everything except
BOOL
s and determining existance (!x
overx == nil
). - Prefer positive comparisons to negative, except when determining existance.
- Long form ternary operators should be wrapped in parentheses and only used for assignment and arguments.
Blah *a = (stuff == thing)? foo : bar;
- Avoid short form,
nil
coalescing ternary operators.
Blah *b = thingThatCouldBeNil ?: defaultValue; // Don't do this
- There shouldn't be a space between a cast and the variable being cast.
NewType a = (NewType)b;
- Always surround
if
bodies with curly braces, unless breaking or returning. - All curly braces should begin and end on a new line.
- Put a single space after keywords and before their parentheses.
- Return and break early.
- No spaces between parentheses and their contents.
if (shitIsBad)
return;
if (something == nil)
{
// do stuff
}
else
{
// do other stuff
}
- Blocks should have a space between their return type and name.
- Block definitions should omit their return type when possible.
void (^blockName1)() = ^()
{
// do some things
};
id (^blockName2)(id) = ^id (id args)
{
// do some things
};
- Unless being used as a parameter, the contents of array and dictionary literals should be on new lines.
- Dictionary literals should have no space between the key and the colon, and a single space between colon and value.
- When building dictionary literals with multiple keys, extra space may be used before values to left-align for readibility.
NSArray *theShit = @[
@1,
@2,
@3
];
NSDictionary *keyedShit = @{
MBDidCreateStyleGuide: @YES
};
NSDictionary *keyedShit = @{
MBDidCreateStyleGuide: @YES
MBDidCreateDict: @YES
};
[anObject doSomethingWithThis: @[@"Hello", @"World"]];
- Never use subscript notation. It too quickly causes a quagmire of square brackets.
[theArray[5] doSomethingWithAnotherArray: @[@0, @1]]; // Don't fucking do this.
- Categories should be named for the sort of functionality they provide. Avoid creating umbrella categories.
- If you need to expose private methods for subclasses or unit testing, create a class extension named
Class+Private
.
- Use Interface Builder where ever possible, as it is much easier to maintain and adapt than code.
IBAction
should be named in a reactionary fashion.
- (IBAction)theButtonWasPressed:(id)sender;
- (Almost) never use magic numbers. Exceptions include multiplying by 1, -1, and 0.
- When possible, establish constants using the
const
keyword. Expose them when appropriate with theextern
keyword.
// in .h
extern NSString *MBSomethingDidHappenNotification;
// in .m
NSString *MBSomethingDidHappenNotification = @"MBSomethingDidHappenNotification";