Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Protocol Naming in Objective C

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?

like image 567
Undistraction Avatar asked Sep 20 '11 10:09

Undistraction


1 Answers

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.

like image 76
Stuart Avatar answered Sep 17 '22 15:09

Stuart