经常听同事说,谁谁的代码好牛逼啊,我问为什么?他说看不懂。看不懂的代码就牛逼吗?我的看法正好相反,好的代码应该是通俗易懂的,可读性非常强的。不是一个函数几百行甚至上千行。 什么是可读性好呢?下面例子你也许有所感悟。
1.原始代码,您不用理解什么意思,因为这一段很长:
- (void)requestWithNavRankCatalogConfig{
[MBProgressHUD showHUDAddedTo:self.view animated:YES];
CMWeakSelf;
[CMCategoryAPI getClientNavRankCatalogConfigRequestWithBlock:^(id model, NSError * _Nonnull error) {
[MBProgressHUD hideHUDForView:self.view animated:YES];
NSMutableArray * titleArray = [NSMutableArray new];
CMStrongSelf;
if (!error) {
if (model && [model isKindOfClass:[NSArray class]]) {
NSArray * modelArray = (NSArray *)model;
if (modelArray.count>0) {
[self.topModelArray setArray:modelArray];
for (CMClassifyTopModel * topModel in modelArray) {
if (topModel.topTabName) {
[titleArray addObject:topModel.topTabName];
}
}
}
}
[self saveTopModelArray];
}else{
NSArray * topModelArray = [self getTopModelArray];
if (topModelArray) {
[self.topModelArray setArray:topModelArray];
for (CMClassifyTopModel * topModel in topModelArray) {
if (topModel.topTabName) {
[titleArray addObject:topModel.topTabName];
}
}
}else{
[titleArray addObjectsFromArray:@[@"男生",@"女生",@"出版",@"听书",@"漫画",@"杂志"]];
}
}
[self setupHederViewWithTitleArray:titleArray];
[self setupContentView];
NSInteger headerSelectIndex = self.headerTitleScrollView.selectedIndex;
if (self.pointHeaderIndex>0) {
headerSelectIndex = self.pointHeaderIndex;
}else{
headerSelectIndex = [self getPointHeaderIndexFromUserPreferences];
}
[self.headerTitleScrollView setSelectedIndex:headerSelectIndex animation:CMScrollSectionSelectNoneAnimation];
[self refreshContentViewWithHeaderIndex:headerSelectIndex];
}];
}
2.重构一次之后的代码:
- (void)requestWithNavRankCatalogConfig{
[MBProgressHUD showHUDAddedTo:self.view animated:YES];
CMWeakSelf;
[CMCategoryAPI getClientNavRankCatalogConfigRequestWithBlock:^(id model, NSError * _Nonnull error) {
[MBProgressHUD hideHUDForView:self.view animated:YES];
CMStrongSelf;
if (!error) {
if (model && [model isKindOfClass:[NSArray class]]) {
NSArray * modelArray = (NSArray *)model;
if (modelArray.count>0) {
[self.topModelArray setArray:modelArray];
}
}
[self saveTopModelArrayToArchive];
}else{
NSArray * topModelArray = [self getTopModelArrayFromArchive];
if (topModelArray) {
[self.topModelArray setArray:topModelArray];
}
}
NSInteger headerSelectIndex = [self getHeaderIndexFromFirstLoad];
[self setupHederViewWithTitleArray:[self getTopTitleArray] selectedIndex:headerSelectIndex];
[self setupContentView];
[self refreshContentViewWithHeaderIndex:headerSelectIndex];
}];
}
1.我把titleArray这个总结了下,提取出一个方法。
2.headerSelectIndex也提取了一个方法。
整个代码逻辑清爽了很多,一屏能显示完。然后每个变量和方法都有清晰的命名。如果可以处理error那段也可以提取出来,可以是下面这个样子:
- (void)requestWithNavRankCatalogConfig{
[MBProgressHUD showHUDAddedTo:self.view animated:YES];
CMWeakSelf;
[CMCategoryAPI getClientNavRankCatalogConfigRequestWithBlock:^(id model, NSError * _Nonnull error) {
[MBProgressHUD hideHUDForView:self.view animated:YES];
CMStrongSelf;
self.topModelArray = [self getModelArrayFromModel:model error:error];
NSInteger headerSelectIndex = [self getHeaderIndexFromFirstLoad];
[self setupHederViewWithTitleArray:[self getTopTitleArray] selectedIndex:headerSelectIndex];
[self setupContentView];
[self refreshContentViewWithHeaderIndex:headerSelectIndex];
}];
}
现在可以看看这段代码含义:requestWithNavRankCatalogConfig方法发起了一个网络请求,处理返回数据,处理topModelArray,获取headerSelectIndex,然后setup(初始化view),刷新view。我想比起第一段,最后这段的可读性明显强了许多。
总结:
1.我想最后面这段代码不影响阅读了,优化也要适可而止。暂时我不打算修改这段代码,当然如果后面有业务变更,当他变的很长的时候,我会打算再做优化。
2.有一些具体的方法可参考:代码重构笔记,如果想学习的更全面,请阅读<<代码重构笔记>>这本书籍,我相信会给你带来不一样的收货。
网友评论