forked from OpenEmu/OpenEmu
-
Notifications
You must be signed in to change notification settings - Fork 0
Style Guide
riquedafreak edited this page Mar 3, 2012
·
20 revisions
Herein lies some conventions used throughout the OpenEmu codebase.
This list is by no means exhaustive and quite prototypal as of yet.
- Braces on their own line
- (void)method
{
}- Pointer declaration
NSString *myString;- Block argument list and types on their own line
int (^blk1)(int) =
^ int (int v)
{
};
[someDictionary enumerateKeysAndObjectsUsingBlock:
^ (id key, id obj, BOOL *stop)
{
}];- Caret and curly brace on previous line when there is no block parameters
dispatch_async(^{
// My code
});- Space between caret and block's return parameter list (when no return data-type is specified)
^ (id key, id obj, BOOL *stop)- Space between caret and block's return data-type and space between block's return data-type and parameter list
^ int (id key, id obj, BOOL *stop)- Use 4 spaces instead of tabs
@interface SomeClass
{
int ivar;
}- Class ivars and properties should be aligned
@interface SomeClass
{
int someValue;
BOOL shouldCloseFile;
NSString *description;
}
@property(strong) IBOutlet OELibraryController *libraryController;
@property(nonatomic, strong) NSViewController *currentContentController;
@property(nonatomic, strong) NSViewController *defaultContentController;
@property BOOL allowWindowResizing;
@property(copy) NSArray *deviceHandlers;
@property(copy) NSArray *coreList;
@end- For
NSArrayandNSSetvariadic list, place each value on its own line
NSArray *someArray = [NSArray arrayWithObjects:
@"value1",
@"value2",
nil];
NSSet *someSet = [NSSet setWithObjects:
@"value1",
@"value2",
nil];- For
NSDictionaryplace each value and key pair on the same line
NSDictionary *someArray = [NSDictionary dictionaryWithObjectsAndKeys:
@"value1", @"key1",
@"value2", @"key2",
nil];- Class names are CamelCased
@implementation SomeClass- Private categories described by empty category, implementation methods placed within main
@implementationblock
@interface SomeClass()
@end- Private methods prefixed with OE_
- (void)OE_privateMethod:(BOOL)value;- Use
YESandNO
-
Implementation classes should follow this general ordering guideline:
@synthesize+initialize-init-dealloc- Class Methods (
+someMethod) - Instance Methods (
-someMethod) - Delegate Implementation
- Setters / Getters
-
Order methods by tasks
-
Private methods should be close to the public methods that call them
- avoid 'dot-syntax'
- Perfer
MAXandMINmacros overifstatements, for example:
value = MAX(someCalculations, someValue);instead of
value = someCalculations;
if(value < someValue) value = someValue;- There is no need to use conversion functions like
NSRectFromCGRectorNSPointToCGPointto convert NS-datatypes to CG-datatypes and vice versa.
Here you can see a sample class:
SomeClass.h
#import "someheader.h"
@interface SomeClass
{
@private
int someValue;
BOOL shouldCloseFile;
@protected
NSString *description;
}
+ (id)sharedSomeClass;
- (void)method;
@property(strong) IBOutlet OELibraryController *libraryController;
@property(nonatomic, strong) NSViewController *currentContentController;
@property(nonatomic, strong) NSViewController *defaultContentController;
@property BOOL allowWindowResizing;
@property(copy) NSArray *deviceHandlers;
@property(copy) NSArray *coreList;
@endSomeClass.m
#import "SomeClass.h"
@interface SomeClass ()
- (void)OE_privateMethod;
@end
@implementation SomeClass
@synthesize libraryController;
@synthesize currentContentController, defaultContentController;
@synthesize allowWindowResizing;
+ (void)initialize
{
if(self == [SomeClass class])
{
// do things
}
}
- (id)init
{
if((self = [super init]))
{
// do things
}
return self;
}
- (void)dealloc
{
// do things
}
+ (id)sharedSomeClass
{
static SomeClass *sharedSomeClass = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedSomeClass = [[SomeClass alloc] init];
});
return sharedSomeClass;
}
- (void)OE_privateMethod
{
// do things
}
- (void)method
{
if(booleanValue && obj != nil)
{
do
{
[self OE_privateMethod];
} while(value > 0);
}
while(value < 10)
{
}
void (^blk)(void) =
^{
// do things
};
int (^blk1)(int) =
^ int (int v)
{
// do things
};
}
- (void)delegateMethod
{
// do things
}
- (void)setDeviceHandlers:(NSArray *)value
{
// do things
}
- (NSArray *)deviceHandlers
{
// do things
}
- (void)setCoreList:(NSArray *)value
{
// do things
}
- (NSArray *)coreList
{
// do things
}
@endFor more information read Apple's Coding Guidelines for Cocoa, since the project adheres to these.