文档学习: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.

相关文章

我正在用TitaniumDeveloper编写一个应用程序,它允许我使用Ja...
我的问题是当我尝试从UIWebView中调用我的AngularJS应用程序...
我想获取在我的Mac上运行的所有前台应用程序的应用程序图标....
我是一名PHP开发人员,我使用MVC模式和面向对象的代码.我真的...
OSX中的SetTimer在Windows中是否有任何等效功能?我正在使用...
我不确定引擎盖下到底发生了什么,但这是我的设置,示例代码和...