29

I'm struggling with naming protocols in Objective-C. For example:

I have a protocol called Command. I have an abstract class that implements Command that is a base class for my concrete Commands.

I believe it is possible to call both the protocol and the base class 'Command' but this is confusing and will cause import clashes if I need to reference the protocol in an implementation. I also understand that in Objective C, using a prefix to denote a protocol is bad form. Some examples use 'ing' added to the end, but in this instance that makes no sense. Calling the abstract class 'CommandBase' seems wrong as well.

So how should I name them?

3 Answers 3

26

I would suggest that in your case it is not necessarily bad to name your protocol and the base class the same thing, as your class is the principal expression of the protocol (such as with NSObject).

From Apple's Coding Guidelines for Cocoa: Code Naming Basics:

Some protocols group a number of unrelated methods (rather than create several separate small protocols). These protocols tend to be associated with a class that is the principal expression of the protocol. In these cases, the convention is to give the protocol the same name as the class. An example of this sort of protocol is the NSObject protocol. This protocol groups methods that you can use to query any object about its position in the class hierarchy, to make it invoke specific methods, and to increment or decrement its reference count. Because the NSObject class provides the primary expression of these methods, the protocol is named after the class.

Sign up to request clarification or add additional context in comments.

6 Comments

So given that I can't put the protocol in a file of the same name, should the protocol reside in the header of the abstract class?
@1ndivisible: Yes, define the protocol at the top of your abstract class' header file. To use the same boring example as in my answer, take a look at the header file for NSObject - it actually defines a number of protocols in its header file, but the principal is the same for any abstract base class that implements a protocol in this way.
Thanks. I have a number of protocols that are shared amongst a set of framework classes. They don't particularly belong to any classes but are used amongst them. Would you say it would be acceptable to place the protocols together in a separate header file?
@1ndivisible: Generally speaking, I wouldn't do that since it breaks encapsulation to some degree. It might not cause any problems for your code, but I would say that it's best to keep them separate and import only the ones you need into a given framework class. I'd only define them all in the same place if they are either all related (e.g. protocols for an object and its delegate), or if a base class implements the methods for a variety of protocols (in which case I'd define them inside that base class' header).
|
17

All covered in Apple's Coding Guidelines For Cocoa in the section Code Naming Basics.

The author states:

Protocols should be named according to how they group behaviors:

Most protocols group related methods that aren’t associated with any class in particular. This type of protocol should be named so that the protocol won’t be confused with a class. A common convention is to use a gerund (“...ing”) form:

NSLocking - Good.

NSLock - Poor (seems like a name for a class).

Some protocols group a number of unrelated methods (rather than create several separate small protocols). These protocols tend to be associated with a class that is the principal expression of the protocol. In these cases, the convention is to give the protocol the same name as the class.

An example of this sort of protocol is the NSObject protocol. This protocol groups methods that you can use to query any object about its position in the class hierarchy, to make it invoke specific methods, and to increment or decrement its reference count. Because the NSObject class provides the primary expression of these methods, the protocol is named after the class.

Comments

-3

if you will see predefine protocal of uitableview, NSUrlconnection then u will get the name of protocal just like UItabaleviewDelegate and NSUrlconnectionDelegate. ........

Then you can undertand easy which delegate is belong from which class

So u can use your classnameDelegate as protocal name ....thanks

1 Comment

(-1) The question is not regarding a delegate protocol. In the examples you cite (UITableView & NSURLConnection) the associated protocols are implemented by a delegate object - a different object that conforms to the delegate protocol. The question is about a protocol that defines behaviour in an abstract base class, for which the methods are implemented by the base class itself (or the concrete subclasses). For this situation the <ClassName>Delegate protocol naming convention does not apply.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.