前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >iOS - 代码Review(一)

iOS - 代码Review(一)

作者头像
Wilbur-L
修改2021-10-27 14:27:20
5920
修改2021-10-27 14:27:20
举报

一·代码命名基础

1.清晰且简短

insertObject: atIndex: //通过
insert: at: //不清楚在插入什么
removeObjectAtIndex: //通过,即使有点长 代码命名优先级 清晰>简短
removeObject: //通过
remove: //不清楚 正在删除什么

2.不要缩写,哪怕很长

destinationSelection //驼峰命名 不缩写 通过
destSel //不清楚
setBackgroundColor: //设置背景颜色 通过
setBkgColor: //不清晰

注:虽然有些写法是缩写,但需要考虑到遇到你的代码或函数名称可能是不同语言文化背景习惯的人时候

3.一致&避免与系统函数冲突

- (NSInterger)tag //tag 已经在NSView,NSCell,NSControll 中已经有定义
- (void)setStringValue:(NSStirng *)str //在UIKit Cocoa中 声明property会具有setter getter 除非你要重写

4.排版约定 (每个公司的代码规范会有些不同,但大都遵循一定的规范)

在一份view.m文件中应遵循统一的排版
//
//  .m
//  app
//
//  Created by you on 202x/x/xx.
//  Copyright © 2021 you. All rights reserved.
//

#import "Foundation.h" //引入头文件
#import "NetWorkManager" //网络层单独封装
@interface xxx()<遵循协议,UICollectionViewDelegate,UICollectionViewDataSource>
//UI层
@property (nonatomic, strong) UIButton *XXXCloseButton;
@property (nonatomic, strong) UIView *backgroundView;
//网络参数层
@property (nonatomic, storng) NSArray *dataArray; 
@property (nonatomic, assign) BOOL didPayStatus;
@end

@implementation xxx
#pragma mark - 生命周期

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDidAppear:animated];
}

- (void)viewDidLoad {
    [super viewDidLoad];
}

#pragma mark - 初始化
- (instancetype)init {
    if (self = [super init]) {
        [self initUI];
    }
    return self;
}

- (void)initUI {
    _dataArray = [NetWorkManager sendPostNetWorkRequest];
    [self addSubview:self.backgroundView];
    [self addSubview:self.XXXCloseButton];
}

#pragma mark - tableView协议&代理

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kCell forIndexPath:indexPath];
    cell.model = self.model[indexPath.row];
    return cell;
} //根据MVC架构需要把数据通过Model传给视图
//每一个函数间隔一行
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.model.count; 
}

#pragma mark - Action&Selector

- (void)yourNamedFuntion:(UIButton *)sender {
    NSLog(@"按钮方法");
}

#pragma mark - 懒加载 //添加标记容易找
//懒加载放在最后
- (UIView *)backgroundView {
    if(!_backgroundView) {
        _backgroundView = [[UIView alloc]init];
        _backgroundView.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight); //宽高使用宏定义 x,y默认0
        _backgroundView.bottom = xx //添加x y 约束
        _backgroundView.left = xx //添加x y 约束
        _backgroundView.backgroundColor = [UIColor blueColor];
    }
    return _backgroundView;
}
//按照视图加载顺序 addSubview 1.背景 addSubview 2按钮
- (UIButton *)XXXCloseButton {
    if (!_XXXCloseButton) {
        _XXXCloseButton = [[UIButton alloc]init];
        _XXXCloseButton.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight); //请把括号内的空格也填好
        _XXXCloseButton.layer.cornerRadius = 8;
        [_XXXCloseButton addTarget:self action:@selector(yourNamedFuntion:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _XXXCloseButton
}

@end

二·命名方法

1.如果函数会对当前对象操作,以动词开头

 - (void)invokeWithTarget:(id)target;
 - (void)selectTabViewIndex:(NSInterger)index;
 //不要使用do does 这些助动词

2.如果方法返回接受者对属性

- (NSSize)cellSize;
- (NSSize)calcCellSize; //不清晰
- (NSSize)getCellSize; //不要使用助动词

3.参数前使用关键字

 - (void)buttonAction:(int)index name:(NSString *)name; //通过
 - (void)buttonAction:(int)index :(NSString *)name; //不能忽略关键字

4.当函数只有一个参数时候需要提前描述

 - (void)viewWithTag:(NSInterger)aTag;

5.当重写构造方法时候需要把参数添加到末尾

- (instancetype)initWithFrame:(CGRect)frame
- (instancetype)initWithFrame:(CGRect)frame dataArray:(NSArray *)dataArray;

6.不要通过And来链接属性关键字

- (int)runModelForDirectory:(NSString *)path andFile:(NSString *)fileName; //不通过
- (int)runModelForDirectory:(NSString *)path WithFile:(NSString *)fileName; //通过
//通常来说一般函数需要的参数最好不要超过4个

7.通知KVO和异常

[关联类的名称] + [Did | Will] + [房间] + 通知

static NSString *const CurrenClassNameWillMoveNotification = @"CurrenClassNameWillMoveNotification";
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(turnBackToRoom:) name:@"CurrenClassNameWillMoveNotification" object:nil];

8.枚举

对具有整数相关的常数使用枚举

有命名枚举

typedef enum : NSInteger{
    acceptOrder = 0,
    denyOrder,
    refundOrder,
    rejectOrder,
    finishedOrder
}status;

无命名枚举

enum {
    acceptOrder = 0,
    denyOrder = 1 << 0,
    refundOrder = 1 << 1,
    rejectOrder = 1 << 2,
    finishedOrder = 1 << 3,
};

本文系外文翻译,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文系外文翻译前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一·代码命名基础
  • 二·命名方法
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档