文档学习:Coding Guidelines for Cocoa

  主要讲述编码规范。注意,函数名、类名、常量名的编写规范。

文档地址:http://www.apple.com.cn/developer/mac/library/documentation/Cocoa/Conceptual/CodingGuidelines/CodingGuidelines.html#/apple_ref/doc/uid/10000146

命名

  1. It is good to be both clear and brief as possible, but clarity shouldn’t suffer because of brevity:

  示例:

   insertObject: atIndex: good
insert:at: not clear; what is being inserted? what does “at” signify

2. 少用缩写,In general, don’t abbreviate names of things. Spell them out, even if they’re long:,但是一些常用的简写是可用的,比如:alloc,temp等等

destinationSelection good
destSel not clea

3. 避免歧义,Avoid ambiguity in API names, such as method names that could be interpreted in more than one way.

sendPort Does it send the port or return it?
displayName Does it display a name or return the receiver’s title in the user interface?

4. 一致性,同样功能的函数或接口定义通用可实现多态性的名字

5. 前缀名,比如:NS,UI等等。Use prefixes when naming classes, protocols, functions, constants, and typedef structures. Do not use prefixes when naming methods; methods exist in a name space created by the class that defines them. Also, don’t use prefixes for naming the fields of a structure

排布

1. 写一个组合名字时,不要用符号(_、-等等)来分隔不同的单词。应该为单词首字母大写,然后拼在一起。

For method names, start with a lowercase letter and capitalize the first letter of embedded words.Don’t use prefixes.
fileExistsAtPath:isDirectory:

For names of functions and constants, use the same prefix as for related classes and capitalize the first letter of embedded words.
NSRunAlertPanel
NSCellDisabled

2. 不要用“_”前缀表示私有的方式命名,尤其是对于方法。

因为Apple底层实现使用了这种方式处理隐藏的成员,当你这样用的时候很可能会屏蔽底层实现中的名字,造成不应出现的问题。:Apple reservesthe use of this convention. Use by third parties could result in name-space collisions; they might unwittingly override an existing private method with one of their own, with disastrous consequences.
See "Private Methods" (page 18) for suggestions on conventions to follow for private API.



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值