iPhone开发重构:提取公用的方法以清理重复代码

   无论在iPhone开发还是学习的过程中都会看到一些不是很理想的代码,不可否认自己也在不断“贡献”着这类代码。面对一些代码的“坏味道”,重构显然是个有效的解决途径。《iPhone开发重构》系列就想总结和补充iPhone开发中经历的一些重构,其间可能会引用一些开源以及实际项目的代码,本着对技术的探求,冒昧之处还请作者多多见谅。

 

   代码重复是一个比较明显的“坏味道”,提取公用的方法就是解决的途径之一。iPhone开发中,使用UITableView的时候就有如下一段“经典”的模板代码,因为这是项目模板自动生成的,所以很多人就自然接受了。但随着越来越多地通过copy&paste在一个项目中使用这段代码,大家是否有些采取行动的压力呢?好吧,我们就从这“动刀”吧!

 

重构前:

- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"wikiHowCell";
    UITableViewCell *cell = (WHTableViewCell *)[tv dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[WHTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.title = [[featuredArticles objectAtIndex:indexPath.row] objectForKey:@"title"];
    return cell;
}

 

   对此我们可以提取个公共的方法,并放置在一个适当的地方。UITableViewCell的Category应该是一个比较好的去处。重构后提取的方法以及实际调用的代码如下:

重构后:

@implementation UITableViewCell(Cache)

+ (id)dequeOrCreateInTable:(UITableView*)tableView withId: (NSString*)reuseId andStyle:(UITableViewCellStyle)style {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseId];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:style reuseIdentifier:reuseId] autorelease];
    }
    return cell;
}

@end

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [UITableViewCell dequeOrCreateInTable:tableView withId:@"wikiHowCell" andStyle:UITableViewCellStyleDefault];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.title = [[featuredArticles objectAtIndex:indexPath.row] objectForKey:@"title"];
    return cell;
}

   从代码量衡量,仅从此处可能感觉重构前后变化不大,甚至还会略有增多。但如果考虑到公用方法的多次使用产生的“效益”,付出的努力应该是值当的!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值