Skip to content

使用Faux Pas 查找潜在Bug #18

Open

Description

@cssoul

Faux Pas是一个Xcode辅助工具,方便我们查找iOS项目存在的潜在bug,以及可维护性和风格问题。Faux Pas提供了107条规则可供我们选择,支持GUI和CLI。

官方地址:http://fauxpasapp.com/

得益于同事的推进,利用Faux Pas在我们的项目中找到一些比较典型的问题,这里整理出来避免以后再放类似的错误。

一、编码错误:

1、数组长度判断写法不对,比如:

if (styleEntity.list>0) { }

改为:

if ([styleEntity.list count]>0) { }

2、delegate属性没有使用weak修饰,比如:

@property (nonatomic) id<slideDelegate> delegate;
修改为:
@property (nonatomic,weak) id<slideDelegate> delegate;

3、词典应该使用strong修饰,比如:

@property (nonatomic, copy) NSMutableArray *brand_tag;

改为:

@property (nonatomic, strong) NSMutableArray *brand_tag;
二、查找资源错误

1、找不到资源文件

UIImage *image = [UIImage imageNamed:@"tomato"];
self.imageView.image = image;   //tomato图片并不存在,image为nil

2、非retina与retina图片资源丢失,比如:

Images.xcassets下面我们针对头像放置了avatar@2x.png,而没有avatar@.png,Faux Pas会提示我们:

“Asset catalog ‘Images’, set ‘avatar’: The ‘x1 scale, Universal’ version of this image is missing.”
二、编码规范&风格

1、pch中引用过的头文件再次引用

2、getter方法命名不规范,推进使用something而非getSomething

- (BOOL)getIsEnd{}

改为:
- (BOOL)isEnd{} 

3、三元运算符书写避免额外开销

return self.mbook ? self.mbook : @"";

改为:
return self.mbook ?  : @"";

4、release环境下使用 NSLog,比如:

代码中直接写:NSLog(@"touch start...");

我们可以加一个判断,再debug模式才会打印log:

#if DEBUG
NSLog(@"touch start..."); 
#endif

5、没有判断Array获取空值,比如:

NSString *paramStr = [params objectAtIndex:1];
推荐使用封装的Category方法:

- (id)safeObjectAtIndex:(NSInteger)index
{
    id obj = nil;
    if (index < self.count) {
        obj = [self objectAtIndex:index];
    }
    return obj;
}

NSString *paramStr = [params safeObjectAtIndex:1];

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions