iOS worst practice

iOS worst practice

最近在重构公司的项目,随着对项目的逐渐深入(被坑的次数越来越多),被各路大牛的神迹所折服,以至于感觉这些人是不是我们竞争对手派来的卧底。这些坑可以算是iOS worst practice了,在这里总结一下。

命名的问题

给变量或者类以及方法起名能从一个侧面反映出一个程序员的水平以及项目的管理情况。在这个项目中,方法名、变量名和类名都没有统一,而且相关业务命名也没有规定。比如用于引导页面就有TechViewIntroduceViewguideView等,而下面的这些命名真是让人看了直接让人凌乱:

方法:

-(void)preTeachView2KnowActionWithNoShow:(UIButton *)sender

-(void)setTableView_Up_Down_NoHidden

变量:

#define fuckvalue -34

_info_tmp

NSString *fuck1;

NSString *fuck2;

类:

@interface New_AdPersonViewController : New_PersonDetailViewController

代码复用性

不会Ctrl-C/Ctrl-V的程序员不是好程序员,不过有些人玩得太high了,看你的工程中到处都是下面这些代码的时候你是什么感觉



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell;
    if(indexPath.section == 0)
    {
        cell = [self setTableView:tableView CellForRowAtIndexPath:indexPath section:0];
    }
    else if(indexPath.section == 1)
    {
        cell = [self setTableView:tableView CellForRowAtIndexPath:indexPath section:1];
    }
    else if(indexPath.section == 2)
    {
        cell = [self setTableView:tableView CellForRowAtIndexPath:indexPath section:2];
    }
    else if(indexPath.section == 3)
    {
        cell = [self setTableView:tableView CellForRowAtIndexPath:indexPath section:3];
    }
    else if(indexPath.section == 4)
    {
        cell = [self setTableView:tableView CellForRowAtIndexPath:indexPath section:4];
    }
    else if(indexPath.section == 5)
    {
        cell = [self setTableView:tableView CellForRowAtIndexPath:indexPath section:5];
    }
    else if(indexPath.section == 6)
    {
        cell = [self setTableView:tableView CellForRowAtIndexPath:indexPath section:6];
    }
    else if(indexPath.section == 7)
    {
        cell = [self setTableView:tableView CellForRowAtIndexPath:indexPath section:7];
    }
    else if(indexPath.section == 8)
    {
        cell = [self setTableView:tableView CellForRowAtIndexPath:indexPath section:8];
    }
    return cell;
}

 NSString *sectionTitle = @"";
        if(indexPath.section == 0)
        {
            sectionTitle = [_arraySections objectAtIndex:0];
        }
        else if(indexPath.section == 1)
        {
            sectionTitle = [_arraySections objectAtIndex:1];
        }
        else if(indexPath.section == 2)
        {
            sectionTitle = [_arraySections objectAtIndex:2];
        }
        else if(indexPath.section == 3)
        {
            sectionTitle = [_arraySections objectAtIndex:3];
        }
        else if(indexPath.section == 4)
        {
            sectionTitle = [_arraySections objectAtIndex:4];
        }
        else if(indexPath.section == 5)
        {
            sectionTitle = [_arraySections objectAtIndex:5];
        }
        else if(indexPath.section == 6)
        {
            sectionTitle = [_arraySections objectAtIndex:6];
        }
        else if(indexPath.section == 7)
        {
            sectionTitle = [_arraySections objectAtIndex:7];
        }
        else if(indexPath.section == 8)
        {
            sectionTitle = [_arraySections objectAtIndex:8];
         }

反正我看了第一感觉是,我x、原来以前公司发工资是按代码行数算钱的······

没有面向对象思想

  • 在ViewController基类中放一些特定的业务逻辑,然后所有的子类中都有一堆阴魂不散的代码
  • 该封装的不封装,完成同样功能的函数在每个用到的地方都一个copy,一旦逻辑变动,改起来真心好爽
  • 不该封装的乱封装,封装的暴露的方法完全不知道是干什么的,想要类运行还必须先设定某些它所依赖的全局变量

滥用单例、通知

实际上工程里面单例就一个,不过单例对象把所有的活都干,所以可以理解为整个工程的架构,都是面向过程的,全部模块水乳交融、浑然一体。用户数据保存靠这个单例、具体页面数据设置靠靠这个单例、应用行为配置靠靠这个单例······然后大部分的ViewController想要正常工作都得提前把所需的数据在这个单例中配置好,想想都佩服那些大神的记忆力;对于滥用通知的后果和单例一样,当你见到一个控制器中有一二十条通知齐刷刷地写在初始化方法里的时候有什么感觉。想想都有点小激动,你确定不是在逗我?

各种脑洞大开的实现

看见一个函数名叫做addRequestQueue的时候你会有什么反应,大部分人肯定想着肯定会是生成请求实例、有请求队列对它们进行管理······对此我只能说大神的想象力是无穷的,这个addRequestQueue是用递归的方式发送的请求,在网络请求成功或失败的闭包中会再次调用自己发送请求,这时候我终于明白这个队列是在哪儿了。

类似的例子还有很多,一次次的被大神的想象力所折服!

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值