Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective C method naming convention

Tags:

I am currently using the following conventions

- (id) initWithName:(NSString *) name;  + (NSString *) aliasForName:(NSString *) name  - (void) method  - (void) methodWithApple:(NSString *) apple andOrange:(NSString *) orange andMango:(NSString *) mango  - (void) statusWasChanged:(id)sender 

Do you have a better style for the above methods?

Thanks

like image 612
Ryan Avatar asked Dec 07 '11 04:12

Ryan


People also ask

What should be the naming convention for methods?

Methods should be verbs, in mixed case with the first letter lowercase, with the first letter of each internal word capitalized. Except for variables, all instance, class, and class constants are in mixed case with a lowercase first letter. Internal words start with capital letters.

What is naming convention in C?

In computer programming, a naming convention is a set of rules for choosing the character sequence to be used for identifiers which denote variables, types, functions, and other entities in source code and documentation.

What is naming convention and example?

A naming convention can include capitalizing an entire word to denote a constant or static variable (which is commonly done in Flash programming), or it could be a simple character limit in a coding language (such as SQL).

How do you write a method name?

While writing a method name we should follow the camel case i.e. first letter of the first word should be small and the first letters of the remaining (later) words should be capital.


2 Answers

Coding Guidelines for Cocoa is a great resource for answering any naming convention questions. My answer is as much as possible based off of this.

Init Method

The init method looks good.

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

Class Method

The class method looks good.

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

Class methods can also be used to instantiate an instance of an object. In this instance, Apple's API's generally have the method start with the name of the class like UIButton's buttonWithType: method that has the signature:

+ (id)buttonWithType:(UIButtonType)buttonType 

Instance Methods

Good resource for coding conventions for methods can be found under General Rules.

The following method should drop the "and"s:

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

Don’t use “and” to link keywords that are attributes of the receiver.

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

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

The signature should look more like the following:

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

Delegate Methods

Lastly, I think there are a couple improvements that could be made on what appears to be a delegate method:

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

First improvement is to add the class name to the method.

Start the name by identifying the class of the object that’s sending the message:

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

Second improvement is to use "DidChange" instead of "WasChanged".

Use “did” or “will” for methods that are invoked to notify the delegate that something has happened or is about to happen.

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

Third improvement is strongly casting the sender parameter. I don't have documentation to support this, however all examples provided in the examples exude this behavior. Notice the (NSBrowser*)sender and (NSWindow*)window in the above code sample taken straight from the apple docs.

With this in mind, the delegate method should look more like:

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

If the sender were a Person object it would look like:

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

A word of caution is that you shouldn't always use "did" in delegate methods.

Although you can use “did” or “will” for methods that are invoked to ask the delegate to do something on behalf of another object, “should” is preferred.

- (BOOL)windowShouldClose:(id)sender; 
like image 127
Sam Avatar answered Sep 23 '22 04:09

Sam


I would say the only one I'm not sure about is:

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

It seems like the "and" in the last two arguments is either unnecessary or should be replaced by a verb. I think a good name really depends a lot on the context of the call, and what will be done with the parameters sent in.

like image 34
Kendall Helmstetter Gelner Avatar answered Sep 19 '22 04:09

Kendall Helmstetter Gelner