制定編碼規(guī)范的原因是我們?cè)陂_發(fā)和維護(hù)時(shí)候可以保持代碼風(fēng)格一致,在一個(gè)項(xiàng)目的生命周期中,可能會(huì)有不同的人參與到開發(fā)和維護(hù),保證了風(fēng)格一致的編碼規(guī)范無疑會(huì)規(guī)避很多的風(fēng)險(xiǎn),讓項(xiàng)目始終保持在一個(gè)合理和健康的狀態(tài)。
1.代碼組織
代碼實(shí)現(xiàn)中以 #Pragram mark - 來分類方法,遵循以下結(jié)構(gòu)、順序
#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個(gè)space,保持統(tǒng)一
3.方法和條件判斷語句的大括號(hào),總在同一行語句打開,并在新行關(guān)閉
應(yīng)該
if (user.isHappy) {
//Do something
} else {
//Do something else
}
不應(yīng)該
if (user.isHappy)
{
//Do something
}
else {
//Do something else
}
方法之間應(yīng)該有且只有一行,這樣有利于視覺上更清晰和更易于組織。方法內(nèi)的空白應(yīng)該分離功能,但通常都抽離出來一個(gè)新的方法
4.注釋
當(dāng)需要注釋時(shí)候,注釋應(yīng)該用來解釋這段特殊代碼為什么這么做
一般都避免是用注釋,因?yàn)榇a盡可能做到自注釋
5.命名
規(guī)則盡可能堅(jiān)持長(zhǎng)的,描述性的命名。
應(yīng)該:
UIButton *settingsButton;
不應(yīng)該:
UIButton *setBut;
常量命名應(yīng)該使用小寫字母k(k代表const)作為前綴,同時(shí)使用駝峰式命名規(guī)則
應(yīng)該:
static NSTimeInterval const kTutorialViewControllerNavigationFadeAnimationDuration = 0.3;
不應(yīng)該:
static NSTimeInterval const fadetime = 1.7;
類名使用駝峰式命名,首字母大寫
屬性使用駝峰式命名,首字母小寫
方法名使用駝峰式命名,首字母小寫
6.下劃線
當(dāng)使用屬性的時(shí)候應(yīng)該使用self.來訪問和改變,這就意味著屬性和實(shí)例變量視覺效果上不同,因?yàn)樗鼈兦斑叾加衧elf.
但是在初始化方法和和getter/setter中應(yīng)該使用下劃線來避免潛在的副作用
局部變量不應(yīng)該存在下劃線
7.方法
在方法簽名中,(-/+)后應(yīng)該存在一個(gè) 空格,這符合Apple的風(fēng)格,在參數(shù)之前包含一個(gè)具有描述性的關(guān)鍵字來描述參數(shù)。“and”這個(gè)詞的用法應(yīng)該保留,它不應(yīng)該用于表示具有多個(gè)參數(shù)
應(yīng)該:
- (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;
不應(yīng)該:
- (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)單個(gè)字符的變量命名,除了在for()循環(huán)中
*代表的是指針,格式應(yīng)該是NSString *text, 不應(yīng)該是 NSString * text 和 NSString* text
基本類型使用NSInteger 代替int
基本類型使用CGFloat代替float
9.屬性特性
NSString, NSArray, NSDictionary應(yīng)該使用copy而不是strong的屬性特性
delegate應(yīng)該使用weak屬性
block使用strong屬性
10.NSString, NSDictionary, NSArray 和NSNumber在初始化時(shí)候應(yīng)當(dāng)盡量使用字面值。注意nil不能傳入NSArray和NSDictionary,這樣會(huì)導(dǎo)致crash
應(yīng)該:
NSArray *names = @[@"Brian", @"Matt", @"Chris", @"Alex", @"Steve", @"Paul"];
NSDictionary *productManagers = @{@"iPhone": @"Kate", @"iPad": @"Kamal", @"Mobile Web": @"Bill"};
NSNumber *shouldUseLiterals = @YES;
NSNumber *buildingStreetNumber = @10018;
不應(yīng)該:
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;
不應(yīng)該:
#define CompanyName @“www.apple.com"
#define thumbnailHeight 2
12.枚舉類型
使用NS_ENUM() 代替 enum(),因?yàn)樗懈鼜?qiáng)的類型檢查和代碼補(bǔ)全
13.init方法使用instancetype作為返回值
應(yīng)該:
- (instancetype)init{
return [self initWithBaseURL:[NSURL URLWithString:BASE_URL]];
}
不應(yīng)該:
- (id)init{
return [self initWithBaseURL:[NSURL URLWithString:BASE_URL]];
}
14.條件判斷
if語句為了提高可讀性避免出錯(cuò),應(yīng)該總是用大括號(hào)包圍,即使只有一樣代碼
應(yīng)該:
if(!error){
return success;
}
不應(yīng)該:
if(!error)
return success;
或:
if(!error) return success;
同時(shí)條件判斷語句應(yīng)該使用黃金路徑,左手邊的代碼應(yīng)該是“golden”和“happy”的,也就是不要嵌套if語句,多個(gè)返回語句也是可以的
應(yīng)該:
- (void)someMethod{
if(!success){
return;
}
// Do something important
}
不應(yīng)該:
- (void)someMethod{
if(success){
// Do something important
}
}
待續(xù)...