Radya Labs Objective-C Coding Style
Here are some of the documents from Apple that informed the style guide:
- The Objective-C Programming Language
- Cocoa Fundamentals Guide
- Coding Guidelines for Cocoa
- iOS App Programming Guide
Dot notation should be used for accessing properties only. Bracket notation is used for mutating properties and other instances.
Dot notation
CGFloat heightOfView = self.view.frame.size.height;
Person *person = self.company.person;
Bracket notation
[self.view setBackgroundColor:[UIColor redColor]];
[self.person setFirstName:@"Johnny"];
Variables names must be named as clear and descriptively as possible.
Single letter variables name should be avoided except in for()
loops.
Asterisks indicating pointers belong with the variable, e.g., NSString *text
not NSString* text
or NSString * text
, except in the case of constants.
Property definitions should be used in place of naked instance variables whenever possible. Direct instance variable access should be avoided except in initializer methods (init
, initWithCoder:
, etc…), dealloc
methods and within custom setters and getters. For more information on using Accessor Methods in Initializer Methods and dealloc, see here.
For example:
@interface Person : NSObject
@property (strong, nonatomic) NSString *firstName;
@property (strong, nonatomic) NSString *lastName;
@end
Not
@interface Person : NSObject {
NSString *firstName;
NSString *lastName;
}
Always use CGFloat
instead of float
or double
and NSInteger
instead of int
Constants should be declared as static
constants and not #define
unless explicitly being used as a macro
Always use 'k' before the constants name.
For example:
static const NSString *kCompanyName = @"Radya Labs";
static const CGFloat kHeaderHeight = 44.0f;
Not:
#define COMPANY_NAME @"Radya Labs"
#define HEADER_HEIGHT 44
Put open brace {
in line with method invocations.
Give new line before close brace }
For example:
- (void)fetchDataFromURL:(NSURL *)url {
...
}
Not:
- (void)fetchDataFromURL:(NSURL *)url
{
...
}
Since nil
resolves to NO
it is unnecessary to compare it in conditions. Never compare something directly to YES
, because YES
is defined to 1 and a BOOL
can be up to 8 bits.
This allows for more consistency across files and greater visual clarity.
For example:
if (!someObject) {
}
Not:
if (someObject == nil) {
}
For a BOOL
, here are two examples:
if (isAwesome)
if (![someObject boolValue])
Not:
if ([someObject boolValue] == NO)
if (isAwesome == YES) // Never do this.
If the name of a BOOL
property is expressed as an adjective, the property can omit the “is” prefix but specifies the conventional name for the get accessor, for example:
@property (assign, getter=isEditable) BOOL editable;
#pragma mark
directives show up in XCode in the menus for direct access to methods. They have no impact on the program at all.
#pragma mark
should be used to organize the code.
For example:
@implementation ViewController
#pragma mark - Designated initializer
- (id)init {
...
}
#pragma mark - UIViewController
- (void)viewDidLoad {
...
}
- (void)viewDidAppear:(BOOL)animated {
...
}
#pragma mark - IBAction
- (IBAction)cancel:(id)sender {
...
}
#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
...
}
#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
...
}
Use prefix UserDef.
for NSUserDefaults
key.
Use prefix Notif.
for NSNotificationCenter
name.
For example:
[[NSUserDefaults standardUserDefaults] setObject:@"RadyaLabs" forKey:@"UserDef.COMPANY_NAME"];
[[NSNotificationCenter defaultCenter] postNotificationName:@"Notif.STOP_PLAYING_MOVIE" object:nil];