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
NSArray
andNSSet
variadic list, place each value on its own line
NSArray *someArray = [NSArray arrayWithObjects:
@"value1",
@"value2",
nil];
NSSet *someSet = [NSSet setWithObjects:
@"value1",
@"value2",
nil];
- For
NSDictionary
place 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
@implementation
block
@interface SomeClass()
@end
- Private methods prefixed with OE_
- (void)OE_privateMethod:(BOOL)value;
- Use YES and NO
-
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
MAX
andMIN
macros over if statements. for example:
value = MAX(someCalculations, someValue);
instead of
value = someCalculations;
if(value < someValue) value = someValue;
- There is no need to use conversion functions like
NSRectFromCGRect
orNSPointToCGPoint
to convert NS-datatypes to CG-datatypes and vice versa.
Here you can see a sample class:
#import "someheader.h"
@interface SomeClass
{
int ivar;
}
- (void)method;
@end
@interface SomeClass ()
- (void)OE_privateMethod;
@end
@implementation SomeClass
- (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
};
}
@end
For more information read Apple's Coding Guidelines for Cocoa, since the project adheres to these.