Retired Document
Important: This document may not represent best practices for current development. Links to downloads and other resources may no longer be valid.
Coding conventions
Coding conventions are a set of guidelines that help to ensure efficiency and consistency in API usage and clarity and consistency in API naming. If you following the usage conventions in your code, you are less likely to experience problems such as runtime exceptions. If you adhere to the naming conventions, any methods, functions, constants, or other symbols that you declare will be better understood by developers who need to work with your code.
The methods defined in Cocoa frameworks—for example, Foundation, AppKit, and UIKit—behave in certain ways in different circumstances. For example:
Methods that return objects typically return
nil
if they cannot create or find the object. They do not directly return a status code.Methods that perform an operation return a Boolean value to indicate success or failure.
If a method takes a collection object—that is, an
NSArray
,NSDictionary
, orNSSet
object—as an argument, do not specifynil
to indicate “default” or “no value”; instead, pass in an empty collection object.If you are explicitly managing program memory, follow the guidelines and practices for memory management.
Here is a sampling of API naming conventions:
Clarity and brevity are both important, but clarity should never be sacrificed for brevity.
Avoid names that are ambiguous.
Use verbs in the names of methods or functions that represent actions.
Use prefixes for class names and for symbols associated with the class, such as functions and data types.
Copyright © 2018 Apple Inc. All Rights Reserved. Terms of Use | Privacy Policy | Updated: 2018-04-06