代码的坏味道

7.Feature Envy(依恋情结)

表现形式:
  • 一个类的函数对其他类的操作多于自己类中数据的操作。
重构方案:
  • 对其他类的操作提炼出来放到一个函数中
  • 将提炼出来的函数移动到对应类中
例子:
 //请求圈子顶部运营广告位
    [[self.subject_requestAdsBannerInfo flattenMap:^RACStream *(id value) {
        return [[[GetAppHomeBannerList signal_request] doNext:^(CallBackModel_GetAppHomeBannerList* x) {
            // 解析数据
            @strongify(self);
            {
                NSArray *bannerListArray = x.bannerListArray;
                if(bannerListArray.count >0){
                    
                    NSMutableArray *bannerObjects = [NSMutableArray array];
                    for (int index = 0; index < bannerListArray.count; index++) {
                        GetAppHomeBannerList_BannerListObject *bannerObject = bannerListArray[index];
                        HZBannerObject *bannerObj = [HZBannerObject new];
                        bannerObj.coverUrl = bannerObject.bannerImgUrl;
                        bannerObj.bannerTitle = bannerObject.title;
                        bannerObj.bannerType = [bannerObject.targetType intValue];
                        // 后端targetInfo配置时需要注意
                        switch (bannerObj.bannerType) {
                            case bannerTypePostList:
                            {
                                HZTagPostObject *tagObject = [HZTagPostObject new];
                                NSDictionary *postInfoDict = [self serializationBannerInfo:bannerObject.targetInfo];
                                tagObject.tagId = postInfoDict[@"tagId"];
                                tagObject.pageNumber = postInfoDict[@"pageNumber"];
                                
                                bannerObj.bannerObject = tagObject;
                            }
                                break;
                            case bannerTypeArticleInfoPage:
                            {
                                GetPostList4C_PostListObject *postObject = [GetPostList4C_PostListObject new];
                                postObject.postId = [[self serializationBannerInfo:bannerObject.targetInfo] objectForKey:@"postId"];
                                HZTagModel *tagModel = [[HZTagModel alloc]init];
                                tagModel.tagId = [[self serializationBannerInfo:bannerObject.targetInfo] objectForKey:@"tagId"];
                                postObject.tagListArray = @[tagModel];
                                postObject.postType = [NSString stringWithFormat:@"%d", PostType_article];
                                bannerObj.bannerObject = postObject;
                            }
                                break;
                            case bannerTypeVideoInfoPage:
                            {
                                GetPostList4C_PostListObject *postObject = [GetPostList4C_PostListObject new];
                                postObject.postId = [[self serializationBannerInfo:bannerObject.targetInfo] objectForKey:@"postId"];
                                HZTagModel *tagModel = [[HZTagModel alloc]init];
                                tagModel.tagId = [[self serializationBannerInfo:bannerObject.targetInfo] objectForKey:@"tagId"];
                                postObject.tagListArray = @[tagModel];
                                postObject.postType = [NSString stringWithFormat:@"%d", PostType_video];
                                bannerObj.bannerObject = postObject;
                            }
                                break;
                            case bannerTypeWebDetail:
                            {
                                NSString *url = [NSString stringWithFormat:@"%@%@",[[INIT share] interfaceAddressFromKey:@"resourceHost"],bannerObject.targetInfo];
                                NSString *poCode = [[[[INIT share] activityInfo] experienceBenfit] poInfo];
                                NSString *activityUrl = [NSString stringWithFormat:@"%@?poCode=%@",url,poCode];
                                bannerObj.bannerObject = activityUrl;
                            }
                                break;
                            default:
                                break;
                        }
                        //屏蔽掉无数据时的占位数据
                        //首页广告运营位无标题
                        if(bannerObj.coverUrl.length){
                            [bannerObjects addObject:bannerObj];
                        }
                    }
                    self.adBannerArray = [bannerObjects copy];
                }else{
                    self.adBannerArray = [NSArray new];
                }
            }
        }] catchTo:[RACSignal empty]];
    }] subscribeNext:^(id x) {
    }];

整个for 函数中大部分是对 HZBannerObject对象进行操作(整合数据)。我们可以将其提取到一个函数中并将这个函数放在HZBannerObject类中。

8.Data Clumps(数据泥团)

表现形式:
  • 两个类中多个相同的属性字段。
  • 多个函数签名中相同的参数。
  • 多个数据有关联的出现在一起。
重构方案:
  • 将其提炼到一个新的对象中
优点:
  • 减少字段和参数的个数
  • 新对象的重用
例子:
    NSArray *array = [self.arraySource objectAtIndex: indexPath.row];
    NSString * nameStr ;
    if (array.count>6) {
        nameStr = array[6];
    }else{
        nameStr = array[1];
    }

    [cell setPreAuthorizeCellWithViewDetailDirection:[[array objectAtIndex:0] integerValue]
                                     andDetailNumber:nameStr detailSort:[array objectAtIndex:2]
                                    andLabelTimeText:[array objectAtIndex:3]
                                       andCellStatus:[[array objectAtIndex:4] integerValue]
                                andTableCellPosition:[[array objectAtIndex:5] integerValue]];

可以将数组中的数据整合成一个对象。

9.Primitive Obsession(基本类型偏执)

表现形式:
  • 习惯于用基本类型的数据作为数据的传递工具。
重构方案:
  • 可以将两个基本类型的参数包装成一个对象,以对象的形式进行传递。
    [self.setPasswordInfo setInfoWithName:@"交易密码" placeHolder:@"请设置您的交易密码"];
  • 声明一个状态类(父类),抽象出一个方法。再创造子类,重写抽象方法。
if ([self.modelHospitalDetail.languageList[index] isEqualToString:@"03"]){
                [ICMBProgressHelper ShowNoticeWithTitle:nil Detail:CustomLocalizedString(@"支持语言:日语", nil)];
            }else if ([self.modelHospitalDetail.languageList[index] isEqualToString:@"04"]){
                [ICMBProgressHelper ShowNoticeWithTitle:nil Detail:CustomLocalizedString(@"支持语言:法语", nil)];
            }else if ([self.modelHospitalDetail.languageList[index] isEqualToString:@"05"]){
                [ICMBProgressHelper ShowNoticeWithTitle:nil Detail:CustomLocalizedString(@"支持语言:韩语", nil)];
            }else if ([self.modelHospitalDetail.languageList[index] isEqualToString:@"06"]){
                [ICMBProgressHelper ShowNoticeWithTitle:nil Detail:CustomLocalizedString(@"支持语言:泰语", nil)];
            }else if ([self.modelHospitalDetail.languageList[index] isEqualToString:@"07"]){
                [ICMBProgressHelper ShowNoticeWithTitle:nil Detail:CustomLocalizedString(@"支持语言:柬埔寨语", nil)];
            }else if ([self.modelHospitalDetail.languageList[index] isEqualToString:@"08"]){
                [ICMBProgressHelper ShowNoticeWithTitle:nil Detail:CustomLocalizedString(@"支持语言:马来语", nil)];
            }else if ([self.modelHospitalDetail.languageList[index] isEqualToString:@"09"]){
                 [ICMBProgressHelper ShowNoticeWithTitle:nil Detail:CustomLocalizedString(@"支持语言:粤语", nil)];
            }else if ([self.modelHospitalDetail.languageList[index] isEqualToString:@"10"]){
                 [ICMBProgressHelper ShowNoticeWithTitle:nil Detail:CustomLocalizedString(@"支持语言:德语", nil)];
            }else if ([self.modelHospitalDetail.languageList[index] isEqualToString:@"11"]){
                 [ICMBProgressHelper ShowNoticeWithTitle:nil Detail:CustomLocalizedString(@"支持语言:俄语", nil)];
            }else if ([self.modelHospitalDetail.languageList[index] isEqualToString:@"12"]){
                 [ICMBProgressHelper ShowNoticeWithTitle:nil Detail:CustomLocalizedString(@"支持语言:维吾尔语", nil)];
            }else if([self.modelHospitalDetail.languageList[index] isEqualToString:@"13"]){
                [ICMBProgressHelper ShowNoticeWithTitle:nil Detail:CustomLocalizedString(@"支持语言:西班牙语", nil)];
            }else if([self.modelHospitalDetail.languageList[index] isEqualToString:@"14"]){
                [ICMBProgressHelper ShowNoticeWithTitle:nil Detail:CustomLocalizedString(@"支持语言:瑞典语", nil)];
            }
            else if([self.modelHospitalDetail.languageList[index] isEqualToString:@"15"]){
                [ICMBProgressHelper ShowNoticeWithTitle:nil Detail:CustomLocalizedString(@"支持语言:越南语", nil)];
            }
            else if([self.modelHospitalDetail.languageList[index] isEqualToString:@"16"]){
                [ICMBProgressHelper ShowNoticeWithTitle:nil Detail:CustomLocalizedString(@"支持语言:印尼语", nil)];
            }
            else if([self.modelHospitalDetail.languageList[index] isEqualToString:@"17"]){
                [ICMBProgressHelper ShowNoticeWithTitle:nil Detail:CustomLocalizedString(@"支持语言:缅甸语", nil)];
            }
            else if([self.modelHospitalDetail.languageList[index] isEqualToString:@"18"]){
                [ICMBProgressHelper ShowNoticeWithTitle:nil Detail:CustomLocalizedString(@"支持语言:蒙古语", nil)];
            }
            else if([self.modelHospitalDetail.languageList[index] isEqualToString:@"19"]){
                [ICMBProgressHelper ShowNoticeWithTitle:nil Detail:CustomLocalizedString(@"支持语言:阿拉伯语", nil)];
            }
            else if([self.modelHospitalDetail.languageList[index] isEqualToString:@"20"]){
                [ICMBProgressHelper ShowNoticeWithTitle:nil Detail:CustomLocalizedString(@"支持语言:意大利", nil)];
            }
优点:
  • 可扩展。

10.Switch Statments(switch 惊悚现身)

表现形式:
  • 出现过多的Switch。
重构方案:
  • 用多态的方式代替。
  • 以明确的函数取代参数。
优点:
  • 解耦。
  • 可扩展。
例子:
第一个例子中有switch,我们可以采用的多态的方式,代替他。
首先以bannerType为父类建立对应的四个子类,四个子类有各自的bannerObject的实现方式。
在父类中添加要给公用的方法getBannerObject。

11.Parallel Inheritance Hierarchies(平行继承体系)

表现形式:
  • 某个继承体系的类名称前缀和另一个继承体系的类名称前缀完全相同。
  • 平行继承体系 是霰弹式修改的特殊情况,一种变化引发多个类的修改。
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容