Learn Objective-C: Cocoa Naming Conventions
The Cocoa framework and applications that use Cocoa (Touch) follow a fixed set of naming conventions that make the programs easier to read, use, and understand.
General Principles
-
Keep your code brief, but never at the expense of clarity. You’ll often see long method names in Cocoa- some programmers will complain about them, but the truth is that they make the code that much more understandable. And with Xcode’s code completion, you really don’t have to worry about all that extra typing. As an example, consider the method name
removeObjectAtIndex:
. That’s a very clear method name- you’re removing an object, and the argument takes an index. However, the method remove: is significantly less clear. Are you passing in an object to be removed, or an index to be removed? Or something else? The method never specifies. -
Don’t add redundancy to names. Prefixes such as “my” or suffixes such as “object” are (almost, in the case of the latter) always unnecessary—it is obvious that it’s your variable, or that (in many cases) it’s an object.
-
95% of the time, don’t abbreviate unless it is a well-known technical term, such as PDF or URL. Abbreviations are often not as obvious as you think they are, especially for developers from other lingual backgrounds.
-
Avoid noun/adjective-verb dualities. For instance, you may encounter a method called displayText. Does that method perform the action of displaying the text, or does it signify the text that is displayed (versus some private identifier text)? The developer has no way of knowing.
-
Consistency is important. Don’t have a count method in one class and a size method in another. Classes which have methods of the same name should do the same thing with those methods.
-
Prefixes are encouraged in Cocoa, and are used throughout Cocoa itself. Foundation and many desktop Cocoa classes begin with NS; UIKit classes begin with UI. Many developers who publish source code will prefix things with their initials, or some initialism of their company. Prefixes are especially important in determining who wrote the code, especially with generic names, such as the String class (which exists in languages such as Java).
-
Class names should have a noun involved. Other names, especially enumerated types/state representations, should have some resemblance to the class name. For example,
UITableViewCell
is a class, and you can specify a style using constants such asUITableViewCellStyleDefault
orUITableViewCellStyleSubtitle
. These constants are long, but make it clear that they work withUITableViewCell
and involve its style. If you just had Default and Subtitle, they’re very ambiguous, could be used by other classes (which could lead to interesting mix-ups and collisions), and are not at all clear about what they’re referring to. -
Categories are usually expressed as
className+categoryName
, such as the fictitiousNSString+HTMLFormatting
. Note there the “HTML” abbreviation, which is completely acceptable because HTML is widely understood. -
Delegate and Data Source protocols will have the descriptor in the name- note
UITableViewDelegate
andUITableViewDataSource
. -
Other protocol names usually take a verb form of the noun they modify. For example, the
NSCoding
protocol works withNSCoder
andNSDecoder
classes.
Method Names
-
Method names usually begin with a lowercase letter, and each subsequent “word” in the name has its initial letter capitalized. Avoid abbreviations that are not clear. Most methods should not begin with a prefix, but you may use a prefix to denote a private method. You may also use a leading underscore (no, Apple does not reserve this method, although any Apple method with a leading underscore is supposed to be private).
-
Methods that perform an action should start with a verb. Auxiliary verbs such as “do” are not necessary and add nothing to the method except extra typing. According to the documentation, “never use adverbs or adjectives before the verb.”
-
If the method returns an attribute, the method should have the same name as the attribute, without any other modifiers, such as “calculate” or “get”. This is the naming convention that getters for properties follow.
-
Name all arguments, and make the name describe the argument. See the remove: example above.
-
Only use “and” to link two parts of a method that do different thing. For example, do not write
getValue:andData:
, but a method name likegetValue:andDelete:
is correct because it is doing two things—reading in a value and deleting (potentially) something else. -
Verbs should not be made into adjectives.
performsCalculation
is preferred tocalculationPerformed
Delegate & Data Source Methods
-
The first argument should be the object that is making the delegate or data source call. In certain cases, a class such as a view controller may be managing more than one object, such as a table view. In that case, the view controller can check to see which table view is calling the method.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { if (tableView == tableView1) // return 1; else if (tableView == tableView2) // return 2; else // return something else }
You’re comparing instances here, so you can use the
==
operator. They’ll both point to the same thing, because you’re just passing pointers around in memory, there is only one instance oftableView1
andtableView2
. -
Delegate methods often use “did” and “will” to notify the delegate that something is about to happen, or has happened. This allows the delegate to prepare or setup some data, or to clean up after an operation. The terms provide a sense of time.
-
Methods that ask a delegate for permission, such as
shouldDeleteObjectAtIndex:
, should use the term “should”.
Miscellaneous
-
Use enumerated types, often with a
typedef
, to declare constants, rather than plain integers. This gives a name to the numbers, and makes it much more meaningful later.UITableViewCellStyleDefault
is a lot more meaningful than simply0
. -
Preprocessor constants are usually in all-caps, with an underscore separating the words,
LIKE_THIS
. Preprocessor macros maybe be named either like this, or as a standard method name; the former is preferred. Preprocessor macros defined by the compiler have leading and trailing double underscores, such as__LINE__
. -
Exceptions are named as
Prefix+UniqueName+Exception
, such asNSIllegalSelectorException
orNSRangeException
. A much more in-depth look at naming conventions, along with more examples and more rules is available from Apple’s developer documentation:https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/CodingGuidelines/CodingGuidelines.html#//apple_ref/doc/uid/10000146-SW1
This post is part of the Learn Objective-C in 24 Days course.
Previous Lesson | Next Lesson |