oc命名规范

来源:互联网 发布:菊正宗化妆水 知乎 编辑:程序博客网 时间:2024/05/22 11:55

本文翻译自苹果开发者网站,对原文有所删减,能力好的同学,可以直接看原文
翻译可能有些不到位,请见谅。

基本规则

一般规则

  • 命名简短清晰

    code Commentary insertObject:atIndex: Good insert:at: Not clear,插入什么,at指代什么 removeObjectAtIndex: Good removeObject: Good remove: Not clear,删除什么
  • 一般情况下不要缩写,即使命名变得很长

    code Commentary destinationSelection: Good destSel Not clear setBackgroundColor: Good setBkgdColor: Not clear
  • 相同功能的方法可以使用相同的名字

    code Commentary (NSInteger)tag Defined in NSView, NSCell, NSControl (void)setStringValue:(NSString *) Defined in a number of Cocoa classes

前缀

我们在命名类、协议、函数(类方法)、常量和自定义数据类型时使用前缀,不要在方法(对象方法)命名中使用。使用前缀使代码更具区分性,前缀通常由2到3个大写字母组成,下面是一些前缀的例子

Prefix Cocoa Framework NS Foundation NS Application Kit AB Address Book IB Interface Builder

Class的命名

类名应该包含一个可以准确描述类的特征或功能,同时应该以前缀开始。

方法命名

一般规则

  • 采用小驼峰的方式,尽量不要使用缩略词作为方法的起始命名,以下两种情况除外。

    • 除非你是采用缩略词作为一种标识,表示这是同一组方法或者这是你编写的方法。例如:NSRunAlertPanel和NSCellDisabled
    • 你采用的缩略词具有明确的含义。
  • 方法表示一种动作时采用动词作为方法的开始命名 。不要采用“do”或“does”作为命名的一部分,因为这两个辅助词几乎不能明确动作。

    - (void)invokeWithTarget:(id)target;- (void)selectTabViewItem:(NSTabViewItem *)tabViewItem
  • 除非间接地返回一个或多个值,否则编写geter方法时,不必使用getXXX的形式,直接使用属性值就可以。(PS:这也是OC和其它语言的一个比较明显命名差异)

    例子 对错 (NSSize)cellSize Right (NSSize)calcCellSize Wrong (NSSize)getCellSize Wrong
  • 在所有参数前使用关键字(ps:OC最令我吐槽的写法之一)

    例子 对错 (void)sendAction:(SEL)aSelector toObject:(id)anObject forAllCells:(BOOL)flag Right (void)sendAction:(SEL)aSelector :(id)anObject :(BOOL)flag; Wrong
  • 方法名应该带有参数名(ps:原文是:Make the word before the argument describe the argument,不知怎么准确表述,看例子领会)

    例子 对错 (id)viewWithTag:(NSInteger)aTag Right (id)taggedView:(int)aTag Wrong
  • 当你在已有方法基础上创建新的含义更加具体的函数时,在原有命名的末尾添加新的关键字(ps:最典型就是重写自己的初始化方法时)

    例子 类 (id)initWithFrame:(CGRect)frameRect NSView, UIView (id)initWithFrame:(NSRect)frameRect mode:(int)aMode cellClass:(Class)factoryId numberOfRows:(int)rowsHigh numberOfColumns:(int)colsWide NSMatrix, a subclass of NSView
  • 方法表示一个动作时,尽量不要使用“and”连接你的函数名,因为如果你的函数很长,你要多写很多个“and”

    例子 对错 (int)runModalForDirectory:(NSString )path file:(NSString ) name types:(NSArray *)fileTypes Right (int)runModalForDirectory:(NSString )path andFile:(NSString )name andTypes:(NSArray *)fileTypes Wrong
  • 如果方法描述两个分离的动作,你就可以使用“and”来连接

    例子 类 (BOOL)openFile:(NSString )fullPath withApplication:(NSString )appName andDeactivate:(BOOL)flag NSWorkspace

getter和setter方法的命名

  • 属性是一个名词
    - (NSString *)title;    - (void)setTitle:(NSString *)aTitle;
  • 返回类型是布尔值,表示一个形容词
- (BOOL)isEditable;- (void)setEditable:(BOOL)flag;
  • 返回类型是布尔值,表示一种意图或动作
- (BOOL)showsAlpha;- (void)setShowsAlpha:(BOOL)flag;
  • 不要将动词转化为形容词(ps:无视翻译,看例子)

    例子 对错 (void)setAcceptsGlyphInfo:(BOOL)flag Right (BOOL)acceptsGlyphInfo Right (void)setGlyphInfoAccepted:(BOOL)flag Wrong (BOOL)glyphInfoAccepted Wrong
  • 使用can,should,will等单词代替do和does

    例子 对错 (void)setCanHide:(BOOL)flag Right (BOOL)canHide Right (void)setShouldCloseDocument:(BOOL)flag Right (BOOL)shouldCloseDocument Right (void)setDoesAcceptGlyphInfo:(BOOL)flag Wrong (BOOL)doesAcceptGlyphInfo Wrong
  • 仅仅在间接返回值或者返回多个值时考虑使用getXXX的命名方式

    例子 类 (void)getLineDash:(float )pattern count:(int )count phase:(float *)phase NSBezierPath

Delegate方法的命名

  • 发送消息(调用方法)的类名作为方法的开头命名(ps:感觉就是第一个参数的类型)

    - (BOOL)tableView:(NSTableView *)tableView shouldSelectRow:(int)row;- (BOOL)application:(NSApplication *)sender openFile:(NSString *)filename;
  • 当方法的参数是一个notification时,可以不遵守上面规则

    - (void)windowDidChangeScreen:(NSNotification *)notification;
  • 使用did,will或shuold(返回值是布尔值)来表明delegate方法将会发生的事

    - (void)browserDidScroll:(NSBrowser *)sender;- (NSUndoManager *)windowWillReturnUndoManager:(NSWindow *)window;- (BOOL)windowShouldClose:(id)sender;

方法参数的命名

有下面几条常用的规则

  • 采用小驼峰方式命名 (例如, removeObject:(id)anObject)
  • 如果参数是一个指针,不要在参数中使用“ptr”或者“pointer”来命名,参数类型足以指明。
  • 不要采用一两个字母的命名
  • 不要采用意义不明的缩写

一般在Cocoa中有以下例子:

...action:(SEL)aSelector...alignment:(int)mode...atIndex:(int)index...content:(NSRect)aRect...doubleValue:(double)aDouble...floatValue:(float)aFloat...font:(NSFont *)fontObj...frame:(NSRect)frameRect...intValue:(int)anInt...keyEquivalent:(NSString *)charCode...length:(int)numBytes...point:(NSPoint)aPoint...stringValue:(NSString *)aString...tag:(int)anInt...target:(id)anObject...title:(NSString *)aString

类的属性和数据类型的命名

声明@property的属性

@property (strong) NSString *title;//名词@property (assign) BOOL showsAlpha;//动作@property (assign, getter=isEditable) BOOL editable;//形容词

声明实例变量时,使用”_XXX”的形式

@implementation MyClass {    BOOL _showsTitle;}

声明枚举,采用缩略词开头,表明它们属于同一组

typedef enum _NSMatrixMode {    NSRadioModeMatrix           = 0,    NSHighlightModeMatrix       = 1,    NSListModeMatrix            = 2,    NSTrackModeMatrix           = 3} NSMatrixMode;

命明Notifications,采用以下规则

[Name of associated class] + [Did | Will] + [UniquePartOfName] + Notification[相关的类名] + [Did | Will] + [表明动作或意图的名字] + Notification------------------------------------------------NSApplicationDidBecomeActiveNotificationNSWindowDidMiniaturizeNotificationNSTextViewDidChangeSelectionNotificationNSColorPanelColorDidChangeNotification

命名Exceptions,和Notifications类似采用以下规则

[Prefix] + [UniquePartOfName] + Exception[前缀] + [名字] + Exception--------------------------NSColorListIOExceptionNSColorListNotEditableExceptionNSDraggingExceptionNSFontUnavailableException
1 0
原创粉丝点击