Skip to content

Code Format Guidelines

casademora edited this page Nov 1, 2011 · 3 revisions

Please format all code submissions using the example provided here

@implementation MRHelper

@synthesize myGreatProperty = _myGreatProperty;
@synthesize moreUsefulInfo = _moreUsefulInfo;

- (void) dealloc
{
     self.myGreatProperty = nil;
     self.moreUsefulInfo = nil;
     [super dealloc];
}

- (id) findByName:(NSString *)name inContext:(NSManagedObjectContext *)context
{
     if (name == nil) return;

     NSArray *results = [self findAllByAttribute:@"name" withValue:name inContext:context];
     [results enumerateObjectsUsingBlock:^(id obj, NSUInteger index, BOOL *stop){

         self.moreUsefulInfo = [obj calculateSomethingBasedOn:index];
         *stop = (index > ([results count] / 2)); 

     }];

     return context;
}

@end

  • Method definition should have a space:
  • after the method type (+ or -)
  • after the return parameter
  • All curly braces must go on their own line AND line up with it's closing scope
  • This goes for for, while, if, else, methods.
  • All if, for, while, etc statements MUST have curly braces
  • Exceptions:
  • Quick return as the first line of a method
  • Blocks inlined with a method (the closing curly and square brace MUST align with the opening square brace
  • Always be explicit about what is being tested in an if, for, while conditional
  • Explicitly specify conditionals
  • use dot notation where appropriate
  • NEVER use dot notation if a method is NOT defined as a property
  • Use autoreleased objects in local scope before manually using retain/release/autorelease explicitly.
  • In dealloc, use self.property = nil; rather than the long form ([_property release], _property = nil;), to clear your ivars
  • (This will be controversial. Doing this will make you find bugs faster, rather than silently ignoring them)
  • When @synthesize-ing properties, all properties go on separate lines.
  • always prefix synthesized ivars with a _.
  • Use camelCase for properties, ivars, etc

The general code principle to follow: Code will be read more often than it's written, therefore code should be clear, easy to read, follow and understand. Clarity over complexity and neat tricks.

Clone this wiki locally