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

WA!Peter

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

 
 
 

日志

 
 

iPhone Code Style 代码规范(TODO中文翻译)  

2010-07-08 17:56:09|  分类: iphone |  标签: |举报 |字号 订阅

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

TODO: add no change on (for) loop elements(array/dictionary)

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

原则:
- 可读性
- 一致性
- 正字, 不用缩略字 (除非是绝大部分人知道的)
- 不用中文拼音作为变量命名
- 变量/函数名称: 小写开头, 多于一字的话第二字开头用大写作为分隔( 例: playerName )
-常数: 全部大写, 使用下划线 _ 分隔  例:  MAX_PLAYER


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 xxxCount for number of xxx,  use length for byte size
  MAX_XXX for constant.
- other counting: xxxCount(prefer), xxxLength(prefer), maxXXX,
  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)
            }
        }
  }
- you may use continue or break to reduce cascading 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 > readwrite
- always prefer readonly > retain(copy) > readwrite(assign)
- primitive type(int, Byte):  @public is same as @property readwrite
- [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 for readonly(immutable),  set as copy if you modify later
  copy is ok but a bit slow, most of the NSString are immutable, in most case
  choose retain
- in case of "two ways" reference, you may use assign, e.g.
  leader.members(NSArray) contains a list of member
  member.leader = leader   (this can be assign)
  if both of them are using retain, memory will never be released!
  note: you need to check when the leader is release, member.leader should be set nil
- Avoid this pattern:
for (obj in array)  {
        if (SOME_CONDITION) {
              [array removeObject:obj];
        }
  }
  评论这张
 
阅读(470)| 评论(0)
推荐 转载

历史上的今天

评论

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

页脚

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