O'Reilly logo

Beginning Mac OS® X Programming by Drew McCormack, Michael Trent

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required