Antidote/local_pod_repo/objcTox/objective-c-style-guide.md

456 lines
15 KiB
Markdown
Raw Normal View History

2024-02-22 20:43:11 +01:00
Forked from [NYTimes](https://github.com/NYTimes/objective-c-style-guide).
## Introduction
Here are some of the documents from Apple that informed the style guide. If something isnt mentioned here, its probably covered in great detail in one of these:
* [The Objective-C Programming Language](http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ObjectiveC/Introduction/introObjectiveC.html)
* [Cocoa Fundamentals Guide](https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CocoaFundamentals/Introduction/Introduction.html)
* [Coding Guidelines for Cocoa](https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CodingGuidelines/CodingGuidelines.html)
* [iOS App Programming Guide](http://developer.apple.com/library/ios/#documentation/iphone/conceptual/iphoneosprogrammingguide/Introduction/Introduction.html)
## Table of Contents
* [Dot Notation Syntax](#dot-notation-syntax)
* [Spacing](#spacing)
* [Conditionals](#conditionals)
* [Ternary Operator](#ternary-operator)
* [Error handling](#error-handling)
* [Methods](#methods)
* [Variables](#variables)
* [Naming](#naming)
* [Comments](#comments)
* [Init & Dealloc](#init-and-dealloc)
* [Literals](#literals)
* [CGRect Functions](#cgrect-functions)
* [Constants](#constants)
* [Enumerated Types](#enumerated-types)
* [Bitmasks](#bitmasks)
* [Private Properties](#private-properties)
* [Booleans](#booleans)
* [Singletons](#singletons)
* [Imports](#imports)
* [Xcode Project](#xcode-project)
## Dot Notation Syntax
Dot notation should **always** be used for accessing and mutating properties. Bracket notation is preferred in all other instances.
**For example:**
```objc
view.backgroundColor = [UIColor orangeColor];
[UIApplication sharedApplication].delegate;
```
**Not:**
```objc
[view setBackgroundColor:[UIColor orangeColor]];
UIApplication.sharedApplication.delegate;
```
## Spacing
* Indent using 4 spaces. Never indent with tabs. Be sure to set this preference in Xcode.
* Method braces and other braces (`if`/`else`/`switch`/`while` etc.) always open on the same line as the statement but close on a new line.
**For example:**
```objc
if (user.isHappy) {
// Do something
}
else {
// Do something else
}
```
* There should be exactly one blank line between methods to aid in visual clarity and organization.
* Whitespace within methods should be used to separate functionality (though often this can indicate an opportunity to split the method into several, smaller methods). In methods with long or verbose names, a single line of whitespace may be used to provide visual separation before the methods body.
* `@synthesize` and `@dynamic` should each be declared on new lines in the implementation.
## Conditionals
Conditional bodies should always use braces even when a conditional body could be written without braces (e.g., it is one line only) to prevent errors. These errors include adding a second line and expecting it to be part of the if-statement. Another, [even more dangerous defect](http://programmers.stackexchange.com/a/16530) may happen where the line “inside” the if-statement is commented out, and the next line unwittingly becomes part of the if-statement. In addition, this style is more consistent with all other conditionals, and therefore more easily scannable.
**For example:**
```objc
if (!error) {
return success;
}
```
**Not:**
```objc
if (!error)
return success;
```
or
```objc
if (!error) return success;
```
### Ternary Operator
The ternary operator, `?` , should only be used when it increases clarity or code neatness. A single condition is usually all that should be evaluated. Evaluating multiple conditions is usually more understandable as an if statement, or refactored into named variables.
**For example:**
```objc
result = a > b ? x : y;
```
**Not:**
```objc
result = a > b ? x = c > d ? c : d : y;
```
## Error Handling
When methods return an error parameter by reference, switch on the returned value, not the error variable.
**For example:**
```objc
NSError *error;
if (![self trySomethingWithError:&error]) {
// Handle Error
}
```
**Not:**
```objc
NSError *error;
[self trySomethingWithError:&error];
if (error) {
// Handle Error
}
```
Some of Apples APIs write garbage values to the error parameter (if non-NULL) in successful cases, so switching on the error can cause false negatives (and subsequently crash).
## Methods
In method signatures, there should be a space after the scope (`-` or `+` symbol). There should be a space between the method segments.
**For example:**
```objc
- (void)setExampleText:(NSString *)text image:(UIImage *)image;
```
## Variables
Variables should be named descriptively, with the variables name clearly communicating what the variable _is_ and pertinent information a programmer needs to use that value properly.
**For example:**
* `NSString *title`: It is reasonable to assume a “title” is a string.
* `NSString *titleHTML`: This indicates a title that may contain HTML which needs parsing for display. _“HTML” is needed for a programmer to use this variable effectively._
* `NSAttributedString *titleAttributedString`: A title, already formatted for display. _`AttributedString` hints that this value is not just a vanilla title, and adding it could be a reasonable choice depending on context._
* `NSDate *now`: _No further clarification is needed._
* `NSDate *lastModifiedDate`: Simply `lastModified` can be ambiguous; depending on context, one could reasonably assume it is one of a few different types.
* `NSURL *URL` vs. `NSString *URLString`: In situations when a value can reasonably be represented by different classes, it is often useful to disambiguate in the variables name.
* `NSString *releaseDateString`: Another example where a value could be represented by another class, and the name can help disambiguate.
Single letter variable names should be avoided except as simple counter variables in loops.
Asterisks indicating a type is a pointer should be “attached to” the variable name. **For example,** `NSString *text` **not** `NSString* text` or `NSString * text`.
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, see [Apples docs on using accessor methods in initializer methods and `dealloc`](https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmPractical.html#//apple_ref/doc/uid/TP40004447-SW6).
**For example:**
```objc
@interface OCTSection: NSObject
@property (nonatomic) NSString *headline;
@end
```
**Not:**
```objc
@interface OCTSection : NSObject {
NSString *headline;
}
```
## Naming
Apple naming conventions should be adhered to wherever possible, especially those related to [memory management rules](https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/MemoryMgmt/Articles/MemoryMgmt.html) ([NARC](http://stackoverflow.com/a/2865194/340508)).
Long, descriptive method and variable names are good.
**For example:**
```objc
UIButton *settingsButton;
```
**Not**
```objc
UIButton *setBut;
```
A three letter prefix (e.g., `OCT`) should always be used for class names and constants. Constants should be camel-case with all words capitalized and prefixed by the related class name for clarity. A two letter prefix (e.g., `NS`) is [reserved for use by Apple](https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/DefiningClasses/DefiningClasses.html#//apple_ref/doc/uid/TP40011210-CH3-SW12).
**For example:**
```objc
static const NSTimeInterval OCTArticleViewControllerNavigationFadeAnimationDuration = 0.3;
```
**Not:**
```objc
static const NSTimeInterval fadetime = 1.7;
```
Properties and local variables should be camel-case with the leading word being lowercase.
Instance variables should be camel-case with the leading word being lowercase, and should be prefixed with an underscore. This is consistent with instance variables synthesized automatically by LLVM. **If LLVM can synthesize the variable automatically, then let it.**
**For example:**
```objc
@synthesize descriptiveVariableName = _descriptiveVariableName;
```
**Not:**
```objc
id varnm;
```
## Comments
When they are needed, comments should be used to explain **why** a particular piece of code does something. Any comments that are used must be kept up-to-date or deleted.
Block comments should generally be avoided, as code should be as self-documenting as possible, with only the need for intermittent, few-line explanations. This does not apply to those comments used to generate documentation.
## init and dealloc
`dealloc` methods should be placed directly after the `init` method.
`init` methods should be structured like this:
```objc
- (instancetype)init
{
self = [super init]; // or call the designated initializer
if (! self) {
return nil;
}
// Custom initialization
return self;
}
```
## Literals
`NSString`, `NSDictionary`, `NSArray`, and `NSNumber` literals should be used whenever creating immutable instances of those objects. Pay special care that `nil` values not be passed into `NSArray` and `NSDictionary` literals, as this will cause a crash.
**For example:**
```objc
NSArray *names = @[@"Brian", @"Matt", @"Chris", @"Alex", @"Steve", @"Paul"];
NSDictionary *productManagers = @{@"iPhone" : @"Kate", @"iPad" : @"Kamal", @"Mobile Web" : @"Bill"};
NSNumber *shouldUseLiterals = @YES;
NSNumber *buildingZIPCode = @10018;
```
**Not:**
```objc
NSArray *names = [NSArray arrayWithObjects:@"Brian", @"Matt", @"Chris", @"Alex", @"Steve", @"Paul", nil];
NSDictionary *productManagers = [NSDictionary dictionaryWithObjectsAndKeys: @"Kate", @"iPhone", @"Kamal", @"iPad", @"Bill", @"Mobile Web", nil];
NSNumber *shouldUseLiterals = [NSNumber numberWithBool:YES];
NSNumber *buildingZIPCode = [NSNumber numberWithInteger:10018];
```
## `CGRect` Functions
When accessing the `x`, `y`, `width`, or `height` of a `CGRect`, always use the [`CGGeometry` functions](http://developer.apple.com/library/ios/#documentation/graphicsimaging/reference/CGGeometry/Reference/reference.html) instead of direct struct member access. From Apple's `CGGeometry` reference:
> All functions described in this reference that take CGRect data structures as inputs implicitly standardize those rectangles before calculating their results. For this reason, your applications should avoid directly reading and writing the data stored in the CGRect data structure. Instead, use the functions described here to manipulate rectangles and to retrieve their characteristics.
**For example:**
```objc
CGRect frame = self.view.frame;
CGFloat x = CGRectGetMinX(frame);
CGFloat y = CGRectGetMinY(frame);
CGFloat width = CGRectGetWidth(frame);
CGFloat height = CGRectGetHeight(frame);
```
**Not:**
```objc
CGRect frame = self.view.frame;
CGFloat x = frame.origin.x;
CGFloat y = frame.origin.y;
CGFloat width = frame.size.width;
CGFloat height = frame.size.height;
```
## Constants
Constants are preferred over in-line string literals or numbers, as they allow for easy reproduction of commonly used variables and can be quickly changed without the need for find and replace. Constants should be declared as `static` constants and not `#define`s unless explicitly being used as a macro.
**For example:**
```objc
static NSString *const OCTAboutViewControllerName = @"Name";
static const CGFloat COTImageThumbnailHeight = 50.0;
```
**Not:**
```objc
#define CompanyName @"Name"
#define thumbnailHeight 2
```
## Enumerated Types
When using `enum`s, use the new fixed underlying type specification, which provides stronger type checking and code completion. The SDK includes a macro to facilitate and encourage use of fixed underlying types: `NS_ENUM()`.
**Example:**
```objc
typedef NS_ENUM(NSInteger, OCTAdRequestState) {
OCTAdRequestStateInactive,
OCTAdRequestStateLoading
};
```
## Bitmasks
When working with bitmasks, use the `NS_OPTIONS` macro.
**Example:**
```objc
typedef NS_OPTIONS(NSUInteger, OCTAdCategory) {
OCTAdCategoryAutos = 1 << 0,
OCTAdCategoryJobs = 1 << 1,
OCTAdCategoryRealState = 1 << 2,
OCTAdCategoryTechnology = 1 << 3
};
```
## Private Properties
Private properties should be declared in class extensions (anonymous categories) in the implementation file of a class.
**For example:**
```objc
@interface OCTAdvertisement ()
@property (nonatomic, strong) GADBannerView *googleAdView;
@property (nonatomic, strong) ADBannerView *iAdView;
@property (nonatomic, strong) UIWebView *adXWebView;
@end
```
## Booleans
Never compare something directly to `YES`, because `YES` is defined as `1`, and a `BOOL` in Objective-C is a `CHAR` type that is 8 bits long (so a value of `11111110` will return `NO` if compared to `YES`).
**For an object pointer:**
```objc
if (! someObject) {
}
if (someObject) {
}
```
**For a `BOOL` value:**
```objc
if (isAwesome)
if (someNumber.boolValue)
if (! someNumber.boolValue)
```
**Not:**
```objc
if (isAwesome == YES) // Never do this.
```
If the name of a `BOOL` property is expressed as an adjective, the propertys name can omit the `is` prefix and specify the conventional name for the getter.
**For example:**
```objc
@property (assign, getter=isEditable) BOOL editable;
```
_Text and example taken from the [Cocoa Naming Guidelines](https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CodingGuidelines/Articles/NamingIvarsAndTypes.html#//apple_ref/doc/uid/20001284-BAJGIIJE)._
## Singletons
Generally you don't want to use singletons. Don't use them.
Singleton objects should use a thread-safe pattern for creating their shared instance.
```objc
+ (instancetype)sharedInstance {
static id sharedInstance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedInstance = [[[self class] alloc] init];
});
return sharedInstance;
}
```
This will prevent [possible and sometimes frequent crashes](http://cocoasamurai.blogspot.com/2011/04/singletons-your-doing-them-wrong.html).
## Imports
If there is more than one import statement, group the statements [together](http://ashfurrow.com/blog/structuring-modern-objective-c). Commenting each group is optional.
Note: For modules use the [@import](http://clang.llvm.org/docs/Modules.html#using-modules) syntax.
```objc
// Frameworks
@import QuartzCore;
// Models
#import "NYTUser.h"
// Views
#import "NYTButton.h"
#import "NYTUserView.h"
```
## Xcode project
The physical files should be kept in sync with the Xcode project files in order to avoid file sprawl. Any Xcode groups created should be reflected by folders in the filesystem. Code should be grouped not only by type, but also by feature for greater clarity.
# Other Objective-C Style Guides
If ours doesnt fit your tastes, have a look at some other style guides:
* [NYTimes](https://github.com/NYTimes/objective-c-style-guide)
* [Google](http://google-styleguide.googlecode.com/svn/trunk/objcguide.xml)
* [GitHub](https://github.com/github/objective-c-conventions)
* [Adium](https://trac.adium.im/wiki/CodingStyle)
* [Sam Soffes](https://gist.github.com/soffes/812796)
* [CocoaDevCentral](http://cocoadevcentral.com/articles/000082.php)
* [Luke Redpath](http://lukeredpath.co.uk/blog/2011/06/28/my-objective-c-style-guide/)
* [Marcus Zarra](http://www.cimgf.com/zds-code-style-guide/)
* [Wikimedia](https://www.mediawiki.org/wiki/Wikimedia_Apps/Team/iOS/ObjectiveCStyleGuide)