制定編碼規(guī)范的原因是我們在開發(fā)和維護時候可以保持代碼風格一致盈魁,在一個項目的生命周期中,可能會有不同的人參與到開發(fā)和維護窃诉,保證了風格一致的編碼規(guī)范無疑會規(guī)避很多的風險杨耙,讓項目始終保持在一個合理和健康的狀態(tài)。
1.代碼組織
代碼實現(xiàn)中以 #Pragram mark - 來分類方法飘痛,遵循以下結構珊膜、順序
#pragram mark - Life Cycle
#pragram mark - Private Methods
#pragram mark - Public Methods
#pragram mark - Event Response
#pragram mark - UITableView Datasource
#pragram mark - UITableView Delegate
#pragram mark - Custom Accessors
2.空格
tab或4個space,保持統(tǒng)一
3.方法和條件判斷語句的大括號敦冬,總在同一行語句打開辅搬,并在新行關閉
應該
if (user.isHappy) {
//Do something
} else {
//Do something else
}
不應該
if (user.isHappy)
{
//Do something
}
else {
//Do something else
}
方法之間應該有且只有一行,這樣有利于視覺上更清晰和更易于組織。方法內(nèi)的空白應該分離功能堪遂,但通常都抽離出來一個新的方法
4.注釋
當需要注釋時候介蛉,注釋應該用來解釋這段特殊代碼為什么這么做
一般都避免是用注釋,因為代碼盡可能做到自注釋
5.命名
規(guī)則盡可能堅持長的溶褪,描述性的命名币旧。
應該:
UIButton *settingsButton;
不應該:
UIButton *setBut;
常量命名應該使用小寫字母k(k代表const)作為前綴,同時使用駝峰式命名規(guī)則
應該:
static NSTimeInterval const kTutorialViewControllerNavigationFadeAnimationDuration = 0.3;
不應該:
static NSTimeInterval const fadetime = 1.7;
類名使用駝峰式命名猿妈,首字母大寫
屬性使用駝峰式命名吹菱,首字母小寫
方法名使用駝峰式命名,首字母小寫
6.下劃線
當使用屬性的時候應該使用self.來訪問和改變彭则,這就意味著屬性和實例變量視覺效果上不同鳍刷,因為它們前邊都有self.
但是在初始化方法和和getter/setter中應該使用下劃線來避免潛在的副作用
局部變量不應該存在下劃線
7.方法
在方法簽名中,(-/+)后應該存在一個 空格俯抖,這符合Apple的風格输瓜,在參數(shù)之前包含一個具有描述性的關鍵字來描述參數(shù)》移迹“and”這個詞的用法應該保留尤揣,它不應該用于表示具有多個參數(shù)
應該:
- (void)setExampleText:(NSString *)text image:(UIImage *)image;
- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;
- (id)viewWithTag:(NSInteger)tag;
- (instancetype)initWithWidth:(CGFloat)width height:(CGFloat)height;
不應該:
- (void)setT:(NSString *)text i:(UIImage *)image;
- (void)sendAction:(SEL)aSelector :(id)anObject :(BOOL)flag;
- (id)taggedView:(NSInteger)tag;
- (instancetype)initWithWidth:(CGFloat)width andHeight:(CGFloat)height;
- (instancetype)initWith:(int)width and:(int)height; // Never do this.
8.變量
盡量以描述性的方式來命名,避免出現(xiàn)單個字符的變量命名,除了在for()循環(huán)中
*代表的是指針柬祠,格式應該是NSString *text北戏, 不應該是 NSString * text 和 NSString* text
基本類型使用NSInteger 代替int
基本類型使用CGFloat代替float
9.屬性特性
NSString, NSArray, NSDictionary應該使用copy而不是strong的屬性特性
delegate應該使用weak屬性
block使用strong屬性
10.NSString, NSDictionary, NSArray 和NSNumber在初始化時候應當盡量使用字面值。注意nil不能傳入NSArray和NSDictionary漫蛔,這樣會導致crash
應該:
NSArray *names = @[@"Brian", @"Matt", @"Chris", @"Alex", @"Steve", @"Paul"];
NSDictionary *productManagers = @{@"iPhone": @"Kate", @"iPad": @"Kamal", @"Mobile Web": @"Bill"};
NSNumber *shouldUseLiterals = @YES;
NSNumber *buildingStreetNumber = @10018;
不應該:
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 *buildingStreetNumber = [NSNumber numberWithInteger:10018];
11.常量
使用常量可以通過查找和替代來快速修改值嗜愈,常量盡可能使用static來代替#define
static NSString * const kAboutViewControllerCompanyName = @“www.apple.com";
static CGFloat const kImageThumbnailHeight = 50.0;
不應該:
#define CompanyName @“www.apple.com"
#define thumbnailHeight 2
12.枚舉類型
使用NS_ENUM() 代替 enum(),因為它有更強的類型檢查和代碼補全
13.init方法使用instancetype作為返回值
應該:
- (instancetype)init{
return [self initWithBaseURL:[NSURL URLWithString:BASE_URL]];
}
不應該:
- (id)init{
return [self initWithBaseURL:[NSURL URLWithString:BASE_URL]];
}
14.條件判斷
if語句為了提高可讀性避免出錯,應該總是用大括號包圍莽龟,即使只有一樣代碼
應該:
if(!error){
return success;
}
不應該:
if(!error)
return success;
或:
if(!error) return success;
同時條件判斷語句應該使用黃金路徑芝硬,左手邊的代碼應該是“golden”和“happy”的,也就是不要嵌套if語句轧房,多個返回語句也是可以的
應該:
- (void)someMethod{
if(!success){
return;
}
// Do something important
}
不應該:
- (void)someMethod{
if(success){
// Do something important
}
}
待續(xù)...