美文网首页
简单-项目开发步骤之代码规范

简单-项目开发步骤之代码规范

作者: Www刘 | 来源:发表于2016-03-30 09:35 被阅读45次

1.建议用.符号代替括号用于设置或获取属性值

For example:

view.backgroundColor = [UIColororangeColor];[UIApplicationsharedApplication].delegate;

Not:

[viewsetBackgroundColor:[UIColororangeColor]];

UIApplication.sharedApplication.delegate;

2.空格

a.缩进必须使用4个空格。没有缩进和制表符。

b.一定要在Xcode中设置此首选项。方法括号和其他括号(如果/其他/开关/等等)。必须打开在同一行语句。括号必须关闭在一个新行。

For example:

if(user.isHappy) {

// Do something

}

else{

// Do something else

}

3.条件语句:必须有括号

For example:

if(!error) {returnsuccess;}

Not:

if(!error)returnsuccess;

or

if(!error)returnsuccess;

4.三元运算符:三元操作符的意图,?,是增加清晰或代码整洁。三元应该只评估每一个条件表达式。评估多个条件通常是更容易理解作为一个if语句或重构命名变量。

For example:

result = a > b ? x : y;

Not:

result = a > b ? x = c > d ? c : d : y;

5.错误处理:当方法返回一个错误参数通过引用,代码必须打开返回值,不得打开错误变量。

For example:

NSError*error;if(![selftrySomethingWithError:&error]) {// Handle Error}

Not:

NSError*error;[selftrySomethingWithError:&error];if(error) {// Handle Error}

6.方法

For example:

- (void)setExampleText:(NSString*)text image:(UIImage *)image;

7.变量:见名知意

For example:

NSString *title: It is reasonable to assume a “title” is a string.

NSString *titleHTML: This indicates a title that may contain HTML which needs parsing for display.“HTML” is needed for a programmer to use this variable effectively.

NSAttributedString *titleAttributedString: A title, already formatted for display.AttributedStringhints that this value is not just a vanilla title, and adding it could be a reasonable choice depending on context.

NSDate *now:No further clarification is needed.

NSDate *lastModifiedDate: SimplylastModifiedcan be ambiguous; depending on context, one could reasonably assume it is one of a few different types.

NSURL *URLvs.NSString *URLString: In situations when a value can reasonably be represented by different classes, it is often useful to disambiguate in the variable’s name.

NSString *releaseDateString: Another example where a value could be represented by another class, and the name can help disambiguate.

Single letter variable names are NOT RECOMMENDED, except as simple counter variables in loops.

Asterisks indicating a type is a pointer MUST be “attached to” the variable name.For example,NSString *textnotNSString* textorNSString * text, except in the case of constants (NSString * const NYTConstantString).

For example:

interfaceNYTSection:NSObject@property(nonatomic)NSString*headline;@end

Not:

@interfaceNYTSection:NSObject{NSString*headline;}

8.变量限定符:

For example:

NSString * __weak text.

9.命名:描述性的方法和变量名越清楚越好

For example:

UIButton *settingsButton;

Not

UIButton *setBut;

For example:

staticconstNSTimeIntervalNYTArticleViewControllerNavigationFadeAnimationDuration =0.3;

Not:

staticconstNSTimeIntervalfadetime =1.7;

For example:

@synthesize descriptiveVariableName = _descriptiveVariableName;

Not:

idvarnm;

10.分类

类别建议简明地细分功能,用来描述该功能

For example:

@interfaceUIViewController(NYTMediaPlaying)@interfaceNSString(NSStringEncodingDetection)

Not:

@interfaceNYTAdvertisement(private)

@interfaceNSString(NYTAdditions)

For example:

@interfaceNSArray(NYTAccessors)- (id)nyt_objectOrNilAtIndex:(NSUInteger)index;@end

Not:

@interfaceNSArray(NYTAccessors)

- (id)objectOrNilAtIndex:(NSUInteger)index;

@end

11.注释

When they are needed, comments SHOULD be used to explainwhya particular piece of code does something. Any comments that are used MUST be kept up-to-date or deleted.

Block comments are NOT RECOMMENDED, as code should be as self-documenting as possible, with only the need for intermittent, few-line explanations. This does not apply to those comments used to generate documentation.

12.init and dealloc

dealloc方法应放置在顶部的实现,直接放在@ synthesize和@dynamic语句之后。init方法应该直接放置在dealloc任何类的方法。

- (instancetype)init {

self = [superinit];// or call the designated initializer

if(self) {

// Custom initialization

}

returnself;

}

12.文字

For example:

NSArray*names = @[@"Brian",@"Matt",@"Chris",@"Alex",@"Steve",@"Paul"];NSDictionary*productManagers = @{@"iPhone":@"Kate",@"iPad":@"Kamal",@"Mobile Web":@"Bill"};NSNumber*shouldUseLiterals = @YES;NSNumber*buildingZIPCode = @10018;

Not:

NSArray*names = [NSArrayarrayWithObjects:@"Brian",@"Matt",@"Chris",@"Alex",@"Steve",@"Paul",nil];

NSDictionary*productManagers = [NSDictionarydictionaryWithObjectsAndKeys:@"Kate",@"iPhone",@"Kamal",@"iPad",@"Bill",@"Mobile Web",nil];

NSNumber*shouldUseLiterals = [NSNumbernumberWithBool:YES];

NSNumber*buildingZIPCode = [NSNumbernumberWithInteger:10018];

13.CGRECT

For example:

CGRectframe = self.view.frame;CGFloatx = CGRectGetMinX(frame);CGFloaty = CGRectGetMinY(frame);CGFloatwidth = CGRectGetWidth(frame);CGFloatheight = CGRectGetHeight(frame);

Not:

CGRectframe = self.view.frame;

CGFloatx = frame.origin.x;

CGFloaty = frame.origin.y;

CGFloatwidth = frame.size.width;

CGFloatheight = frame.size.height;

14.常量

For example:

staticNSString*constNYTAboutViewControllerCompanyName =@"The New York Times Company";staticconstCGFloatNYTImageThumbnailHeight =50.0;

Not:

#defineCompanyName@"The New York Times Company"

#definethumbnailHeight2

15.枚举

Example:

typedefNS_ENUM(NSInteger, NYTAdRequestState) {

NYTAdRequestStateInactive,

NYTAdRequestStateLoading

};

16.在处理位掩码时,必须使用NS_OPTIONS宏。

Example:

typedefNS_OPTIONS(NSUInteger, NYTAdCategory) {

NYTAdCategoryAutos      =1<<0,

NYTAdCategoryJobs       =1<<1,

NYTAdCategoryRealState  =1<<2,

NYTAdCategoryTechnology =1<<3

};

17.私有方法:

Private properties SHALL be declared in class extensions (anonymous categories) in the implementation file of a class.

For example:

@interfaceNYTAdvertisement()

@property(nonatomic,strong) GADBannerView *googleAdView;

@property(nonatomic,strong) ADBannerView *iAdView;

@property(nonatomic,strong) UIWebView *adXWebView;

@end

18.图片名称

图片应该命名为一个驼峰式大小写字符串的描述他们的目的,其次是无前缀的名称他们定制的类或属性(如果有的话),其次是进一步描述颜色和/或位置,最后他们的状态。

For example:

RefreshBarButtonItem/RefreshBarButtonItem@2xandRefreshBarButtonItemSelected/RefreshBarButtonItemSelected@2x

ArticleNavigationBarWhite/ArticleNavigationBarWhite@2xandArticleNavigationBarBlackSelected/ArticleNavigationBarBlackSelected@2x.

18.boolen

Values MUST NOT be compared directly toYES, becauseYESis defined as1, and aBOOLin Objective-C is aCHARtype that is 8 bits long (so a value of11111110will returnNOif compared toYES).

For an object pointer:

if(!someObject) {}if(someObject ==nil) {}

For aBOOLvalue:

if(isAwesome)if(!someNumber.boolValue)if(someNumber.boolValue ==NO)

Not:

if(isAwesome ==YES)// Never do this.

@property (assign, getter=isEditable)BOOLeditable;

20.单例

Singleton objects SHOULD use a thread-safe pattern for creating their shared instance.

+ (instancetype)sharedInstance {

staticidsharedInstance =nil;

staticdispatch_once_tonceToken;

dispatch_once(&onceToken, ^{

sharedInstance = [[[selfclass]alloc]init];

});

returnsharedInstance;

}

21.imports

If there is more than one import statement, statements MUST be groupedtogether. Groups MAY be commented.

Note: For modules use the@importsyntax.

// Frameworks

@import QuartzCore;

// Models

#import"NYTUser.h"

// Views

#import"NYTButton.h"

#import"NYTUserView.h"

22.协议

In adelegate or data source protocol, the first parameter to each method SHOULD be the object sending the message.

This helps disambiguate in cases when an object is the delegate for multiple similarly-typed objects, and it helps clarify intent to readers of a class implementing these delegate methods.

For example:

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

Not:

- (void)didSelectTableRowAtIndexPath:(NSIndexPath*)indexPath;

相关文章

  • 简单-项目开发步骤之代码规范

    1.建议用.符号代替括号用于设置或获取属性值 For example: view.backgroundColor ...

  • 乐乎-代码规范概述

    参考: 1、iOS中书写代码规范35条小建议 2、iOS开发总结之代码规范 3、iOS代码编程规范-根据项目经验汇...

  • iOS开发总结之代码规范

    iOS开发总结之代码规范

  • 项目开发代码规范

    HTML部分 1.属性顺序永远排前三的属性1.class 2.id 3.name2.class和id命名class...

  • 前端开发规范

    前端代码规范 Front Standard Guide 前端 JS 项目开发规范 规范的目的是为了编写高质量的代码...

  • 项目管理内容

    1、项目进度把控(开发时间、测试接入) 2、项目质量把控(代码规范、接口规范、文档规范) 3、项目人力把控 4、项...

  • Xcode一键进行项目格式化

    在多个人开发的项目中由于每个的代码规范不同导致项目中的代码也是不尽相同,也可能会忙着写代码而忽略了格式的问题 在之...

  • 简单-项目开发步骤

    1.为工程设置编码风格规范 编码风格规范指的是在使用特定语言写代码之前要明确遵守的风格和惯例,它包括类似于该使用t...

  • 项目开发规范参考

    现有项目的开发规范文档 目录 命名规则文件命名 HTML规范 CSS规范 JS规范变量申明简写代码性能优化注释规范...

  • iOS开发规范

    iOS代码编写规范Git的使用iOS进阶开发 目的 了利于项目维护以及规范开发,促进成员之间Code Review...

网友评论

      本文标题:简单-项目开发步骤之代码规范

      本文链接:https://www.haomeiwen.com/subject/jtgslttx.html