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
Phew, we've covered quite a few different rules. In this video, let's take a small break from the new stuff to recap what we've learned so far
-
0:00
When naming methods, we want them to read like grammatical English phrases.
-
0:05
These methods' base names, along with the first argument label, can form one of
-
0:10
three things, a prepositional phrase, a grammatical phrase, or neither.
-
0:17
With a prepositional phrase, the argument label, or
-
0:20
just the first argument, should begin at the preposition.
-
0:24
Creating a method that looks like this.
-
0:27
An exception arises when more than the first argument
-
0:30
represents part of the abstraction that governs the function.
-
0:35
Here, we have a function that fades a views background color to a new color.
-
0:40
The background color is determined by passing in RGB values.
-
0:45
Since red, green, and
-
0:47
blue are all part of the same abstraction split across different arguments, having
-
0:52
the propositional label prepended to the first argument label, doesn't read well.
-
0:58
So in this case, we move the proposition to the base name, and
-
1:02
begin the argument label after the proposition to keep the abstraction clear.
-
1:07
Our function therefore, reads like this.
-
1:10
Next up,
-
1:11
a method name along with the first argument can form a grammatical phrase.
-
1:16
It's important to note here that we're only considering the first
-
1:19
argument when deciding if the phase is grammatical.
-
1:23
We'll get into that in just a second.
-
1:25
With a grammatical phrase we omit the first argument label entirely and
-
1:30
append any preceding words to the base name.
-
1:34
Finally, if the methods base name and
-
1:36
first argument label do not form a propositional phrase and
-
1:40
can't be written in such a way that it is grammatically correct,
-
1:44
then we include a default argument label something like this.
-
1:49
Note that I've been focusing on the first argument label this entire time.
-
1:53
And that's because regardless of what type of phrase it forms.
-
1:57
We always label any arguments after the first one.
-
2:02
Also, we're only ever considering the first argument to determine
-
2:06
whether we have a grammatical phrase because it's okay for
-
2:10
fluency to degrade after the first argument or
-
2:13
two when those arguments are not central to the cause meaning.
-
2:17
There are some considerations we have to make along with these,
-
2:20
however, because remember we have three main guidelines we established.
-
2:24
Omit needless words.
-
2:26
Include on the words to remove ambiguity and compensate for weak type information.
-
2:32
Take the example of the addSubview method.
-
2:35
The function signature looks like this.
-
2:39
The method forms a grammatical phrase with the first argument label.
-
2:43
So we've omitted the label and appended the label to the base name.
-
2:48
Let's look at another example from the iOS SDK.
-
2:52
Here, we have a method, a type method on the NS layout constraint class
-
2:58
that activates an array of constraints.
-
3:01
The method reads activate constraints which is a grammatical phrase.
-
3:05
However, you'll notice here that while the argument label has been omitted
-
3:10
much like we did with add some view,
-
3:12
the word constraints is not added to the methods base name.
-
3:17
Why?
-
3:19
This is because of the guideline to omit needless words.
-
3:23
The argument has a type of an array of NSLayoutConstraint
-
3:28
which is a very specific type.
-
3:31
We don't typically subclass constraints to make our own.
-
3:34
So it's evident from our own signature that we're adding an array of constraints.
-
3:39
We don't need to label constraints here because it is redundant, and
-
3:44
we can rely on the type information.
-
3:46
There is one final edge case left.
-
3:49
After this, I'll stop, I promise.
-
3:51
What happens if we have a function where the arguments can't be
-
3:55
usefully distinguished?
-
3:57
Here, we have a function max that returns the maximum
-
4:01
of two values that we provide as arguments.
-
4:04
There's really no meaningful way to label these arguments in a max function
-
4:08
beyond value one in value two.
-
4:11
In such cases, we just omit all labels altogether.
-
4:15
It sounds like this goes against everything we've stated so far but
-
4:19
in practice if you choose your function based names well
-
4:23
it's quite obvious what's going on.
-
4:26
Now, I'll stress here that you don't have to adhere to all these rules on day one.
-
4:31
In fact, take it one at a time until these come to you naturally
You need to sign up for Treehouse in order to download course files.
Sign up