Skip to content
Tony Arnold edited this page Jul 13, 2014 · 3 revisions

The general code principle we follow is:

Code will be read more often than it's written, therefore code should be easy to read, easy to follow and easy understand. Always favour clarity over complexity or neat tricks.

Rules

All code contributed to the project should follow these rules:

  • Always add a space after the method type (instance or class) in method definitions, i.e. - (void)myMethod or + (void)myMethod

  • Always remove any space after the method's return type in method definitions, i.e. - (NSString *)description, NOT - (NSString *) description

  • Always add a space after parameter types in method definitions, i.e. (MyClass *)

  • Always put curly braces on their own line

  • Always align opening and closing curly braces

  • Always use braces in your control statements (i.e. if, for, while, etc), with the following exceptions:

    • When you are returning quickly at the very start 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, etc conditional

  • Always use camelCase when naming properties, instance variables, etc

  • Always explicitly specify conditionals, i.e.

    if (someBoolValue == NO) {  }
    if (someObjectValue != nil) {  }
  • Use dot notation for accessing/setting properties and structs only

  • Use modern Objective-C syntax:

    • Don't include @synthesize statements
    • Use subscripting to access and set array and dictionary values
    • Use @(23) to create NSNumber instances from scalar types

Example

@implementation MRHelper

- (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];
         self.someUsefulIndex = @(index);
         *stop = (index > ([results count] / 2));

     }];

     return context;
}

@end