代码的整体可读性:功能菜单
- (void)dealloc { /* ... */ }
- (instancetype)init { /* ... */ }
#pragma mark - View Lifecycle
- (void)viewDidLoad { /* ... */ }
- (void)viewWillAppear:(BOOL)animated { /* ... */ }
- (void)didReceiveMemoryWarning { /* ... */ }
#pragma mark - Custom Accessors
- (void)setCustomProperty:(id)value { /* ... */ }
- (id)customProperty { /* ... */ }
#pragma mark - IBActions
- (IBAction)submitData:(id)sender { /* ... */ }
#pragma mark - Public
- (void)publicMethod { /* ... */ }
#pragma mark - Private
- (void)zoc_privateMethod { /* ... */ }
#pragma mark - UITableViewDataSource
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { /* ... */ }
#pragma mark - ZOCSuperclass
// ... overridden methods from ZOCSuperclass
#pragma mark - NSObject
- (NSString *)description { /* ... */ }
条件语句
有条件的机构应始终使用,即使有条件的机构可以不带括号(例如,它只是一条线),以防止错误写括号。这些错误包括增加第二条生产线,并期待它成为if语句的一部分。另一个更危险的缺陷,可能会出现在那里行“里面的”if语句已被注释掉,并在下一行不知不觉成为if语句的一部分。
should be
if (!error) {
return success;
}
Not
if (!error)
return success;
// or
if (!error) return success;
should be
- (void)someMethod {
if (![someOther boolValue]) {
return;
}
//Do something important
}
Not
- (void)someMethod {
if ([someOther boolValue]) {
//Do something important
}
}
Yoda conditions
should be
if ([myValue isEqual:@42]) { ...
Not
if ([@42 isEqual:myValue]) { ...
nil and BOOL judge
should be
if (someObject) { ...
if (![someObject boolValue]) { ...
if (!someObject) { ...
Not
if (someObject == YES) { ... // Wrong
if (myRawValue == YES) { ... // Never do this.
if ([someObject boolValue] == NO) { ...
Case Statements
switch (menuType) { // 枚举
case CC_EnumNone:
// ...
break;
case CC_EnumValue1:
// ...
break;
case CC_EnumValue2:
// ...
break;
}
Enumerated Types
typedef NS_ENUM(NSUInteger, CC_MachineState) {
CC_MachineStateNone,
CC_MachineStateIdle,
CC_MachineStateRunning,
CC_MachineStatePaused
};
命名
- Methods
should be
- (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;
Not
- (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.
- 代码解耦及加强可读性
示例应用场景:假设有个Person类,需要喝水,根据职责划分,我们需要另外一个类Cup来完成喝水的动作,代码如下:
//Person.h
@interface Person:NSObject
- (void)drink;
@end
// Cup.h
@interface Cup:NSObject
- (id)provideWater;
@end
很明显Person和Cup间要完成喝水动作,一定会产生耦合,我们来看看OC中不同耦合方式下调用,代码的质量比较
方式一:.m引用
在.m文件导入Cup.h,同时生成临时的Cup对象来调用Cup中的方法。
#import "Person.h"
#import "Cup.h"
@implementation Person
- (void)drink {
Cup *c = [Cup new];
id water = [c provideWater];
[self sip:water];
}
- (void)sip:(id)water
{
// sip water
}
@end
方式二: .h Property
避免方式一直接在.m中应用导致耦合不清晰,可以将耦合的部分声明放在.h中,提高代码可读性
// Person.h
@interface Person:NSObject
@property (nonatomic, strong) Cup *cup;
- (void)drink;
@end
// Person.m
@implementation Person
- (void)drink {
self.cup = [Cup new];
id water = [self.cup provideWater];
[self sip:water];
}
- (void)sip:(id)water
{
// sip water
}
@end