01.02.2014 Views

Objective-C Fundamentals

Objective-C Fundamentals

Objective-C Fundamentals

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Variable naming conventions<br />

295<br />

As IDEs have become more powerful with features such as Code Sense (Apple’s equivalent<br />

to IntelliSense), the use of Hungarian notation has quickly lost favor among developers.<br />

It’s now quick enough to determine the data type associated with a variable<br />

without encoding it into the name. Most usage of Hungarian notation is now confined<br />

to C-based source code; it’s uncommon to see this convention used in applications that<br />

embrace <strong>Objective</strong>-C. This is not to say that other naming conventions have also faded;<br />

for example, camel case is still popular among <strong>Objective</strong>-C developers.<br />

B.1.2<br />

B.1.3<br />

Camel case<br />

It’s helpful to make identifiers as descriptive and self-documenting as possible. As an<br />

example, a variable named CountOfPeople is more meaningful than a variable simply<br />

named n.<br />

Because identifiers can’t contain spaces, one common technique for achieving this<br />

goal is to capitalize the first letter of each word, as seen in the variable name CountOf-<br />

People. This technique is named camel case for the “humps” the uppercase letters<br />

spread throughout the identifier. Another alternative to camel case is the use of<br />

underscores to separate words: Count_Of_People.<br />

Namespaces<br />

When your applications start getting more complex or you start to use code developed<br />

by a third party, one problem you may come across is a clash between identifiers. This<br />

occurs when two separate parts of the application attempt to use the same identifier to<br />

represent different things. For example, your application may name a variable Counter,<br />

and a third-party library might name a class Counter. If you were to refer to Counter<br />

in your source code, the compiler would be unable to determine which item you were<br />

referring to.<br />

Some programming languages resolve this problem via a mechanism called<br />

namespaces. A namespace allows you to group related identifiers into separate groupings<br />

or containers. For example, all the code written by Apple could be placed inside<br />

one namespace, and all the code written by you could be placed inside another. Each<br />

time you reference a variable or function, you must provide not only the name of the<br />

identifier but also its containing namespace (although this may be implied rather<br />

than explicit). In this manner, if two regions of code attempt to reference something<br />

called Counter, the compiler can differentiate between the two interpretations and<br />

understand your intent.<br />

The C programming language doesn’t provide a concept equivalent to that of<br />

namespaces, but it’s possible to simulate enough of their behavior to avoid collisions<br />

by using commonly agreed-upon prefixes for your identifiers.<br />

A good example of this technique is the venerable NSLog function. This function<br />

can be thought of as having the name log in a namespace called NS. NS is the<br />

namespace prefix Apple uses for the Foundation Kit library; as such, it would be unwise<br />

to start the name of any of your variables or functions with the NS prefix because it<br />

would increase the likelihood of its clashing with a preexisting Apple function.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!