Objective-C Style Guide

This is the style guide that I follow when programming in Objective-C, this is for my own reference, but other people may find this useful.

General

  • Use modern objective-c 2.0 syntax
  • Use ARC whenever possible.
  • Manual memory management should only be used when using older libraries that haven’t been updated yet.
  • Use tabs when indenting, not spaces.

Code Naming Basics

  • Follow the Apple naming guidelines
  • Use descriptive names, even if they are long. Xcode has great code completion features. Use them.
    • Clarity is preferred over all else.
    • E.g. Compare the two methods: [spaceship fly: 10.0f] and [spaceship flyAtSpeed: 10.0f].
    • Note how much clearer the second declaration is, the method describes its intentions and parameter names.
  • Don’t use abbreviations. E.g. bgColor vs. backgroundColor
  • Custom resuseable classes should be prefixed with AFG
    • Application level classes do not have to be prefixed. E.g. ViewControllers
  • All view controllers should be descriptively named and end with ViewController.
    • E.g. CalculatorSettingsViewController

Commenting

  • Follow the appledoc code formatting style.
  • Only comment the method declarations. E.g. In the @interface or private @interface ... () code blocks.
  • Describe what the method does, the parameter names and the return value and what it signifies. The following is a good example of method commenting:
/** This method asks the delegate if the AFGContainerViewController should pop the current view from the view stack.
 @param - The current AFGContainerViewController object
 @return A BOOL value indicating if the current view controller should be removed from the AFGContainerViewController's view stack.
 */
-(BOOL) AFGContainerViewControllerShouldPopContainerViewController: (AFGContainerViewController*) containerViewController;

Spacing

-(void) performOperationWithObject: (NSObject*) objectName {

  NSLog(@"This is a well spaced method");
  [objectName performSomeOperation];
}
  • No space between the class or instance specifier (+,-)
  • Single space between the return type and method name
  • Single spaces between method signature, object types and object names
  • Brace on the same line as the method signature.
  • Optional newline below the method signature for readability
  • No newline before the closing brace

Variables and Initializers

  • Variable names should always start with lower case.
    • Good: numberOfObjects
    • Bad: NumberOfObjects
  • Clearly identify the designated initializer for the class
    • All other initializers should propagate up to and use the designated initializer.
  • Override the superclass’ designated initializer.
  • Initialization method should be at the top of the @implementation
  • Don’t explicitly initialize variables to nil. Under ARC this happens automatically.

Properties

  • Don’t @synthesize properties. ARC does this automatically.
  • Always start with a lower case letter.
  • NSString properties should always be copy
  • IBOutlets should always be weak

Other readings

These are some good references / starting points for creating your own style guide or just to see other coding styles.

Github Gist Support for WordPress

Something I didn’t realise was possible until now, is WordPress natively supports Github Gists. All you have to do to embed a gist in your post is copy and paste the gist url on a new line. Everything else is handled automatically!
Code formatting is something that has been difficult for me up until now, any posts from now on will definitely be using gists.

More info here: http://en.support.wordpress.com/gist/

Example: