Skip to content

Instantly share code, notes, and snippets.

@pcperini
Last active October 12, 2015 00:17
Show Gist options
  • Save pcperini/3941614 to your computer and use it in GitHub Desktop.
Save pcperini/3941614 to your computer and use it in GitHub Desktop.
Objective-C Style Guide

General

Follow ALL Apple Naming Conventions!

Whitespace

  • Spaces, not tabs.

Documentation

  • Comments should be hard-wrapped at 80 characters.
  • Use #pragma marks to categorize methods and protocol implementations.

Declarations

  • 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);

Expressions

  • 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 BOOLs and determining existance (!x over x == 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;

Control Structures

  • 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

  • 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
};

Literals

  • 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

  • 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.

Interface Builder

  • 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;

Constants

  • (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 the extern keyword.
// in .h
extern NSString *MBSomethingDidHappenNotification;

// in .m
NSString *MBSomethingDidHappenNotification = @"MBSomethingDidHappenNotification";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment