命名规范

2020-04-02  本文已影响0人  SunshineBrother

1、条件表达式

//good
if (condition1() && 
    condition2() && 
    condition3() && 
    condition4()) {
    // Do something
    }
//bad
if (condition1() && condition2() && condition3() && condition4()) { // Do something }
- (void)doHomework
{
    if (self.hungry) {
        return;
    }
 
    ...
}
// bad
if (self.hungry) self.eat() 
// good
if (self.hungry) {
    self.eat()
}
if ( currentCursor == 2 ) { //... }

2、类名

3、枚举

枚举的命名和类的命名相近。

typedef NS_ENUM(NSInteger, UIControlContentVerticalAlignment) {
    UIControlContentVerticalAlignmentCenter  = 0,
    UIControlContentVerticalAlignmentTop     = 1,
    UIControlContentVerticalAlignmentBottom  = 2,
    UIControlContentVerticalAlignmentFill    = 3,
};

4、宏

#define HOME_PAGE_DID_SCROLL @"com.xq.home.page.tableview.did.scroll"
#define KHomePageDidScroll @"com.xq.home.page.tableview.did.scroll"

5、属性

书写规则,基本上就是 @property 之后空一格,括号,里面的 线程修饰词、内存修饰词、读写修饰词,空一格 类 对象名称 根据不同的场景选择合适的修饰符。

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, assign, readonly) BOOL loading;   
@property (nonatomic, weak) id<#delegate#> delegate;
@property (nonatomic, copy) <#returnType#> (^<#Block#>)(<#parType#>);

6、私有变量

推荐以 _开头,写在 .m 文件中。例如 NSString * _somePrivateVariable

7、代理方法

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath;

- (void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath;

8、方法

- (instancetype)init
{
    self = [super init];
    if (self) {

    }
    return self;
}

- (void)doHomework:(NSString *)name
            period:(NSInteger)second
            score:(NSInteger)score;
//good
- (instancetype)initWithAge:(NSInteger)age name:(NSString *)name;

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;


//bad
- (instancetype)initWithAge:(NSInteger)age andName:(NSString *)name;

- (void)tableView:(UITableView *)tableView :(NSIndexPath *)indexPath;
#pragra mark - life cycle 

#pragra mark - notification 

#pragra mark - action 

#pragra mark - UITableViewDelegate
.....总之这里是各种代理就对了

#pragra mark - UI
- (void)loadView;

#pragra mark - setter & getter

9、图片资源

10、版本规范

采用 A.B.C 三位数字命名,比如:1.0.2,当有更新的情况下按照下面的依据

版本号 右说明对齐标题 示例
A.b.c 属于重大内容的更新 1.0.2 -> 2.0.0
a.B.c 属于小部分内容的更新 1.0.2 -> 1.1.1
a.b.C 属于补丁更新 1.0.2 -> 1.0.3

11、用名字表达代码含义

一些比较有表达力的单词:

单词 可替代单词
send deliver、dispatch、announce、distribute、route
find search、extract、locate、recover
start launch、create、begin、open
make create、set up、build、generate、compose、add、new

使用 i、j、k 作为循环迭代器的名字过于简单,user_i、member_i 这种名字会更有表达力。因为循环层次越多,代码越难理解,有表达力的迭代器名字可读性会更高。

为名字添加形容词等信息能让名字更具有表达力,但是名字也会变长。名字长短的准则是:作用域越大,名字越长。因此只有在短作用域才能使用一些简单名字。

12、名字不能带来歧义

起完名字要思考一下别人会对这个名字有何解读,会不会误解了原本想表达的含义。

13、良好的代码风格

适当的空行和缩进。
排列整齐的注释:

int a = 1;   // 注释
int b = 11;  // 注释
int c = 111; // 注释

14、编写注释

阅读代码首先会注意到注释,如果注释没太大作用,那么就会浪费代码阅读的时间。那些能直接看出含义的代码不需要写注释,特别是并不需要为每个方法都加上注释,比如那些简单的 getter 和 setter 方法,为这些方法写注释反而让代码可读性更差。

不能因为有注释就随便起个名字,而是争取起个好名字而不写注释。

可以用注释来记录采用当前解决办法的思考过程,从而让读者更容易理解代码。

注释用来提醒一些特殊情况。

用 TODO 等做标记:

标记 用法
TODO 待做
FIXME 待修复
HACK 粗糙的解决方案
XXX 危险!这里有重要的问题

15、版本号

我们都知道版本号是x.x.x这样的。关于它的命名规则,这里有篇简单的文章可以浏览一下版本号命名规范及原则,大致意思就是:

上一篇 下一篇

猜你喜欢

热点阅读