编码规范

制定编码规范的原因是我们在开发和维护时候可以保持代码风格一致,在一个项目的生命周期中,可能会有不同的人参与到开发和维护,保证了风格一致的编码规范无疑会规避很多的风险,让项目始终保持在一个合理和健康的状态。

1.代码组织
代码实现中以 #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,保持统一

3.方法和条件判断语句的大括号,总在同一行语句打开,并在新行关闭
应该

if (user.isHappy) {  
    //Do something  
} else {  
    //Do something else  
}  

不应该

if (user.isHappy)  
{  
    //Do something  
}  
else {  
    //Do something else  
}  

方法之间应该有且只有一行,这样有利于视觉上更清晰和更易于组织。方法内的空白应该分离功能,但通常都抽离出来一个新的方法

4.注释
当需要注释时候,注释应该用来解释这段特殊代码为什么这么做
一般都避免是用注释,因为代码尽可能做到自注释

5.命名
规则尽可能坚持长的,描述性的命名。
应该:

UIButton *settingsButton;  

不应该:

UIButton *setBut;  

常量命名应该使用小写字母k(k代表const)作为前缀,同时使用驼峰式命名规则
应该:

static NSTimeInterval const kTutorialViewControllerNavigationFadeAnimationDuration = 0.3;  

不应该:

static NSTimeInterval const fadetime = 1.7;  

类名使用驼峰式命名,首字母大写
属性使用驼峰式命名,首字母小写
方法名使用驼峰式命名,首字母小写

6.下划线
当使用属性的时候应该使用self.来访问和改变,这就意味着属性和实例变量视觉效果上不同,因为它们前边都有self.
但是在初始化方法和和getter/setter中应该使用下划线来避免潜在的副作用
局部变量不应该存在下划线

7.方法
在方法签名中,(-/+)后应该存在一个 空格,这符合Apple的风格,在参数之前包含一个具有描述性的关键字来描述参数。“and”这个词的用法应该保留,它不应该用于表示具有多个参数

应该:

- (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.变量
尽量以描述性的方式来命名,避免出现单个字符的变量命名,除了在for()循环中
*代表的是指针,格式应该是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
     }
}

待续...

你可能感兴趣的:(编码规范)