2011-12-07 195 views
23

我目前使用下列約定目標C方法的命名約定

- (id) initWithName:(NSString *) name; 

+ (NSString *) aliasForName:(NSString *) name 

- (void) method 

- (void) methodWithApple:(NSString *) apple andOrange:(NSString *) orange 
andMango:(NSString *) mango 

- (void) statusWasChanged:(id)sender 

你有上面的方法更好的風格?

感謝

+1

對於'initWithName'確保您瞭解ARC將如何對待這個(即返回保留的對象) – Thilo

+1

你的命名約定好.. –

回答

52

Coding Guidelines for Cocoa是回答任何命名約定問題的好資源。我的回答儘可能基於此。

初始化方法

init方法看起來不錯。

- (id) initWithName:(NSString *) name; 

類方法

類方法看起來不錯。

+ (NSString *) aliasForName:(NSString *) name 

類方法也可用於實例化對象的實例。在這種情況下,蘋果的API通常有類似的UIButtonbuttonWithType:方法的類名稱的方式啓動具有簽名:

+ (id)buttonWithType:(UIButtonType)buttonType 

實例方法

良好的資源用於編碼規範的方法可以是根據General Rules找到。

下面的方法應該放棄的"and" S:

- (void) methodWithApple:(NSString *) apple andOrange:(NSString *) orange 
andMango:(NSString *) mango // BAD 

不要使用「and」來連接兩個表示接受者屬性的關鍵字。

- (int)runModalForDirectory:(NSString *)path file:(NSString *) name types:(NSArray *)fileTypes;

- (int)runModalForDirectory:(NSString *)path andFile:(NSString *)name andTypes:(NSArray *)fileTypes;錯誤

簽名應該看起來更像如下:

- (void) methodWithApple:(NSString*)apple orange:(NSString*)orange 
mango:(NSString*)mango // GOOD 

Delegate Methods

最後,我認爲有可能在什麼似乎是一個委託方法制成的幾個改進:

- (void) statusWasChanged:(id)sender // Not horrible, but not ideal 

第一個改進是將類名添加到方法。

通過識別類在嘲弄 消息的對象的啓動名稱:

- (BOOL)tableView:(NSTableView *)tableView shouldSelectRow:(int)row; 
- (BOOL)application:(NSApplication *)sender openFile:(NSString *)filename; 

第二個改進是使用"DidChange"而不是"WasChanged"

將「did」或「will」用於被調用來通知 委託人已發生或即將發生事件的方法。

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

第三個改進強烈鑄造sender參數。我沒有文檔來支持這個,但是這些例子中提供的所有例子都表明了這種行爲。注意上述代碼示例中的(NSBrowser*)sender(NSWindow*)window直接從apple文檔中獲取。

考慮到這一點,委託方法應該看起來更像是:

- (void) senderClassNameStatusDidChange:(SenderClassName*)sender // Good 

如果發件人是一個Person對象它看起來像:

- (void) personStatusDidChange:(Person*)sender // Good 

一個忠告就是你不應該總是在委託方法中使用「did」。

雖然可以用「沒有」或「會」爲所調用的方法 要求委託做代表其他物體的東西,「應該」 是首選。

- (BOOL)windowShouldClose:(id)sender; 
+3

+ 1爲基於文檔的意見。 :) –

+0

正如Rahul指出的那樣,斯科特史蒂文森的Cocoa約定指南在命名時是另一個有用的資源:http://cocoadevcentral.com/articles/000082.php –

1

我要說的只有一個我不知道的是:

- (void) methodWithApple:(NSString *) apple andOrange:(NSString *) orange 
andMango:(NSString *) mango 

這似乎是「和」在過去的兩個參數或者是不必要的或應代之以動詞。我認爲一個好名字確實在很大程度上取決於呼叫的背景下,什麼將與發送的參數來完成。

1
- (id) initWithName:(NSString *) name; 

任意以init方法由框架的方法返回一個保留的理解對象(例如initWithObjectsAndKeysdictionaryWithObjectsAndKeys之間的差異)。所以你應該考慮到這一點,特別是在使用ARC時。

+ (NSString *) aliasForName:(NSString *) name 

使用相同的慣例,這種類型的方法會返回自動釋放的對象(靜態方法或沒有)

- (void) method 

我要說的是,如果只有一個詞,這個詞是名詞,它應該是屬性的吸氣(如viewsuperview ...)。否則,如果它是一個動詞,爲什麼不添加它引用的對象?像closeModalViewController

- (void) methodWithApple:(NSString *) apple andOrange:(NSString *) orange 
andMango:(NSString *) mango 

我的確會掉落and

- (void) statusWasChanged:(id)sender 

更蘋果-Y的辦法是statusDidChange:

+0

+1很好的回答。我認爲'statusWasChanged:'同樣的事情,直到我意識到委託方法,這是我假設,是缺少發件人的名字。它應該更像' - (void)senderNameStatusDidChange:(id)sender'。所以如果發件人是一個人,它會像' - (void)personStatusDidChange:(Person *)person'。關於這一點的文檔通常強烈地發送發送者。參見[委託方法] [http://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/CodingGuidelines/Articles/NamingMethods.html#//apple_ref/doc/uid/20001282-1001803-BCIDAIJE] – Sam

0

這些都是除了「和」良好的命名約定。我傾向於看看Google Style Guide'。