7.8. Naming Conventions

At several points in this chapter you have encountered Objective-C conventions. Informal protocols are a good example of a concept that is not built into the language proper, but pervades Objective-C/Cocoa programming. Understanding conventions like this can be just as important as grasping the formal aspects of the language, such as its syntax.

Objective-C has many conventions, and some of them apply to naming. You may think that you can name functions, variables, and classes in any way you like, within the rules laid down by the Objective-C grammar. That is true, but if you do not stick to the naming conventions of Objective-C/Cocoa, your programs will be at a disadvantage, and not have access to certain functionality.

You have probably already worked out most of the naming conventions, simply by reading the example code. Classes and variables should be in mixed-case format, with all words beginning with a capital letter, and all other letters lowercase. The exception to this rule is that variable names should begin with a lowercase letter; classes begin with an uppercase letter. So you might have a class called ClassWithALongName, and a variable called variableWithALongName.

Accessor methods, which were introduced earlier, should also follow a convention: Setters should begin with set, and be followed by the corresponding variable in mixed-case form, such as setVariableWithALongName:. Getters should have the same name as the variable they are accessing, ...

Get Beginning Mac OS® X Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.