My App 之 代碼整潔

在我們開始編碼之前,其實(shí)我想每一個(gè)人都有其自己的習(xí)慣,也就是自己編碼習(xí)慣 ,然而我們的自己習(xí)慣都是對(duì)的嗎?或者說你看的習(xí)慣其他人的代碼風(fēng)格嗎? 有一些潔癖或者說處女座風(fēng)格的你我,都是會(huì)有所追求的。

先一個(gè) Test 類 開始...

#import "PQTestViewController.h"
#import <Masonry.h>

#define PQ_SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height
#define PQ_SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width

static const CGFloat kTestTableViewCellHeight = 50.0f;
static NSString *const kTestTableViewCellIden = @"kTestTableViewCellIden";

@interface PQTestViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArray;

@end

@implementation PQTestViewController

#pragma mark - Life Cycle
- (void)viewDidLoad {
    [super viewDidLoad];
    [self pqInitView];
    [self pqLayoutView];
    [self requestData];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    //:To Do
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    //:To Do
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    //:To Do
}

- (void)dealloc {
    NSLog(@"TestVC Dealloc");
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

#pragma mark - Method
- (void)requestData {
    // Get data
}

#pragma mark - Delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kTestTableViewCellIden forIndexPath:indexPath];
    // cell.textLabel.text = self.dataArray[indexPath.row];
    return cell;
}

#pragma mark - InitAndLayout
- (void)pqInitView {
    [self.view addSubview:self.tableView];
}

- (void)pqLayoutView {
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsZero);
    }];
}

#pragma mark - Getter
- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.rowHeight = kTestTableViewCellHeight;
        [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:kTestTableViewCellIden];
    }
    return _tableView;
}

- (NSMutableArray *)dataArray {
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}
@end

#import <UIKit/UIKit.h>

//用戶性別
typedef NS_ENUM(NSUInteger, TestUserEnumSexType){
    TestUserEnumSexTypeSecret = 0, // 0 保密
    TestUserEnumSexTypeMale, //  1 男
    TestUserEnumSexTypeFemale // 2 女
};

@interface PQTestViewController : UIViewController

/**
    用戶性別選擇
 */
@property (nonatomic, assign) TestUserEnumSexType userSexType;

@end

對(duì)于具體的代碼規(guī)范,我是推薦:

而我們組也總結(jié)了下 GB 代碼規(guī)范(此處是可持續(xù)更新的),雖說有點(diǎn)亂,但是還是還很齊的。
另外我再補(bǔ)充兩個(gè)建議點(diǎn):

就是類似下面這種空行:

// 建議
@interface PQTestViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArray;

@end
// 不建議
@interface PQTestViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArray;
@end

當(dāng)然方法之間肯定是要空行的,同時(shí)如屬性單詞之間的空格,和方法名前的空格,也需要注意。

// 建議
@property(nonatomic,strong)UITableView*tableView;
-(void)requestData
// 不建議
@property (nonatomic, strong) UITableView *tableView;
- (void)requestData
順序
#pragma mark - Life Cycle
#pragma mark - Method
#pragma mark - Delegate
#pragma mark - InitAndLayout
#pragma mark - Getter

個(gè)人習(xí)慣在么一個(gè)類中,按模塊分區(qū)域,最上面是 聲明周期,下面是方法,緊接著是代理,然后再是布局,最后是懶加載,這樣感覺每一個(gè)類中,不需要通過搜索,直接很快就可以下意思的找到我們想找的地方。

另外同時(shí)例如在生命周期這個(gè)模塊中,我也是推薦按順序走的

// 建議
- (void)viewDidLoad 

- (void)viewWillAppear:(BOOL)animated 

- (void)viewDidAppear:(BOOL)animated 

- (void)viewWillDisappear:(BOOL)animated

- (void)dealloc
// 不建議
- (void)viewWillAppear:(BOOL)animated 

- (void)dealloc

- (void)viewDidAppear:(BOOL)animated 

- (void)viewWillDisappear:(BOOL)animated

- (void)viewDidLoad 

假如這個(gè)順序亂了,我都感覺怪怪的,畢竟其生命周期是這樣的嘛...

這樣一下來,相對(duì)來說每一個(gè)類都會(huì)更清晰,感覺會(huì)更舒服,也利于后期的開發(fā)和維護(hù)的。
當(dāng)然,每一個(gè)團(tuán)隊(duì)或多或少都有自己的規(guī)范,不同之處都可理解,但是規(guī)范一定還是要有的。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 蘋果官方文檔翻譯 《Objective-C語言編程》(Programming with Objective-C) ...
    fever105閱讀 26,320評(píng)論 19 129
  • iOS編程規(guī)范0規(guī)范 0.1前言 為??高產(chǎn)品代碼質(zhì)量,指導(dǎo)廣大軟件開發(fā)人員編寫出簡(jiǎn)潔、可維護(hù)、可靠、可 測(cè)試、高效...
    iOS行者閱讀 4,606評(píng)論 21 35
  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,021評(píng)論 25 709
  • 推薦文章:禪與 Objective-C 編程藝 前言 為??高產(chǎn)品代碼質(zhì)量,指導(dǎo)廣大軟件開發(fā)人員編寫出簡(jiǎn)潔、可維護(hù)、...
    MurtoTien閱讀 2,941評(píng)論 0 1
  • 詩是什么 是縱容 是饑渴 是不知疲倦的吶喊 是心高氣傲的嗔狂 是自由無邊上的一匹野馬 是天馬行空里的一道雪花 是戰(zhàn)...
    炫浪破天閱讀 449評(píng)論 0 0

友情鏈接更多精彩內(nèi)容