Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
Objective-C has been around for many many years and in that time, lots of conventions have come into being. Let’s talk through some of the biggies so you know what to do and what not to do, when working in Objective-C.
Teacher’s Notes
-
0:00
[MUSIC]
-
0:04
Objective-C has been around for quite a while, and
-
0:07
during that time lots of conventions have come into being.
-
0:10
Some are simply styles for naming methods or variables.
-
0:13
Others are more conceptual and
-
0:14
represent idiomatic ways of handling common problems.
-
0:18
Let's poke around a bit and answer a few of the whys that might be floating
-
0:21
around in your head as you explore Objective-C code bases.
-
0:25
So in this video we'll focus on naming conventions.
-
0:28
In terms of idiomatic ways of handling problems, we'll get a sampling of that as
-
0:32
we explore design patterns and some complete Objective-C apps.
-
0:36
Most of the naming conventions I'll describe are simply accepted best practice
-
0:40
in terms of style, while other naming conventions must be followed in order for
-
0:44
things to work correctly.
-
0:45
I'll point out the latter when it applies,
-
0:47
as that could really come back to bite you.
-
0:49
Let's begin with class names.
-
0:51
There are a few basic rules to follow here.
-
0:53
Apple framework classes used to letter prefixes like NSArray,
-
0:58
CALayer, or UILabel.
-
1:00
We've talked about what NS stands for,
-
1:02
but the CA and UI are more relevant to your everyday life.
-
1:06
They signal that the class belongs to the Core Animation framework and
-
1:10
the UI Kit framework, respectively.
-
1:12
Alternatively, when you create your own classes in your projects,
-
1:15
you should generally use a three letter prefix that makes sense for
-
1:19
your project and organization.
-
1:21
Since the apps we've built in this course are really tiny,
-
1:24
I just called classes animal or canine.
-
1:26
But in a real world app, something like TRHAnimal for
-
1:30
tree house animal would be clearer.
-
1:33
Classes use CamelCase.
-
1:35
Instances of classes use lower camelCase.
-
1:38
Classes should always be named after what they are or do in terms of your app.
-
1:43
For instance, ABCLoginViewController or XYZPdfMaker.
-
1:48
Lastly, class names should be unique across your app.
-
1:52
As for method names, in general they should be expressive and descriptive.
-
1:58
The names shouldn't just make sense, but they should convey useful meaning and
-
2:01
context to anyone reading your code.
-
2:03
Avoid abbreviations unless they're commonly used, and
-
2:06
I've linked a list of those below.
-
2:08
Method names should be lower camelCase.
-
2:10
That's different than many languages like C# that use upper CamelCase.
-
2:14
The method names you choose only need to be unique within a class,
-
2:19
though remember that if you use that name again in a subclass, it will override
-
2:23
the superclass's implementation when you call it on the subclass.
-
2:27
Be sure to name your parameters descriptively but concisely.
-
2:31
Otherwise they can quickly get out of control.
-
2:33
And if you're passing an error as a parameter, list that last.
-
2:36
There are also a few required conventions for methods we should point out.
-
2:40
For accessors, if you want to set a property called userEmail,
-
2:45
you should name that method setUserEmail.
-
2:48
The getter is simply the property name.
-
2:50
However, if the property you're setting and getting is a boolean, like LoggedIn,
-
2:54
you should use the set prefix for the setter and the is prefix for the getter.
-
2:59
These conventions are critical if you wanna make use of key value coding and
-
3:02
key value observing, which are both really powerful.
-
3:05
And yes, I've linked to some resources on those below as well.
-
3:09
Lastly, if you wanna create a factory method, let's say for
-
3:12
the Animal class, you use a lower camelCase of the class name like so.
You need to sign up for Treehouse in order to download course files.
Sign up