注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

WA!Peter

广州天河, 香港火炭,深圳罗湖, 北京国贸

 
 
 

日志

 
 

Objective-C Coding Style  

2010-03-01 17:47:39|  分类: iphone |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
 CODING_STYLE for the MMO2iphone - Objective-C
===============================================
first draft by peter 2010-02-15
last update by peter 2010-03-01

Principle:
- readability
- consistent
- full word (if possible), allow very common shortform
- no chinese pinyin (unless for weapon/equipment names etc.)
- variable/function: small letter, with capital letter as separator
- constant: all capital, use _ as separator


Variable naming:
- primitive type:  int, char, byte, just give a simple name,
  e.g. int total, sum, i, j, k;   //  no need for totalInt
  for loop:  (outter) i < j < k (inner)
  for x,y :  x or y depends on your order of coding
  prefer in-loop declaration: for (int i=0; i<total; i++) 
  avoid:
  int i;
  for (i=0; i<total; i++) 
- Several NS object, use suffix:
  NSMutableArray *playerArray;
  NSMutableDictionary *playerDictionary;
  NSData * fullUpdateData;
- special for Byte * pointer: Byte * playerByte;
- Quick note:  use count for number of xxx,  use length for byte size
  MAX_XXX for constant.
- others: maxXXX, xxxCount, xxxLength, totalXXX, numberOfXXX, sizeXXX:
  xxxLength : usually refer to "byte length"  (size is acceptable)
  NOTE:  length follows NSData convention
  xxxCount, totalXXX, numberOfXXX: usually refer to number of items
  NOTE:  count follows NSArray convention
  maxXXX:  avoid this, reserve it for constant #define MAX_XXX


Constant
- #define MAX_ITEM 10
  all capital letters, use _ to separate


Cascading level:
- avoid using more than 3 levels
  for (int i=0; i<totalXXX; i++) {
for (int j=0; j<totalYYY; j++) {
if (flag==YES) {
// do something
// this is the max levels (3rd level)
}
}
  }
- use {} to embrace every level, even the statement is short
  (sometimes we are lazy and skip this, but try to keep this,
   because adding a new line of code may introduce problem)
- try to use for loop, avoid while loop without a clear
  signal of when to exit
  all while loop can transform to for loop with an EPOCH
  e.g. 
  while (condition==YES) {
     // do something
  }
  // transform to:
  #define EPOCH 10000   // this can be in Common.h
  for (int i=0; condition==YES && i<EPOCH; i++) {
// do something
  }
     


@property: readonly > retain > assign
- Object: always prefer readonly > retain > copy > assign
- primitive type(int, char, float, double, Byte): 
  readonly > readwrite   
  (assign is not needed, because it is always assign for write)
  Under the @public section, we can use Object->variable 
 is same as @property readwrite,  using readwrite is prefered
  e.g. 
@interface MyClass : NSObject {
@public 
int userCount;
}
@property (nonatomic, readwrite) int userCount; 
// need synthesis in .m

// in another class:
MyClass *myClass = [[myClass alloc] init];
  myClass->userCount = 5; // AAA 
  myClass.userCount = 5;  // BBB this is same as AAA
using BBB style and remove the @public is prefered.

- always include nonatomic, unless u need thread-safe access
- [NSMutableArray, NSDictionary]: prefer to alloc inside the 
  container class init (constructor) and release in the dealloc(destructor)
  and set @property as readonly
- NSString: set as retain if need to be written, set as copy
  is ok but a bit slow, most of the NSString are immutable, so 
  it is safe to do retain in most case.
  (note: NSMutableString can be set to a NSString pointer)
  

  评论这张
 
阅读(574)| 评论(0)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017